Skip to content

Commit aa09b13

Browse files
committed
Remove requiresLeadingNewline from basic format
1 parent 2782139 commit aa09b13

File tree

9 files changed

+39
-87
lines changed

9 files changed

+39
-87
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: 6 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,16 @@ open class BasicFormat: SyntaxRewriter {
178174
}
179175

180176
switch (first?.tokenKind, second?.tokenKind) {
177+
181178
case (.multilineStringQuote, .backslash), // string interpolation segment inside a multi-line string literal
182179
(.multilineStringQuote, .multilineStringQuote), // empty multi-line string literal
183180
(.multilineStringQuote, .stringSegment), // segment starting a multi-line string literal
184181
(.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
182+
(.rightParen, .multilineStringQuote), // ending a multi-line string literal that has a string interpolation segment at its end
183+
(_, .poundElseKeyword),
184+
(_, .poundElseifKeyword),
185+
(_, .poundEndifKeyword),
186+
(_, .rightBrace):
186187
return true
187188
default:
188189
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/SwiftSyntaxBuilderTest/IfConfigDeclSyntaxTests.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@ final class IfConfigDeclSyntaxTests: XCTestCase {
3434
)
3535
)
3636
IfConfigClauseSyntax(
37-
poundKeyword: .poundElseKeyword(leadingTrivia: .newline),
37+
poundKeyword: .poundElseifKeyword(),
38+
condition: ExprSyntax("TEST"),
39+
elements: .statements(
40+
CodeBlockItemListSyntax {
41+
DeclSyntax(
42+
"""
43+
public func debug(_ data: Foo) -> String {
44+
return data.description
45+
}
46+
"""
47+
)
48+
}
49+
)
50+
)
51+
IfConfigClauseSyntax(
52+
poundKeyword: .poundElseKeyword(),
3853
elements: .statements(
3954
CodeBlockItemListSyntax {
4055
DeclSyntax(
@@ -48,7 +63,7 @@ final class IfConfigDeclSyntaxTests: XCTestCase {
4863
)
4964
)
5065
},
51-
poundEndif: .poundEndifKeyword(leadingTrivia: .newline)
66+
poundEndif: .poundEndifKeyword()
5267
)
5368

5469
assertBuildResult(
@@ -58,6 +73,10 @@ final class IfConfigDeclSyntaxTests: XCTestCase {
5873
public func debug(_ data: Foo) -> String {
5974
return data.debugDescription
6075
}
76+
#elseif TEST
77+
public func debug(_ data: Foo) -> String {
78+
return data.description
79+
}
6180
#else
6281
public func debug(_ data: Foo) -> String {
6382
return data.description

Tests/SwiftSyntaxBuilderTest/StringLiteralExprSyntaxTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ final class StringLiteralExprSyntaxTests: XCTestCase {
168168
// Tab should not be escaped in single-line string literals
169169
assertBuildResult(
170170
StringLiteralExprSyntax(
171-
openQuote: .multilineStringQuoteToken(trailingTrivia: .newline),
171+
openQuote: .multilineStringQuoteToken(),
172172
content: "a\tb",
173-
closeQuote: .multilineStringQuoteToken(leadingTrivia: .newline)
173+
closeQuote: .multilineStringQuoteToken()
174174
),
175175
#"""
176176
"""
@@ -225,7 +225,7 @@ final class StringLiteralExprSyntaxTests: XCTestCase {
225225

226226
func testMultiLineStringWithResultBuilder() {
227227
let buildable = StringLiteralExprSyntax(
228-
openQuote: .multilineStringQuoteToken(trailingTrivia: .newline),
228+
openQuote: .multilineStringQuoteToken(),
229229
segments: StringLiteralSegmentsSyntax {
230230
StringSegmentSyntax(content: .stringSegment(#"Error validating child at index \(index) of \(nodeKind):"#), trailingTrivia: .newline)
231231
StringSegmentSyntax(content: .stringSegment(#"Node did not satisfy any node choice requirement."#), trailingTrivia: .newline)
@@ -236,7 +236,7 @@ final class StringLiteralExprSyntaxTests: XCTestCase {
236236
}
237237
)
238238
},
239-
closeQuote: .multilineStringQuoteToken(leadingTrivia: .newline)
239+
closeQuote: .multilineStringQuoteToken()
240240
)
241241

242242
assertBuildResult(

0 commit comments

Comments
 (0)