Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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