Skip to content

Commit

Permalink
Fix void rule inside generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jul 29, 2019
1 parent b052575 commit 28087e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
25 changes: 16 additions & 9 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3276,16 +3276,23 @@ public struct _FormatRules {
formatter.insertToken(.startOfScope("("), at: i)
}
}
if formatter.options.useVoid {
formatter.forEach(.startOfScope("(")) { i, _ in
if formatter.last(.nonSpaceOrCommentOrLinebreak, before: i) == .operator("->", .infix),
let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: i, if: {
$0 == .endOfScope(")")
}), !isArgumentToken(at: nextIndex) {
// Replace with Void
formatter.replaceTokens(inRange: i ... nextIndex, with: [.identifier("Void")])
}
guard formatter.options.useVoid else {
return
}
formatter.forEach(.startOfScope("(")) { i, _ in
guard let endIndex = formatter.index(of: .nonSpaceOrLinebreak, after: i, if: {
$0 == .endOfScope(")")
}), let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: i),
!isArgumentToken(at: endIndex) else {
return
}
if formatter.last(.nonSpaceOrCommentOrLinebreak, before: i) == .operator("->", .infix) {
formatter.replaceTokens(inRange: i ... endIndex, with: [.identifier("Void")])
} else if prevToken == .startOfScope("<") ||
(prevToken == .delimiter(",") && formatter.currentScope(at: i) == .startOfScope("<")) {
formatter.replaceTokens(inRange: i ... endIndex, with: [.identifier("Void")])
}
// TODO: other cases
}
}

Expand Down
13 changes: 10 additions & 3 deletions Tests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3864,6 +3864,13 @@ class RulesTests: XCTestCase {
XCTAssertEqual(try format(input, rules: [FormatRules.void]), output)
}

func testEmptyParensInGenericsConvertedToVoid() {
let input = "Foo<(), ()>"
let output = "Foo<Void, Void>"
XCTAssertEqual(try format(input, rules: [FormatRules.void]), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n")
}

// useVoid = false

func testUseVoidOptionFalse() {
Expand Down Expand Up @@ -4701,21 +4708,21 @@ class RulesTests: XCTestCase {
let input = "let foo = Foo<Bar, (), ()>"
let output = input
XCTAssertEqual(try format(input, rules: [FormatRules.redundantParens]), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n")
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all(except: ["void"])), output + "\n")
}

func testParensNotRemovedAroundTupleGenerics() {
let input = "let foo = Foo<Bar, (Int, String), ()>"
let output = input
XCTAssertEqual(try format(input, rules: [FormatRules.redundantParens]), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n")
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all(except: ["void"])), output + "\n")
}

func testParensNotRemovedAroundLabeledTupleGenerics() {
let input = "let foo = Foo<Bar, (a: Int, b: String), ()>"
let output = input
XCTAssertEqual(try format(input, rules: [FormatRules.redundantParens]), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n")
XCTAssertEqual(try format(input + "\n", rules: FormatRules.all(except: ["void"])), output + "\n")
}

// after indexed tuple
Expand Down
1 change: 1 addition & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ extension RulesTests {
("testEmptyClosureArgsNotMangled", testEmptyClosureArgsNotMangled),
("testEmptyClosureArgsNotUnwrapped", testEmptyClosureArgsNotUnwrapped),
("testEmptyClosureReturnValueConvertedToVoid", testEmptyClosureReturnValueConvertedToVoid),
("testEmptyParensInGenericsConvertedToVoid", testEmptyParensInGenericsConvertedToVoid),
("testEmptyParensReturnValueConvertedToVoid", testEmptyParensReturnValueConvertedToVoid),
("testEnumCaseIndenting", testEnumCaseIndenting),
("testEnumCaseIndentingCommas", testEnumCaseIndentingCommas),
Expand Down

0 comments on commit 28087e7

Please sign in to comment.