Skip to content

Commit

Permalink
Improve redundantType rule
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Aug 20, 2020
1 parent 745eceb commit 7067641
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
53 changes: 24 additions & 29 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -678,38 +678,33 @@ public struct _FormatRules {
formatter.forEachToken(where: { (token) -> Bool in
token == .keyword("var") || token == .keyword("let")
}) { i, _ in
func isExactSameType(startTypeIndex: Int, startInitIndex: Int) -> Bool {
var i = startTypeIndex
var j = startInitIndex
var shouldFormat = true

while let typeTokenIndex = formatter.index(of: .nonSpaceOrComment, after: i),
typeTokenIndex < startInitIndex - 1,
let initTokenIndex = formatter.index(of: .nonSpaceOrComment, after: j)
{
shouldFormat = shouldFormat
&& formatter.token(at: typeTokenIndex) == formatter.token(at: initTokenIndex)
i += 1
j += 1
}

return shouldFormat && i > startTypeIndex
}

guard
let colonIndex = formatter.index(of: .delimiter,
after: i,
if: { (token) -> Bool in token == .delimiter(":") }),
let operatorIndex = formatter.index(of: .operator,
after: colonIndex,
if: { (token) -> Bool in token == .operator("=", .infix) })
guard let colonIndex = formatter.index(after: i, where: {
[.delimiter(":"), .operator("=", .infix)].contains($0)
}), formatter.tokens[colonIndex] == .delimiter(":"),
let equalsIndex = formatter.index(of: .operator("=", .infix), after: colonIndex),
let endIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: equalsIndex)
else { return }

guard isExactSameType(startTypeIndex: colonIndex + 1,
startInitIndex: operatorIndex + 1) else { return }
// Check types match
var i = colonIndex, j = equalsIndex
while let typeIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i),
typeIndex <= endIndex,
let valueIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: j)
{
guard formatter.tokens[typeIndex] == formatter.tokens[valueIndex] else {
return
}
i = typeIndex
j = valueIndex
}
guard i == endIndex else {
return
}

formatter.removeTokens(in: colonIndex ... (operatorIndex - 1))
formatter.insertSpace(" ", at: colonIndex)
formatter.removeTokens(in: colonIndex ... endIndex)
if formatter.tokens[colonIndex - 1].isSpace {
formatter.removeToken(at: colonIndex - 1)
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,42 @@ class RulesTests: XCTestCase {
testFormatting(for: input, rule: FormatRules.redundantType)
}

func testRedundantTypeRemovedIfValueOnNextLine() {
let input = """
let view: UIView
= UIView()
"""
let output = """
let view
= UIView()
"""
testFormatting(for: input, output, rule: FormatRules.redundantType)
}

func testRedundantTypeRemovedIfValueOnNextLine2() {
let input = """
let view: UIView =
UIView()
"""
let output = """
let view =
UIView()
"""
testFormatting(for: input, output, rule: FormatRules.redundantType)
}

func testRedundantTypeRemovalWithComment() {
let input = "var view: UIView /* view */ = UIView()"
let output = "var view /* view */ = UIView()"
testFormatting(for: input, output, rule: FormatRules.redundantType)
}

func testRedundantTypeRemovalWithComment2() {
let input = "var view: UIView = /* view */ UIView()"
let output = "var view = /* view */ UIView()"
testFormatting(for: input, output, rule: FormatRules.redundantType)
}

// MARK: - consecutiveSpaces

func testConsecutiveSpaces() {
Expand Down
4 changes: 4 additions & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,10 @@ extension RulesTests {
("testRedundantSelfRuleDoesntErrorInForInTryLoop", testRedundantSelfRuleDoesntErrorInForInTryLoop),
("testRedundantSelfWithStaticMethodAfterForLoop", testRedundantSelfWithStaticMethodAfterForLoop),
("testRedundantSelfWithStaticMethodAfterForWhereLoop", testRedundantSelfWithStaticMethodAfterForWhereLoop),
("testRedundantTypeRemovalWithComment", testRedundantTypeRemovalWithComment),
("testRedundantTypeRemovalWithComment2", testRedundantTypeRemovalWithComment2),
("testRedundantTypeRemovedIfValueOnNextLine", testRedundantTypeRemovedIfValueOnNextLine),
("testRedundantTypeRemovedIfValueOnNextLine2", testRedundantTypeRemovedIfValueOnNextLine2),
("testRemoveBacktickCaseRawStringCases", testRemoveBacktickCaseRawStringCases),
("testRemoveBackticksAroundClassSelfArgument", testRemoveBackticksAroundClassSelfArgument),
("testRemoveBackticksAroundClassSelfAsParameterType", testRemoveBackticksAroundClassSelfAsParameterType),
Expand Down

0 comments on commit 7067641

Please sign in to comment.