Skip to content

Commit 585a13f

Browse files
committed
Remove requiresLeadingNewline from basic format
1 parent 4c0ad1b commit 585a13f

File tree

9 files changed

+39
-74
lines changed

9 files changed

+39
-74
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

@@ -160,8 +159,7 @@ public class Child {
160159
isOptional: Bool = false,
161160
classification: String? = nil,
162161
forceClassification: Bool = false,
163-
isIndented: Bool = false,
164-
requiresLeadingNewline: Bool = false
162+
isIndented: Bool = false
165163
) {
166164
if let firstCharInName = name.first {
167165
precondition(firstCharInName.isUppercase == true, "The first letter of a child’s name should be uppercase")
@@ -173,7 +171,6 @@ public class Child {
173171
self.classification = classificationByName(classification)
174172
self.forceClassification = forceClassification
175173
self.isIndented = isIndented
176-
self.requiresLeadingNewline = requiresLeadingNewline
177174
self.isOptional = isOptional
178175
}
179176
}

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

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

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
),
@@ -992,8 +991,7 @@ public let DECL_NODES: [Node] = [
992991
Child(
993992
name: "PoundKeyword",
994993
kind: .token(choices: [.token(tokenKind: "PoundIfToken"), .token(tokenKind: "PoundElseifToken"), .token(tokenKind: "PoundElseToken")]),
995-
classification: "BuildConfigId",
996-
requiresLeadingNewline: true
994+
classification: "BuildConfigId"
997995
),
998996
Child(
999997
name: "Condition",
@@ -1045,8 +1043,7 @@ public let DECL_NODES: [Node] = [
10451043
Child(
10461044
name: "PoundEndif",
10471045
kind: .token(choices: [.token(tokenKind: "PoundEndifToken")]),
1048-
classification: "BuildConfigId",
1049-
requiresLeadingNewline: true
1046+
classification: "BuildConfigId"
10501047
),
10511048
]
10521049
),
@@ -1355,8 +1352,7 @@ public let DECL_NODES: [Node] = [
13551352
),
13561353
Child(
13571354
name: "RightBrace",
1358-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
1359-
requiresLeadingNewline: true
1355+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
13601356
),
13611357
]
13621358
),

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ public let EXPR_NODES: [Node] = [
473473
),
474474
Child(
475475
name: "RightBrace",
476-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
477-
requiresLeadingNewline: true
476+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
478477
),
479478
]
480479
),
@@ -1643,8 +1642,7 @@ public let EXPR_NODES: [Node] = [
16431642
),
16441643
Child(
16451644
name: "RightBrace",
1646-
kind: .token(choices: [.token(tokenKind: "RightBraceToken")]),
1647-
requiresLeadingNewline: true
1645+
kind: .token(choices: [.token(tokenKind: "RightBraceToken")])
16481646
),
16491647
]
16501648
),

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,6 @@ let basicFormatExtensionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
9494
}
9595
}
9696

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

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)