Skip to content

Commit 1443953

Browse files
committed
Remove requiresLeadingNewline from basic format
1 parent d3ce991 commit 1443953

File tree

19 files changed

+56
-166
lines changed

19 files changed

+56
-166
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public class Child {
6262
public let description: String?
6363
public let forceClassification: Bool
6464
public let isIndented: Bool
65-
public let requiresLeadingNewline: Bool
6665
public let isOptional: Bool
6766
public let classification: SyntaxClassification?
6867

@@ -158,8 +157,7 @@ public class Child {
158157
isOptional: Bool = false,
159158
classification: String? = nil,
160159
forceClassification: Bool = false,
161-
isIndented: Bool = false,
162-
requiresLeadingNewline: Bool = false
160+
isIndented: Bool = false
163161
) {
164162
if let firstCharInName = name.first {
165163
precondition(firstCharInName.isUppercase == true, "The first letter of a child’s name should be uppercase")
@@ -171,7 +169,6 @@ public class Child {
171169
self.classification = classificationByName(classification)
172170
self.forceClassification = forceClassification
173171
self.isIndented = isIndented
174-
self.requiresLeadingNewline = requiresLeadingNewline
175172
self.isOptional = isOptional
176173
}
177174
}

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public let COMMON_NODES: [Node] = [
7777
),
7878
Child(
7979
name: "RightBrace",
80-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
81-
requiresLeadingNewline: true
80+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
8281
),
8382
]
8483
),

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public let DECL_NODES: [Node] = [
5555
),
5656
Child(
5757
name: "RightBrace",
58-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
59-
requiresLeadingNewline: true
58+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
6059
),
6160
]
6261
),
@@ -1001,8 +1000,7 @@ public let DECL_NODES: [Node] = [
10011000
Child(
10021001
name: "PoundKeyword",
10031002
kind: .token(choices: [.token(tokenKind: "PoundIfToken"), .token(tokenKind: "PoundElseifToken"), .token(tokenKind: "PoundElseToken")]),
1004-
classification: "BuildConfigId",
1005-
requiresLeadingNewline: true
1003+
classification: "BuildConfigId"
10061004
),
10071005
Child(
10081006
name: "Condition",
@@ -1054,8 +1052,7 @@ public let DECL_NODES: [Node] = [
10541052
Child(
10551053
name: "PoundEndif",
10561054
kind: .token(choices: [.token(tokenKind: "PoundEndifToken")]),
1057-
classification: "BuildConfigId",
1058-
requiresLeadingNewline: true
1055+
classification: "BuildConfigId"
10591056
),
10601057
]
10611058
),
@@ -1366,8 +1363,7 @@ public let DECL_NODES: [Node] = [
13661363
),
13671364
Child(
13681365
name: "RightBrace",
1369-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
1370-
requiresLeadingNewline: true
1366+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
13711367
),
13721368
]
13731369
),

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,7 @@ public let EXPR_NODES: [Node] = [
477477
),
478478
Child(
479479
name: "RightBrace",
480-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
481-
requiresLeadingNewline: true
480+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
482481
),
483482
]
484483
),
@@ -1654,8 +1653,7 @@ public let EXPR_NODES: [Node] = [
16541653
),
16551654
Child(
16561655
name: "RightBrace",
1657-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
1658-
requiresLeadingNewline: true
1656+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
16591657
),
16601658
]
16611659
),

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftbasicformat/BasicFormatExtensionsFile.swift

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ let basicFormatExtensionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
6565
)
6666
}
6767

