Skip to content

Add option to disable trailing commas on multi-line collections #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Documentation/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ top-level keys and values:
* `spacesAroundRangeFormationOperators` _(boolean)_: Determines whether whitespace should be forced
before and after the range formation operators `...` and `..<`.

* `multilineCollectionTrailingCommas` _(boolean)_: Determines whether multi-line collections should have trailing commas.
Defaults to `true`.

> TODO: Add support for enabling/disabling specific syntax transformations in
> the pipeline.

Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftFormat/API/Configuration+Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ extension Configuration {
self.indentSwitchCaseLabels = false
self.spacesAroundRangeFormationOperators = false
self.noAssignmentInExpressions = NoAssignmentInExpressionsConfiguration()
self.multilineCollectionTrailingCommas = true
}
}
9 changes: 9 additions & 0 deletions Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public struct Configuration: Codable, Equatable {
case rules
case spacesAroundRangeFormationOperators
case noAssignmentInExpressions
case multilineCollectionTrailingCommas
}

/// A dictionary containing the default enabled/disabled states of rules, keyed by the rules'
Expand Down Expand Up @@ -162,6 +163,9 @@ public struct Configuration: Codable, Equatable {
/// Contains exceptions for the `NoAssignmentInExpressions` rule.
public var noAssignmentInExpressions: NoAssignmentInExpressionsConfiguration

/// Determines whether multi-line list initializers should have trailing commas
public var multilineCollectionTrailingCommas: Bool

/// Constructs a Configuration by loading it from a configuration file.
public init(contentsOf url: URL) throws {
let data = try Data(contentsOf: url)
Expand Down Expand Up @@ -239,6 +243,10 @@ public struct Configuration: Codable, Equatable {
try container.decodeIfPresent(
NoAssignmentInExpressionsConfiguration.self, forKey: .noAssignmentInExpressions)
?? defaults.noAssignmentInExpressions
self.multilineCollectionTrailingCommas =
try container.decodeIfPresent(
Bool.self, forKey: .multilineCollectionTrailingCommas)
?? defaults.multilineCollectionTrailingCommas

// If the `rules` key is not present at all, default it to the built-in set
// so that the behavior is the same as if the configuration had been
Expand Down Expand Up @@ -271,6 +279,7 @@ public struct Configuration: Codable, Equatable {
try container.encode(fileScopedDeclarationPrivacy, forKey: .fileScopedDeclarationPrivacy)
try container.encode(indentSwitchCaseLabels, forKey: .indentSwitchCaseLabels)
try container.encode(noAssignmentInExpressions, forKey: .noAssignmentInExpressions)
try container.encode(multilineCollectionTrailingCommas, forKey: .multilineCollectionTrailingCommas)
try container.encode(rules, forKey: .rules)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public class PrettyPrinter {
// We never want to add a trailing comma in an initializer so we disable trailing commas on
// single element collections.
let shouldHaveTrailingComma =
startLineNumber != openCloseBreakCompensatingLineNumber && !isSingleElement
startLineNumber != openCloseBreakCompensatingLineNumber && !isSingleElement && configuration.multilineCollectionTrailingCommas
if shouldHaveTrailingComma && !hasTrailingComma {
diagnose(.addTrailingComma, category: .trailingComma)
} else if !shouldHaveTrailingComma && hasTrailingComma {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extension Configuration {
config.indentSwitchCaseLabels = false
config.spacesAroundRangeFormationOperators = false
config.noAssignmentInExpressions = NoAssignmentInExpressionsConfiguration()
config.multilineCollectionTrailingCommas = true
return config
}
}
145 changes: 145 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/CommaTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import SwiftFormat

final class CommaTests: PrettyPrintTestCase {
func testCommasAbsentEnabled() {
let input =
"""
let MyList = [
1,
2,
3
]

"""

let expected =
"""
let MyList = [
1,
2,
3,
]

"""

var configuration = Configuration.forTesting
configuration.multilineCollectionTrailingCommas = true
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
}

func testCommasAbsentDisabled() {
let input =
"""
let MyList = [
1,
2,
3
]

"""

let expected =
"""
let MyList = [
1,
2,
3
]

"""

var configuration = Configuration.forTesting
configuration.multilineCollectionTrailingCommas = false
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
}

func testCommasPresentEnabled() {
let input =
"""
let MyList = [
1,
2,
3,
]

"""

let expected =
"""
let MyList = [
1,
2,
3,
]

"""

var configuration = Configuration.forTesting
configuration.multilineCollectionTrailingCommas = true
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
}

func testCommasPresentDisabled() {
let input =
"""
let MyList = [
1,
2,
3,
]

"""

let expected =
"""
let MyList = [
1,
2,
3
]

"""

var configuration = Configuration.forTesting
configuration.multilineCollectionTrailingCommas = false
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
}

func testCommasPresentSingleLineDisabled() {
let input =
"""
let MyList = [1, 2, 3,]

"""

// no effect expected
let expected =
"""
let MyList = [1, 2, 3]

"""

var configuration = Configuration.forTesting
configuration.multilineCollectionTrailingCommas = true
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
}

func testCommasPresentSingleLineEnabled() {
let input =
"""
let MyList = [1, 2, 3,]

"""

// no effect expected
let expected =
"""
let MyList = [1, 2, 3]

"""

var configuration = Configuration.forTesting
configuration.multilineCollectionTrailingCommas = false
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
}
}