Skip to content

Commit 66cac37

Browse files
dylansturgallevato
authored andcommitted
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 c52e0d2 commit 66cac37

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+13-4
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
739739
}
740740

741741
override func visit(_ node: ArrayExprSyntax) -> SyntaxVisitorContinueKind {
742-
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
743-
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
742+
if !node.elements.isEmpty || node.rightSquare.leadingTrivia.numberOfComments > 0 {
743+
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
744+
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
745+
}
744746
return .visitChildren
745747
}
746748

@@ -778,8 +780,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
778780
}
779781

780782
override func visit(_ node: DictionaryExprSyntax) -> SyntaxVisitorContinueKind {
781-
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
782-
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
783+
// The node's content is either a `DictionaryElementListSyntax` or a `TokenSyntax` for a colon
784+
// token (for an empty dictionary).
785+
if !(node.content.as(DictionaryElementListSyntax.self)?.isEmpty ?? true)
786+
|| node.content.leadingTrivia?.numberOfComments ?? 0 > 0
787+
|| node.rightSquare.leadingTrivia.numberOfComments > 0
788+
{
789+
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
790+
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
791+
}
783792
return .visitChildren
784793
}
785794

Tests/SwiftFormatPrettyPrintTests/ArrayDeclTests.swift

+11
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

+16
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)