Skip to content

Conversation

@NikKovIos
Copy link
Contributor

Blocked by #1800

@calda calda changed the base branch from main to develop July 31, 2024 16:35
@calda
Copy link
Collaborator

calda commented Jul 31, 2024

PRs are merged into develop rather than main. You'll need to resolve merge conflicts with #1782 and #1789 (the rule should go in a file name RuleName.swift and the tests should go in a file name RuleNameTests.swift.

}
}

public let spacesWithGuard = FormatRule(help: "Remove space between guard and add spaces after last guard.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A name like guardStatementSpacing or guardSpacing could be nice -- we have an existing rule named consistentSwitchCaseSpacing, which is somewhat similar. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to guardSpacing

@@ -0,0 +1,156 @@
// Created by @NikeKov on 27.07.2024
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of #1789 this file should be Tests/Rules/SpaceWithGuardTests.swift (or whatever we end up naming the rule)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

// GuardSpacingTests.swift
// GuardSpacingTests
//

import XCTest
@testable import SwiftFormat

class GuardSpacingTests: RulesTests {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the test class should match the rule name

Suggested change
class GuardSpacingTests: RulesTests {
class SpaceWithGuardTests: RulesTests {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed

public let spacesWithGuard = FormatRule(help: "Remove space between guard and add spaces after last guard.",
disabledByDefault: true)
{ formatter in
func leaveOrSetLinebreaksInIndexes(_ indexes: Set<Int>, linebreaksCount: Int) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you move this rule to its own file (e.g. Sources/Rules/SpacesWithGuard.swift, it would probably be nicer to move this func leaveOrSetLinebreaksInIndexes to be in a separate extension Formatter at the bottom of the file, rather than being nested within the rule implementation closure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved

Comment on lines 8098 to 8099
let guardKeyword = Token.keyword("guard")
formatter.forEach(guardKeyword) { guardIndex, _ in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more common to just do:

Suggested change
let guardKeyword = Token.keyword("guard")
formatter.forEach(guardKeyword) { guardIndex, _ in
formatter.forEach(.keyword("guard")) { guardIndex, _ in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


let guardKeyword = Token.keyword("guard")
formatter.forEach(guardKeyword) { guardIndex, _ in
print("✳️ start \(guardIndex)")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to clean up these print statements before merging!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned

Comment on lines 8113 to 8121
switch nextNonSpaceAndNonLinebreakToken {
case let .endOfScope(string):
if string == "}" {
// Do not add space for end bracket
return
}
default:
break
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler way to write this would be:

Suggested change
switch nextNonSpaceAndNonLinebreakToken {
case let .endOfScope(string):
if string == "}" {
// Do not add space for end bracket
return
}
default:
break
}
if nextNonSpaceAndNonLinebreakToken == .endOfScope("}") {
// Do not add space for end bracket
return
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

if alreadyHasLinebreaksCount != linebreaksCount,
let firstIndex = indexes.first
{
formatter.insert(.linebreak("\n", 0), at: firstIndex)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

Suggested change
formatter.insert(.linebreak("\n", 0), at: firstIndex)
formatter.insertLinebreak(at: firstIndex)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Collaborator

@calda calda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I'll take another look after the merge conflicts are resolved.

Rules.md Outdated
* [wrapEnumCases](#wrapEnumCases)
* [wrapMultilineConditionalAssignment](#wrapMultilineConditionalAssignment)
* [wrapSwitchCases](#wrapSwitchCases)
* [spaceWithGuard](#spaceWithGuard)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you modify the Rules.md file by hand?

This file is generated by MetadataTests.testGenerateRulesDocumentation(), which is currently failing on this branch. You can run the test case and commit the result, then the test should pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I did it by myself. How is needed to be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, see in next thread. I'll change it.

Rules.md Outdated
<details>
<summary>Examples</summary>

```diff
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to put this example in Examples.swift, which is used when generating the Rules.md file

@NikKovIos NikKovIos closed this by deleting the head repository Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants