Skip to content

Commit

Permalink
Fix allman inference
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 9, 2020
1 parent f1c2a8e commit 7f07e1e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/Inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ private struct Inference {
else {
return
}
// Ignore wrapped if/else/guard
if let keyword = formatter.lastSignificantKeyword(at: i - 1, excluding: ["else"]),
["if", "guard", "while", "let", "var", "case"].contains(keyword)
{
return
}
// Check if brace is wrapped
if let prevTokenIndex = formatter.index(of: .nonSpace, before: i),
let prevToken = formatter.token(at: prevTokenIndex)
Expand Down
39 changes: 39 additions & 0 deletions Tests/RulesTests+Wrapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@ extension RulesTests {
testFormatting(for: input, rule: FormatRules.elseOnSameLine)
}

func testElseOnSameLineInferenceEdgeCase() {
let input = """
func foo() {
if let foo == bar {
// ...
} else {
// ...
}
if let foo == bar,
let baz = quux
{
print()
}
if let foo == bar,
let baz = quux
{
print()
}
if let foo == bar,
let baz = quux
{
print()
}
if let foo == bar,
let baz = quux
{
print()
}
}
"""
let options = FormatOptions(elseOnNextLine: false)
testFormatting(for: input, rule: FormatRules.elseOnSameLine, options: options,
exclude: ["braces"])
}

// guardelse = auto

func testSingleLineGuardElseNotWrappedByDefault() {
Expand Down
1 change: 1 addition & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ extension RulesTests {
("testElseOnNextLineOption", testElseOnNextLineOption),
("testElseOnSameLine", testElseOnSameLine),
("testElseOnSameLineDoesntEatPreviousStatement", testElseOnSameLineDoesntEatPreviousStatement),
("testElseOnSameLineInferenceEdgeCase", testElseOnSameLineInferenceEdgeCase),
("testElseOnSameLineOnlyAppliedToDanglingBrace", testElseOnSameLineOnlyAppliedToDanglingBrace),
("testEmptyBracesNotRemovedInDoCatch", testEmptyBracesNotRemovedInDoCatch),
("testEmptyBracesNotRemovedInIfElse", testEmptyBracesNotRemovedInIfElse),
Expand Down

0 comments on commit 7f07e1e

Please sign in to comment.