Skip to content

Commit 82a24fe

Browse files
committed
Add additional trailingCommas tests
1 parent 84c51f0 commit 82a24fe

File tree

2 files changed

+107
-30
lines changed

2 files changed

+107
-30
lines changed

Sources/Rules/TrailingCommas.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public extension FormatRule {
6767
}
6868

6969
// In Swift 6.1, trailing commas are also supported in tuple values,
70-
// but not tuple types: https://github.com/swiftlang/swift/issues/81485
70+
// but not tuple or closure types: https://github.com/swiftlang/swift/issues/81485
7171
// If we know this is a tuple value, then trailing commas are supported.
7272
if formatter.options.swiftVersion >= "6.1",
7373
let tokenBeforeStartOfScope = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: startOfScope)

Tests/Rules/TrailingCommasTests.swift

Lines changed: 106 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,37 @@ class TrailingCommasTests: XCTestCase {
16421642
testFormatting(for: input, output, rule: .trailingCommas, options: options)
16431643
}
16441644

1645+
func testTrailingCommasNotRemovedFromInitParametersWithAlwaysOption() {
1646+
let input = """
1647+
public init(
1648+
parameter: Parameter,
1649+
) {
1650+
// test
1651+
}
1652+
"""
1653+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
1654+
testFormatting(for: input, rule: .trailingCommas, options: options, exclude: [.unusedArguments])
1655+
}
1656+
1657+
func testTrailingCommasAddedToInitParametersWithAlwaysOption() {
1658+
let input = """
1659+
public init(
1660+
parameter: Parameter
1661+
) {
1662+
// test
1663+
}
1664+
"""
1665+
let output = """
1666+
public init(
1667+
parameter: Parameter,
1668+
) {
1669+
// test
1670+
}
1671+
"""
1672+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
1673+
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.unusedArguments])
1674+
}
1675+
16451676
// MARK: - Multi-element lists tests
16461677

16471678
func testMultiElementListsAddsCommaToMultiElementArray() {
@@ -1838,47 +1869,40 @@ class TrailingCommasTests: XCTestCase {
18381869
testFormatting(for: input, output, rule: .trailingCommas, options: options)
18391870
}
18401871

1841-
func testTrailingCommasNotRemovedFromInitParametersWithAlwaysOption() {
1872+
func testTrailingCommaNotRemovedFromTupleAndClosureTypesSwift6_1() {
18421873
let input = """
1843-
public init(
1844-
parameter: Parameter,
1845-
) {
1846-
// test
1847-
}
1848-
"""
1849-
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
1850-
testFormatting(for: input, rule: .trailingCommas, options: options, exclude: [.unusedArguments])
1851-
}
1874+
let foo: (
1875+
bar: String,
1876+
quux: String,
1877+
)
18521878
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
1879+
let bar: (
1880+
bar: String,
1881+
baaz: String,
1882+
) -> Void
1883+
1884+
public func testClosureArgumentInTuple() {
1885+
_ = object.methodWithTupleArgument((
1886+
closureArgument: { capturedObject in
1887+
_ = capturedObject
1888+
},
1889+
))
18661890
}
18671891
"""
18681892
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
1869-
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.unusedArguments])
1893+
testFormatting(for: input, rule: .trailingCommas, options: options)
18701894
}
18711895

1872-
func testTrailingCommaNotRemovedFromTupleTypeSwift6_1() {
1896+
func testTrailingCommaNotAddedToTupleAndClosureTypesSwift6_1() {
18731897
let input = """
18741898
let foo: (
18751899
bar: String,
1876-
quux: String,
1900+
quux: String
18771901
)
18781902
18791903
let bar: (
18801904
bar: String,
1881-
baaz: String,
1905+
baaz: String
18821906
) -> Void
18831907
18841908
public func testClosureArgumentInTuple() {
@@ -1893,7 +1917,7 @@ class TrailingCommasTests: XCTestCase {
18931917
testFormatting(for: input, rule: .trailingCommas, options: options)
18941918
}
18951919

1896-
func testTrailingCommaNotRemovedFromClosureTypeSwift6_1_mutliElementLists() {
1920+
func testMultiElementListsTrailingCommaNotRemovedFromClosureTypeSwift6_1() {
18971921
let input = """
18981922
let foo: (
18991923
bar: String,
@@ -1904,7 +1928,6 @@ class TrailingCommasTests: XCTestCase {
19041928
baaz: String,
19051929
) -> Void
19061930
"""
1907-
19081931
let output = """
19091932
let foo: (
19101933
bar: String
@@ -1919,6 +1942,22 @@ class TrailingCommasTests: XCTestCase {
19191942
testFormatting(for: input, output, rule: .trailingCommas, options: options)
19201943
}
19211944

1945+
func testMultiElementListsTrailingCommaNotAddedToTupleAndClosureTypesSwift6_1() {
1946+
let input = """
1947+
let bar: (
1948+
bar: String,
1949+
baaz: String
1950+
)
1951+
1952+
let bar: (
1953+
bar: String,
1954+
baaz: String
1955+
) -> Void
1956+
"""
1957+
let options = FormatOptions(trailingCommas: .multiElementLists, swiftVersion: "6.1")
1958+
testFormatting(for: input, rule: .trailingCommas, options: options)
1959+
}
1960+
19221961
func testTrailingCommasAddedToOptionalClosureCall() {
19231962
let input = """
19241963
myClosure?(
@@ -2004,4 +2043,42 @@ class TrailingCommasTests: XCTestCase {
20042043
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
20052044
testFormatting(for: input, rule: .trailingCommas, options: options)
20062045
}
2046+
2047+
func testIssue2142() {
2048+
let input = """
2049+
public func bindExitButton<T: Presenter>(
2050+
action: T.Action,
2051+
withIdentifier identifier: UIAction.Identifier? = nil,
2052+
on controlEvents: UIControl.Event = .primaryActionTriggered,
2053+
to presenter: T,
2054+
) {
2055+
_ = action
2056+
_ = identifier
2057+
_ = controlEvents
2058+
_ = presenter
2059+
}
2060+
2061+
let setModeSwizzle = Swizzle<AVAudioSession>(
2062+
instance: instance,
2063+
original: #selector(AVAudioSession.setMode(_:)),
2064+
swizzled: #selector(AVAudioSession.swizzled_setMode(_:)),
2065+
)
2066+
"""
2067+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
2068+
testFormatting(for: input, rule: .trailingCommas, options: options, exclude: [.propertyTypes])
2069+
}
2070+
2071+
func testIssue2143() {
2072+
let input = """
2073+
public func testClosureArgumentInTuple() {
2074+
_ = object.methodWithTupleArgument((
2075+
closureArgument: { capturedObject in
2076+
_ = capturedObject
2077+
},
2078+
))
2079+
}
2080+
"""
2081+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
2082+
testFormatting(for: input, rule: .trailingCommas, options: options)
2083+
}
20072084
}

0 commit comments

Comments
 (0)