Skip to content

Commit d9f90c7

Browse files
committed
Disallow line breaks in completely empty array and dict exprs.
There's no reason to have a newline between the square brackets when there's nothing, other than a `:` for dict exprs, between the brackets.
1 parent cd6ae7c commit d9f90c7

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
733733
}
734734

735735
override func visit(_ node: ArrayExprSyntax) -> SyntaxVisitorContinueKind {
736-
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
737-
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
736+
if !node.elements.isEmpty || node.rightSquare.leadingTrivia.numberOfComments > 0 {
737+
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
738+
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
739+
}
738740
return .visitChildren
739741
}
740742

@@ -772,8 +774,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
772774
}
773775

774776
override func visit(_ node: DictionaryExprSyntax) -> SyntaxVisitorContinueKind {
775-
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
776-
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
777+
// The node's content is either a `DictionaryElementListSyntax` or a `TokenSyntax` for a colon
778+
// token (for an empty dictionary).
779+
if !(node.content.as(DictionaryElementListSyntax.self)?.isEmpty ?? true)
780+
|| node.content.leadingTrivia?.numberOfComments ?? 0 > 0
781+
|| node.rightSquare.leadingTrivia.numberOfComments > 0
782+
{
783+
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
784+
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
785+
}
777786
return .visitChildren
778787
}
779788

Tests/SwiftFormatPrettyPrintTests/ArrayDeclTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ final class ArrayDeclTests: PrettyPrintTestCase {
55
func testBasicArrays() {
66
let input =
77
"""
8+
let a = [ ]
9+
let a = [
10+
]
11+
let a = [
12+
// Comment
13+
]
814
let a = [1, 2, 3,]
915
let a: [Bool] = [false, true, true, false]
1016
let a = [11111111, 2222222, 33333333, 4444444]
@@ -20,6 +26,11 @@ final class ArrayDeclTests: PrettyPrintTestCase {
2026

2127
let expected =
2228
"""
29+
let a = []
30+
let a = []
31+
let a = [
32+
// Comment
33+
]
2334
let a = [1, 2, 3]
2435
let a: [Bool] = [false, true, true, false]
2536
let a = [

Tests/SwiftFormatPrettyPrintTests/DictionaryDeclTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
55
func testBasicDictionaries() {
66
let input =
77
"""
8+
let a: [String: String] = [ : ]
9+
let a: [String: String] = [
10+
:
11+
]
12+
let a: [String: String] = [
13+
// Comment A
14+
:
15+
// Comment B
16+
]
817
let a = [1: "a", 2: "b", 3: "c",]
918
let a: [Int: String] = [1: "a", 2: "b", 3: "c"]
1019
let a = [10000: "abc", 20000: "def", 30000: "ghij"]
@@ -20,6 +29,13 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
2029

2130
let expected =
2231
"""
32+
let a: [String: String] = [:]
33+
let a: [String: String] = [:]
34+
let a: [String: String] = [
35+
// Comment A
36+
:
37+
// Comment B
38+
]
2339
let a = [1: "a", 2: "b", 3: "c"]
2440
let a: [Int: String] = [1: "a", 2: "b", 3: "c"]
2541
let a = [

0 commit comments

Comments
 (0)