Skip to content

Commit

Permalink
Merge pull request #798 from TTOzzi/fix-multiElementCollectionTrailin…
Browse files Browse the repository at this point in the history
…gCommas

Fix extraction of trailing comments only when necessary during afterToken addition
  • Loading branch information
allevato committed Aug 26, 2024
2 parents 7bca483 + 3f5d15a commit 4a3def9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2929,8 +2929,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
var shouldExtractTrailingComment = false
if wasLineComment && !hasAppendedTrailingComment {
switch afterToken {
case .break, .printerControl: shouldExtractTrailingComment = true
default: break
case let .break(kind, _, _):
if case let .close(mustBreak) = kind {
shouldExtractTrailingComment = mustBreak
} else {
shouldExtractTrailingComment = true
}
case .printerControl:
shouldExtractTrailingComment = true
default:
break
}
}
if shouldExtractTrailingComment {
Expand Down
96 changes: 96 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/CommaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,102 @@ final class CommaTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
}

func testArrayWithCommentCommasPresentEnabled() {
let input =
"""
let MyCollection = [
1,
2 // some comment
]
"""

let expected =
"""
let MyCollection = [
1,
2, // some comment
]
"""

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

func testArrayWithCommentCommasPresentDisabled() {
let input =
"""
let MyCollection = [
1,
2 // some comment
]
"""

let expected =
"""
let MyCollection = [
1,
2 // some comment
]
"""

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

func testArrayWithTernaryOperatorAndCommentCommasPresentEnabled() {
let input =
"""
let MyCollection = [
1,
true ? 1 : 2 // some comment
]
"""

let expected =
"""
let MyCollection = [
1,
true ? 1 : 2, // some comment
]
"""

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

func testArrayWithTernaryOperatorAndCommentCommasPresentDisabled() {
let input =
"""
let MyCollection = [
1,
true ? 1 : 2 // some comment
]
"""

let expected =
"""
let MyCollection = [
1,
true ? 1 : 2 // some comment
]
"""

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

func testDictionaryCommasAbsentEnabled() {
let input =
"""
Expand Down

0 comments on commit 4a3def9

Please sign in to comment.