Skip to content

Commit 7a2dadd

Browse files
committed
Fix parens orphaned by redundantInit rule
1 parent 58c2c65 commit 7a2dadd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Sources/Rules/RedundantInit.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public extension FormatRule {
6666
formatter.removeTokens(in: initIndex + 1 ..< openParenOrOpenBraceIndex)
6767
}
6868

69-
formatter.removeTokens(in: dotIndex ... initIndex)
69+
let endOfTypeIndex = (startOfTypeIndex ..< dotIndex).last(where: {
70+
formatter.tokens[$0].isNonSpaceOrCommentOrLinebreak
71+
}) ?? startOfTypeIndex
72+
73+
formatter.removeTokens(in: (endOfTypeIndex + 1) ... initIndex)
7074
}
7175
} examples: {
7276
"""

Tests/Rules/RedundantInitTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,27 @@ final class RedundantInitTests: XCTestCase {
256256
"""
257257
testFormatting(for: input, output, rule: .redundantInit)
258258
}
259+
260+
func testInitOnOwnLine() {
261+
let input = """
262+
let foo = String
263+
.init()
264+
"""
265+
let output = """
266+
let foo = String()
267+
"""
268+
testFormatting(for: input, output, rule: .redundantInit, exclude: [.propertyTypes])
269+
}
270+
271+
func testInitOnOwnLine2() {
272+
let input = """
273+
let foo = String /*
274+
comment
275+
*/ .init()
276+
"""
277+
let output = """
278+
let foo = String()
279+
"""
280+
testFormatting(for: input, output, rule: .redundantInit, exclude: [.propertyTypes])
281+
}
259282
}

0 commit comments

Comments
 (0)