68-
try! ExtensionDeclSyntax("public extension TokenSyntax") {
69-
DeclSyntax(
70-
"""
71-
var requiresLeadingNewline: Bool {
72-
if let keyPath = keyPathInParent, keyPath.requiresLeadingNewline {
73-
return true
74-
}
75-
return false
76-
}
77-
"""
78-
)
79-
}
80-
8168
try! ExtensionDeclSyntax("fileprivate extension AnyKeyPath") {
8269
try VariableDeclSyntax("var requiresIndent: Bool") {
8370
try SwitchExprSyntax("switch self") {
@@ -94,21 +81,6 @@ let basicFormatExtensionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
9481
}
9582
}
9683

97-
try VariableDeclSyntax("var requiresLeadingNewline: Bool") {
98-
try SwitchExprSyntax("switch self") {
99-
for node in SYNTAX_NODES where !node.isBase {
100-
for child in node.children where child.requiresLeadingNewline {
101-
SwitchCaseSyntax("case \\\(raw: node.type.syntaxBaseName).\(raw: child.swiftName):") {
102-
StmtSyntax("return true")
103-
}
104-
}
105-
}
106-
SwitchCaseSyntax("default:") {
107-
StmtSyntax("return false")
108-
}
109-
}
110-
}
111-
11284
try VariableDeclSyntax("var requiresLeadingSpace: Bool?") {
11385
try SwitchExprSyntax("switch self") {
11486
for node in SYNTAX_NODES where !node.isBase {

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ open class BasicFormat: SyntaxRewriter {
155155
{
156156
return false
157157
} else if let second {
158-
if second.requiresLeadingNewline {
159-
return true
160-
}
161-
162158
var ancestor: Syntax = Syntax(second)
163159
while let parent = ancestor.parent {
164160
ancestor = parent
@@ -178,11 +174,15 @@ open class BasicFormat: SyntaxRewriter {
178174
}
179175

180176
switch (first?.tokenKind, second?.tokenKind) {
177+
case (.leftBrace, .rightBrace): // empty declarations like `struct {}`.
178+
return false
181179
case (.multilineStringQuote, .backslash), // string interpolation segment inside a multi-line string literal
182180
(.multilineStringQuote, .multilineStringQuote), // empty multi-line string literal
183181
(.multilineStringQuote, .stringSegment), // segment starting a multi-line string literal
184182
(.stringSegment, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
185-
(.rightParen, .multilineStringQuote): // ending a multi-line string literal that has a string interpolation segment at its end
183+
(.rightParen, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
184+
(_, .rightBrace),
185+
(_, .poundEndifKeyword):
186186
return true
187187
default:
188188
return false

Sources/SwiftBasicFormat/generated/BasicFormat+Extensions.swift

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ public extension SyntaxProtocol {
2323
}
2424
}
2525

26-
public extension TokenSyntax {
27-
var requiresLeadingNewline: Bool {
28-
if let keyPath = keyPathInParent, keyPath.requiresLeadingNewline {
29-
return true
30-
}
31-
return false
32-
}
33-
}
34-
3526
fileprivate extension AnyKeyPath {
3627
var requiresIndent: Bool {
3728
switch self {
@@ -70,27 +61,6 @@ fileprivate extension AnyKeyPath {
7061
}
7162
}
7263

73-
var requiresLeadingNewline: Bool {
74-
switch self {
75-
case \AccessorBlockSyntax.rightBrace:
76-
return true
77-
case \ClosureExprSyntax.rightBrace:
78-
return true
79-
case \CodeBlockSyntax.rightBrace:
80-
return true
81-
case \IfConfigClauseSyntax.poundKeyword:
82-
return true
83-
case \IfConfigDeclSyntax.poundEndif:
84-
return true
85-
case \MemberDeclBlockSyntax.rightBrace:
86-
return true
87-
case \SwitchExprSyntax.rightBrace:
88-
return true
89-
default:
90-
return false
91-
}
92-
}
93-
9464
var requiresLeadingSpace: Bool? {
9565
switch self {
9666
case \AvailabilityArgumentSyntax.entry:

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ final class DeclarationTests: XCTestCase {
127127
DiagnosticSpec(message: "expected member block in class", fixIts: ["insert member block"]),
128128
],
129129
fixedSource: """
130-
class T where t{
131-
}
130+
class T where t{}
132131
"""
133132
)
134133
assertParse(
@@ -148,8 +147,7 @@ final class DeclarationTests: XCTestCase {
148147
),
149148
],
150149
fixedSource: """
151-
class B<where g> {
152-
}
150+
class B<where g> {}
153151
"""
154152
)
155153
}
@@ -882,8 +880,7 @@ final class DeclarationTests: XCTestCase {
882880
),
883881
],
884882
fixedSource: """
885-
struct S {
886-
}
883+
struct S {}
887884
/ ###line 25 "line-directive.swift"/
888885
}
889886
"""
@@ -1197,8 +1194,7 @@ final class DeclarationTests: XCTestCase {
11971194
DiagnosticSpec(locationMarker: "2️⃣", message: "expected member block in class", fixIts: ["insert member block"]),
11981195
],
11991196
fixedSource: """
1200-
}class C {
1201-
}
1197+
}class C {}
12021198
"""
12031199
)
12041200
assertParse(
@@ -1208,8 +1204,7 @@ final class DeclarationTests: XCTestCase {
12081204
DiagnosticSpec(locationMarker: "2️⃣", message: "expected member block in enum", fixIts: ["insert member block"]),
12091205
],
12101206
fixedSource: """
1211-
}enum C {
1212-
}
1207+
}enum C {}
12131208
"""
12141209
)
12151210
assertParse(
@@ -1219,8 +1214,7 @@ final class DeclarationTests: XCTestCase {
12191214
DiagnosticSpec(locationMarker: "2️⃣", message: "expected member block in protocol", fixIts: ["insert member block"]),
12201215
],
12211216
fixedSource: """
1222-
}protocol C {
1223-
}
1217+
}protocol C {}
12241218
"""
12251219
)
12261220
assertParse(
@@ -1230,8 +1224,7 @@ final class DeclarationTests: XCTestCase {
12301224
DiagnosticSpec(locationMarker: "2️⃣", message: "expected member block in actor", fixIts: ["insert member block"]),
12311225
],
12321226
fixedSource: """
1233-
}actor C {
1234-
}
1227+
}actor C {}
12351228
"""
12361229
)
12371230
assertParse(
@@ -1241,8 +1234,7 @@ final class DeclarationTests: XCTestCase {
12411234
DiagnosticSpec(locationMarker: "2️⃣", message: "expected member block in struct", fixIts: ["insert member block"]),
12421235
],
12431236
fixedSource: """
1244-
}struct C {
1245-
}
1237+
}struct C {}
12461238
"""
12471239
)
12481240
assertParse(
@@ -1384,8 +1376,7 @@ final class DeclarationTests: XCTestCase {
13841376
),
13851377
],
13861378
fixedSource: """
1387-
struct U<@<#type#> <#identifier#>> {
1388-
}
1379+
struct U<@<#type#> <#identifier#>> {}
13891380
"""
13901381
)
13911382
}
@@ -1402,8 +1393,7 @@ final class DeclarationTests: XCTestCase {
14021393
],
14031394
fixedSource: """
14041395
struct Foo {
1405-
struct Bar {
1406-
}
1396+
struct Bar {}
14071397
}
14081398
"""
14091399
)
@@ -1892,8 +1882,7 @@ final class DeclarationTests: XCTestCase {
18921882
DiagnosticSpec(locationMarker: "3️⃣", message: "expected member block in protocol", fixIts: ["insert member block"]),
18931883
],
18941884
fixedSource: """
1895-
protocol <#identifier#><<#identifier#>>: <#type#> {
1896-
}
1885+
protocol <#identifier#><<#identifier#>>: <#type#> {}
18971886
"""
18981887
)
18991888
}

Tests/SwiftParserTest/StatementTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,7 @@ final class StatementTests: XCTestCase {
758758
),
759759
],
760760
fixedSource: """
761-
subscript() -> <#type#> {
762-
}@self _modify
761+
subscript() -> <#type#> {}@self _modify
763762
"""
764763
)
765764
}

Tests/SwiftParserTest/translated/ActorTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ final class ActorTests: XCTestCase {
2727
)
2828
],
2929
fixedSource: """
30-
actor MyActor1 {
31-
}
30+
actor MyActor1 {}
3231
"""
3332
)
3433
}

Tests/SwiftParserTest/translated/GuardTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ final class GuardTests: XCTestCase {
2727
],
2828
fixedSource: """
2929
func noConditionNoElse() {
30-
guard {} else {
31-
}
30+
guard {} else {}
3231
}
3332
"""
3433
)

0 commit comments

Comments
 (0)