Skip to content

Commit 84c51f0

Browse files
committed
Fix trailingCommas logic for init declarations
1 parent 9e302ed commit 84c51f0

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

Sources/ParsingHelpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3236,7 +3236,7 @@ extension Formatter {
32363236
/// `foo()`, `@foo()`, or `#foo()`
32373237
/// Exclude keywords to avoid confusing `return (...)`, `as? (...)`, `{ _ in (...) }`, etc.
32383238
let isFunctionIdentifier = { (token: Token) in
3239-
token.isIdentifier || token.isAttribute || (token.isKeyword && token.string.hasPrefix("#"))
3239+
token.isIdentifier || token.isAttribute || (token.isKeyword && token.string.hasPrefix("#") || token.string == "init")
32403240
}
32413241

32423242
if isFunctionIdentifier(tokens[previousToken]) {

Tests/Rules/TrailingClosuresTests.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ class TrailingClosuresTests: XCTestCase {
4141
testFormatting(for: input, rule: .trailingClosures)
4242
}
4343

44-
func testClosureArgumentInExpressionNotMadeTrailing() {
44+
func testClosureArgumentInIfStatementNotMadeTrailing() {
4545
let input = """
4646
if let foo = foo(foo: 5, { /* some code */ }) {}
4747
"""
4848
testFormatting(for: input, rule: .trailingClosures)
4949
}
5050

51-
func testClosureArgumentInCompoundExpressionNotMadeTrailing() {
51+
func testClosureArgumentInCompoundIfStatementNotMadeTrailing() {
5252
let input = """
5353
if let foo = foo(foo: 5, { /* some code */ }), let bar = bar(bar: 2, { /* some code */ }) {}
5454
"""
@@ -75,6 +75,23 @@ class TrailingClosuresTests: XCTestCase {
7575
testFormatting(for: input, output, rule: .trailingClosures)
7676
}
7777

78+
func testAnonymousInitClosureArgumentMadeTrailing() {
79+
let input = """
80+
Foo.init({ foo = bar })
81+
"""
82+
let output = """
83+
Foo.init { foo = bar }
84+
"""
85+
testFormatting(for: input, output, rule: .trailingClosures, exclude: [.redundantInit])
86+
}
87+
88+
func testNamedInitClosureArgumentNotMadeTrailing() {
89+
let input = """
90+
Foo.init(bar: { foo = bar })
91+
"""
92+
testFormatting(for: input, rule: .trailingClosures, exclude: [.redundantInit])
93+
}
94+
7895
func testNoRemoveParensAroundClosureFollowedByOpeningBrace() {
7996
let input = """
8097
foo({ bar }) { baz }

Tests/Rules/TrailingCommasTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,46 @@ class TrailingCommasTests: XCTestCase {
17981798
testFormatting(for: input, output, rule: .trailingCommas, options: options)
17991799
}
18001800

1801+
func testMultiElementListsRemovesCommaFromSingleElementInit() {
1802+
let input = """
1803+
public init(
1804+
a: Int,
1805+
) {
1806+
print(a)
1807+
}
1808+
"""
1809+
let output = """
1810+
public init(
1811+
a: Int
1812+
) {
1813+
print(a)
1814+
}
1815+
"""
1816+
let options = FormatOptions(trailingCommas: .multiElementLists, swiftVersion: "6.1")
1817+
testFormatting(for: input, output, rule: .trailingCommas, options: options)
1818+
}
1819+
1820+
func testMultiElementListsAddCommaToInit() {
1821+
let input = """
1822+
public init(
1823+
a: Int,
1824+
b: Int
1825+
) {
1826+
print(a, b)
1827+
}
1828+
"""
1829+
let output = """
1830+
public init(
1831+
a: Int,
1832+
b: Int,
1833+
) {
1834+
print(a, b)
1835+
}
1836+
"""
1837+
let options = FormatOptions(trailingCommas: .multiElementLists, swiftVersion: "6.1")
1838+
testFormatting(for: input, output, rule: .trailingCommas, options: options)
1839+
}
1840+
18011841
func testTrailingCommasNotRemovedFromInitParametersWithAlwaysOption() {
18021842
let input = """
18031843
public init(
@@ -1810,6 +1850,25 @@ class TrailingCommasTests: XCTestCase {
18101850
testFormatting(for: input, rule: .trailingCommas, options: options, exclude: [.unusedArguments])
18111851
}
18121852

1853+
func testTrailingCommasAddedToInitParametersWithAlwaysOption() {
1854+
let input = """
1855+
public init(
1856+
parameter: Parameter
1857+
) {
1858+
// test
1859+
}
1860+
"""
1861+
let output = """
1862+
public init(
1863+
parameter: Parameter,
1864+
) {
1865+
// test
1866+
}
1867+
"""
1868+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
1869+
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.unusedArguments])
1870+
}
1871+
18131872
func testTrailingCommaNotRemovedFromTupleTypeSwift6_1() {
18141873
let input = """
18151874
let foo: (

0 commit comments

Comments
 (0)