Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added two tests, fixed rule for operator. Corrected other tests.
  • Loading branch information
NikKovIos committed Aug 1, 2024
commit 441affbaeab3132d7549c4d126882401ac062947
23 changes: 23 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
* [propertyType](#propertyType)
* [redundantProperty](#redundantProperty)
* [sortSwitchCases](#sortSwitchCases)
* [spacingGuards](#spacingGuards)
* [unusedPrivateDeclaration](#unusedPrivateDeclaration)
* [wrapConditionalBodies](#wrapConditionalBodies)
* [wrapEnumCases](#wrapEnumCases)
Expand Down Expand Up @@ -2585,6 +2586,28 @@ Remove space inside parentheses.
</details>
<br/>

## spacingGuards

Remove space between guard and add spaces after last guard.

<details>
<summary>Examples</summary>

```diff
guard let spicy = self.makeSpicy() else {
return
}
-
guard let soap = self.clean() else {
return
}
+
let doTheJob = nikekov()
```

</details>
<br/>

## specifiers

Use consistent ordering for member modifiers.
Expand Down
8 changes: 5 additions & 3 deletions Sources/Rules/SpacingGuards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public extension FormatRule {

let nextNonSpaceAndNonLinebreakToken = formatter.token(at: nextNonSpaceAndNonLinebreakIndex)

if nextNonSpaceAndNonLinebreakToken == .endOfScope("}") {
// Do not add space for end bracket
if nextNonSpaceAndNonLinebreakToken == .endOfScope("}")
|| nextNonSpaceAndNonLinebreakToken?.isOperator == true
{
// Do not add space in this cases
return
}

let isGuard = nextNonSpaceAndNonLinebreakToken == .keyword("guard")
let indexesBetween = Set(endOfScopeOfGuard + 1 ... nextNonSpaceAndNonLinebreakIndex - 1)
let indexesBetween = Set(endOfScopeOfGuard + 1 ..< nextNonSpaceAndNonLinebreakIndex)
formatter.leaveOrSetLinebreaksInIndexes(indexesBetween, linebreaksCount: isGuard ? 1 : 2)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/ElseOnSameLineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,6 @@ class ElseOnSameLineTests: XCTestCase {
"""

let options = FormatOptions(elseOnNextLine: false, guardElsePosition: .nextLine)
testFormatting(for: input, output, rule: .elseOnSameLine, options: options)
testFormatting(for: input, output, rule: .elseOnSameLine, options: options, exclude: [.spacingGuards])
}
}
2 changes: 1 addition & 1 deletion Tests/Rules/HoistPatternLetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class HoistPatternLetTests: XCTestCase {
"""
let options = FormatOptions(hoistPatternLet: false)
testFormatting(for: input, rule: .hoistPatternLet, options: options,
exclude: [.wrapConditionalBodies])
exclude: [.wrapConditionalBodies, .spacingGuards])
}

func testNoUnhoistSwitchCaseLetFollowedByWhere() {
Expand Down
14 changes: 7 additions & 7 deletions Tests/Rules/IndentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ class IndentTests: XCTestCase {
"""
let options = FormatOptions(wrapArguments: .disabled, closingParenPosition: .balanced)
testFormatting(for: input, rule: .indent, options: options,
exclude: [.wrapConditionalBodies])
exclude: [.wrapConditionalBodies, .spacingGuards])
}

func testSingleIndentTrailingClosureBody2() {
Expand All @@ -1540,7 +1540,7 @@ class IndentTests: XCTestCase {
"""
let options = FormatOptions(wrapArguments: .disabled, closingParenPosition: .sameLine)
testFormatting(for: input, rule: .indent, options: options,
exclude: [.wrapConditionalBodies, .wrapMultilineStatementBraces])
exclude: [.wrapConditionalBodies, .wrapMultilineStatementBraces, .spacingGuards])
}

func testDoubleIndentTrailingClosureBody() {
Expand All @@ -1556,7 +1556,7 @@ class IndentTests: XCTestCase {
"""
let options = FormatOptions(wrapArguments: .disabled, closingParenPosition: .sameLine)
testFormatting(for: input, rule: .indent, options: options,
exclude: [.wrapConditionalBodies, .wrapMultilineStatementBraces])
exclude: [.wrapConditionalBodies, .wrapMultilineStatementBraces, .spacingGuards])
}

func testDoubleIndentTrailingClosureBody2() {
Expand Down Expand Up @@ -1601,7 +1601,7 @@ class IndentTests: XCTestCase {
"""
let options = FormatOptions(wrapArguments: .disabled, closingParenPosition: .sameLine)
testFormatting(for: input, rule: .indent, options: options,
exclude: [.braces, .wrapConditionalBodies])
exclude: [.braces, .wrapConditionalBodies, .spacingGuards])
}

func testSingleIndentTrailingClosureBodyOfShortMethod() {
Expand All @@ -1613,7 +1613,7 @@ class IndentTests: XCTestCase {
"""
let options = FormatOptions(wrapArguments: .disabled, closingParenPosition: .sameLine)
testFormatting(for: input, rule: .indent, options: options,
exclude: [.wrapConditionalBodies])
exclude: [.wrapConditionalBodies, .spacingGuards])
}

func testNoDoubleIndentInInsideClosure() {
Expand Down Expand Up @@ -1975,7 +1975,7 @@ class IndentTests: XCTestCase {
"""
let options = FormatOptions(xcodeIndentation: true)
testFormatting(for: input, output, rule: .indent,
options: options, exclude: [.wrapConditionalBodies])
options: options, exclude: [.wrapConditionalBodies, .spacingGuards])
}

func testWrappedChainedFunctionsWithNestedScopeIndent() {
Expand Down Expand Up @@ -3717,7 +3717,7 @@ class IndentTests: XCTestCase {
"""
let options = FormatOptions(indent: "\t", truncateBlankLines: false, tabWidth: 2)
testFormatting(for: input, rule: .indent, options: options,
exclude: [.consecutiveBlankLines, .wrapConditionalBodies])
exclude: [.consecutiveBlankLines, .wrapConditionalBodies, .spacingGuards])
}

// async
Expand Down
23 changes: 12 additions & 11 deletions Tests/Rules/RedundantSelfTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class RedundantSelfTests: XCTestCase {
func testNoRemoveSelfForVarCreatedInGuardScope() {
let input = "func foo() {\n guard let bar = 5 else {}\n let baz = self.bar\n}"
testFormatting(for: input, rule: .redundantSelf,
exclude: [.wrapConditionalBodies])
exclude: [.wrapConditionalBodies, .spacingGuards])
}

func testRemoveSelfForVarCreatedInIfScope() {
Expand Down Expand Up @@ -1035,7 +1035,7 @@ class RedundantSelfTests: XCTestCase {
"""
let options = FormatOptions(swiftVersion: "5.8")
testFormatting(for: input, output, rule: .redundantSelf,
options: options, exclude: [.redundantOptionalBinding])
options: options, exclude: [.redundantOptionalBinding, .spacingGuards])
}

func testWeakSelfNotRemovedIfNotUnwrapped() {
Expand Down Expand Up @@ -1124,7 +1124,7 @@ class RedundantSelfTests: XCTestCase {
}
"""
let options = FormatOptions(swiftVersion: "5.7")
testFormatting(for: input, rule: .redundantSelf, options: options)
testFormatting(for: input, rule: .redundantSelf, options: options, exclude: [.spacingGuards])
}

func testNonRedundantSelfNotRemovedAfterConditionalLet() {
Expand Down Expand Up @@ -1199,7 +1199,7 @@ class RedundantSelfTests: XCTestCase {
self.bar()
}
"""
testFormatting(for: input, rule: .redundantSelf)
testFormatting(for: input, rule: .redundantSelf, exclude: [.spacingGuards])
}

func testNoRemoveSelfInClosureInIfCondition() {
Expand Down Expand Up @@ -1343,7 +1343,7 @@ class RedundantSelfTests: XCTestCase {
}) {}
"""
testFormatting(for: input, rule: .redundantSelf,
exclude: [.wrapConditionalBodies])
exclude: [.wrapConditionalBodies, .spacingGuards])
}

func testStructSelfRemovedInTrailingClosureInIfCase() {
Expand Down Expand Up @@ -1488,7 +1488,7 @@ class RedundantSelfTests: XCTestCase {
}
"""
testFormatting(for: input, rule: .redundantSelf,
exclude: [.wrapConditionalBodies])
exclude: [.wrapConditionalBodies, .spacingGuards])
}

func testNoRemoveSelfInAssignmentInsideIfAsStatement() {
Expand Down Expand Up @@ -1536,7 +1536,7 @@ class RedundantSelfTests: XCTestCase {
}
"""
testFormatting(for: input, output, rule: .redundantSelf,
exclude: [.hoistPatternLet])
exclude: [.hoistPatternLet, .spacingGuards])
}

func testRedundantSelfParsingBug2() {
Expand Down Expand Up @@ -1742,7 +1742,7 @@ class RedundantSelfTests: XCTestCase {
}
"""
let options = FormatOptions(swiftVersion: "5.0")
testFormatting(for: input, output, rule: .redundantSelf, options: options)
testFormatting(for: input, output, rule: .redundantSelf, options: options, exclude: [.spacingGuards])
}

func testShadowedSelfRemovedInGuardLet() {
Expand All @@ -1762,7 +1762,7 @@ class RedundantSelfTests: XCTestCase {
print(optional)
}
"""
testFormatting(for: input, output, rule: .redundantSelf)
testFormatting(for: input, output, rule: .redundantSelf, exclude: [.spacingGuards])
}

func testShadowedStringValueNotRemovedInInit() {
Expand Down Expand Up @@ -2799,7 +2799,7 @@ class RedundantSelfTests: XCTestCase {
}
}
"""
testFormatting(for: input, rule: .redundantSelf)
testFormatting(for: input, rule: .redundantSelf, exclude: [.spacingGuards])
}

func testSelfRemovalParsingBug2() {
Expand Down Expand Up @@ -2889,7 +2889,7 @@ class RedundantSelfTests: XCTestCase {
}
}
"""
testFormatting(for: input, rule: .redundantSelf)
testFormatting(for: input, rule: .redundantSelf, exclude: [.spacingGuards])
}

func testSelfNotRemovedInCaseIfElse() {
Expand Down Expand Up @@ -2980,6 +2980,7 @@ class RedundantSelfTests: XCTestCase {
print(self.bar)
return
}

print(self.bar)
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/Rules/RedundantStaticSelfTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class RedundantStaticSelfTests: XCTestCase {
guard let value = Self.location(for: warnRegion) else {
return nil
}

self.init(location: value)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/SortSwitchCasesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SortSwitchCasesTests: XCTestCase {
}
"""

testFormatting(for: input, rule: .sortSwitchCases, exclude: [.redundantSelf])
testFormatting(for: input, rule: .sortSwitchCases, exclude: [.redundantSelf, .spacingGuards])
}

func testSortedSwitchCaseMultilineWithOneComment() {
Expand Down
25 changes: 25 additions & 0 deletions Tests/Rules/SpacingGuardsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,29 @@ final class GuardSpacingTests: XCTestCase {

testFormatting(for: input, output, rule: .spacingGuards, exclude: [.docComments])
}

func testNotInsertLineBreakWhenInlineFunction() {
let input = """
let array = [1, 2, 3]
guard array.map { String($0) }.isEmpty else {
return
}
"""
testFormatting(for: input, rule: .spacingGuards, exclude: [.wrapConditionalBodies])
}

func testNotInsertLineBreakInChain() {
let input = """
guard aBool,
anotherBool,
aTestArray
.map { $0 * 2 }
.filter { $0 == 4 }
.isEmpty,
yetAnotherBool
else { return }
"""

testFormatting(for: input, rule: .spacingGuards, exclude: [.wrapConditionalBodies])
}
}
3 changes: 3 additions & 0 deletions Tests/Rules/UnusedArgumentsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ class UnusedArgumentsTests: XCTestCase {
guard num > 0, locations.count >= count else {
return
}

print(locations)
}
"""
Expand Down Expand Up @@ -948,6 +949,7 @@ class UnusedArgumentsTests: XCTestCase {
else {
return nil
}

return History(firstParameter, secondParameter)
}
"""
Expand Down Expand Up @@ -1096,6 +1098,7 @@ class UnusedArgumentsTests: XCTestCase {
guard let update, error == nil else {
return
}

self?.configure(update)
}
"""
Expand Down
24 changes: 11 additions & 13 deletions Tests/Rules/WrapArgumentsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@ class WrapArgumentsTests: XCTestCase {
testFormatting(
for: input, rule: .wrapArguments,
options: FormatOptions(indent: " ", wrapConditions: .beforeFirst),
exclude: [.elseOnSameLine, .wrapConditionalBodies]
exclude: [.elseOnSameLine, .wrapConditionalBodies, .spacingGuards]
)
}

Expand Down Expand Up @@ -2246,12 +2246,11 @@ class WrapArgumentsTests: XCTestCase {
else {}
"""

testFormatting(
for: input,
[output],
rules: [.wrapArguments],
options: FormatOptions(indent: " ", conditionsWrap: .auto, maxWidth: 40)
)
testFormatting(for: input,
[output],
rules: [.wrapArguments],
options: FormatOptions(indent: " ", conditionsWrap: .auto, maxWidth: 40),
exclude: [.spacingGuards])
}

func testConditionsWrapAutoOptionForGuardWhenElseOnNewLine() {
Expand Down Expand Up @@ -2297,12 +2296,11 @@ class WrapArgumentsTests: XCTestCase {
else {}
"""

testFormatting(
for: input,
[output],
rules: [.wrapArguments],
options: FormatOptions(indent: " ", conditionsWrap: .auto, maxWidth: 40)
)
testFormatting(for: input,
[output],
rules: [.wrapArguments],
options: FormatOptions(indent: " ", conditionsWrap: .auto, maxWidth: 40),
exclude: [.spacingGuards])
}

func testConditionsWrapAutoOptionForGuardInMethod() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/WrapConditionalBodiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class WrapConditionalBodiesTests: XCTestCase {
return true
}
"""
testFormatting(for: input, output, rule: .wrapConditionalBodies)
testFormatting(for: input, output, rule: .wrapConditionalBodies, exclude: [.spacingGuards])
}

func testIfElseReturnsWrap() {
Expand Down