-
Notifications
You must be signed in to change notification settings - Fork 665
Draft: [New rule] Space with guard #1801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Sources/Rules.swift
Outdated
| } | ||
| } | ||
|
|
||
| public let spacesWithGuard = FormatRule(help: "Remove space between guard and add spaces after last guard.", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
// GuardSpacingTests.swift
// GuardSpacingTests
//
Tests/RulesTests+GuardSpacing.swift
Outdated
| import XCTest | ||
| @testable import SwiftFormat | ||
|
|
||
| class GuardSpacingTests: RulesTests { |
There was a problem hiding this comment.
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
| class GuardSpacingTests: RulesTests { | |
| class SpaceWithGuardTests: RulesTests { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed
Sources/Rules.swift
Outdated
| 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved
Sources/Rules.swift
Outdated
| let guardKeyword = Token.keyword("guard") | ||
| formatter.forEach(guardKeyword) { guardIndex, _ in |
There was a problem hiding this comment.
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:
| let guardKeyword = Token.keyword("guard") | |
| formatter.forEach(guardKeyword) { guardIndex, _ in | |
| formatter.forEach(.keyword("guard")) { guardIndex, _ in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
Sources/Rules.swift
Outdated
|
|
||
| let guardKeyword = Token.keyword("guard") | ||
| formatter.forEach(guardKeyword) { guardIndex, _ in | ||
| print("✳️ start \(guardIndex)") |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleaned
Sources/Rules.swift
Outdated
| switch nextNonSpaceAndNonLinebreakToken { | ||
| case let .endOfScope(string): | ||
| if string == "}" { | ||
| // Do not add space for end bracket | ||
| return | ||
| } | ||
| default: | ||
| break | ||
| } |
There was a problem hiding this comment.
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:
| 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 | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Sources/Rules.swift
Outdated
| if alreadyHasLinebreaksCount != linebreaksCount, | ||
| let firstIndex = indexes.first | ||
| { | ||
| formatter.insert(.linebreak("\n", 0), at: firstIndex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be:
| formatter.insert(.linebreak("\n", 0), at: firstIndex) | |
| formatter.insertLinebreak(at: firstIndex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
calda
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
Blocked by #1800