Skip to content

Commit 1258671

Browse files
committed
Make garbage parameters in SyntaxFactory unlabeled
If the `garbage` parameters in the SyntaxFactory are labelled, it’s not possible to specify e.g. the second garbage node but leave the others to their default (swiftlang/swift#60274). We could either give these `garbage` parameters distinct names or make them unlabeled to work around that issue. I chose to go for the unlabeled approach because it should be clear that these parameters contain garbage because of their type.
1 parent 438f48b commit 1258671

File tree

8 files changed

+887
-883
lines changed

8 files changed

+887
-883
lines changed

Sources/SwiftSyntax/SyntaxFactory.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public enum SyntaxFactory {
5757
% param_type = param_type + "?"
5858
% end
5959
% if child.is_garbage_nodes():
60-
% child_params.append("garbage %s: %s = nil" % (child.swift_name, param_type))
60+
% # It would be nice if we could label all of these arguments 'garbage'.
61+
% # But if we do this, we hit https://github.com/apple/swift/issues/60274.
62+
% child_params.append("_ %s: %s = nil" % (child.swift_name, param_type))
6163
% else:
6264
% child_params.append("%s: %s" % (child.swift_name, param_type))
6365
% end

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 202 additions & 202 deletions
Large diffs are not rendered by default.

Sources/SwiftSyntaxBuilder/BuildableNodes.swift.gyb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,15 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
117117
/// - Returns: The built `${type.syntax()}`.
118118
func build${type.base_name()}(format: Format, leadingTrivia additionalLeadingTrivia: Trivia? = nil) -> ${type.syntax()} {
119119
let result = SyntaxFactory.make${type.base_name()}(
120-
${',\n '.join(['%s: %s' % (child.factory_parameter_name(), child.generate_expr_build_syntax_node(child.name(), 'format')) for child in children])}
120+
% parameters = []
121+
% for (index, child) in enumerate(children):
122+
% comma = ',' if index != len(children) - 1 else ''
123+
% if child.child.is_garbage_nodes():
124+
${child.generate_expr_build_syntax_node(child.name(), 'format')}${comma}
125+
% else:
126+
${child.name()}: ${child.generate_expr_build_syntax_node(child.name(), 'format')}${comma}
127+
% end
128+
% end
121129
)
122130
let combinedLeadingTrivia = leadingTrivia + (additionalLeadingTrivia ?? []) + (result.leadingTrivia ?? [])
123131
return result.withLeadingTrivia(combinedLeadingTrivia.addingSpacingAfterNewlinesIfNeeded())

Sources/SwiftSyntaxBuilder/generated/ExpressibleAsProtocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public protocol ExpressibleAsSyntaxBuildable: ExpressibleAsGarbageNodes, Express
6161
}
6262
public extension ExpressibleAsSyntaxBuildable {
6363
/// Conformance to `ExpressibleAsGarbageNodes`
64-
func createGarbageNodes() -> GarbageNodes {
64+
func createGarbageNodes() -> GarbageNodes {
6565
return GarbageNodes([self])
6666
}
6767
/// Conformance to `ExpressibleAsStringLiteralSegments`

Sources/SwiftSyntaxBuilder/gyb_generated/BuildableCollectionNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public struct GarbageNodes: ExpressibleByArrayLiteral, SyntaxBuildable, Expressi
9292
$0.buildSyntax(format: format, leadingTrivia: nil)
9393
})
9494
if let leadingTrivia = leadingTrivia {
95-
return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
95+
return result.withLeadingTrivia((leadingTrivia + (result.leadingTrivia ?? [])).addingSpacingAfterNewlinesIfNeeded())
9696
} else {
9797
return result
9898
}

Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift

Lines changed: 667 additions & 667 deletions
Large diffs are not rendered by default.

Sources/SwiftSyntaxBuilder/gyb_helpers/SyntaxBuildableWrappers.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ def name(self):
1919
return self.child.swift_name
2020

2121

22-
def factory_parameter_name(self):
23-
if self.child.is_garbage_nodes():
24-
return 'garbage'
25-
else:
26-
return self.name()
27-
2822
def type(self):
2923
"""
3024
The type of this child, represented by a `SyntaxBuildableType`, which can be used to create the corresponding `Buildable` and `ExpressibleAs` types.

Tests/SwiftSyntaxTest/VisitorTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ public class VisitorTests: XCTestCase {
2727
public func testVisitGarbage() throws {
2828
// This is just bunch of garbage
2929
let garbageReturnStmt = SyntaxFactory.makeReturnStmt(
30-
garbage: SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("starting"), presence: .present)), Syntax(SyntaxFactory.makeToken(.identifier("garbage"), presence: .present))]),
30+
SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("starting"), presence: .present)), Syntax(SyntaxFactory.makeToken(.identifier("garbage"), presence: .present))]),
3131
returnKeyword: SyntaxFactory.makeReturnKeyword(trailingTrivia: [.spaces(1)]),
32-
garbage: SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("middle"), presence: .present))]),
33-
expression: ExprSyntax(SyntaxFactory.makeNilLiteralExpr(garbage: SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("end"), presence: .present))]), nilKeyword: SyntaxFactory.makeToken(.nilKeyword, presence: .present)))
32+
SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("middle"), presence: .present))]),
33+
expression: ExprSyntax(SyntaxFactory.makeNilLiteralExpr(SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("end"), presence: .present))]), nilKeyword: SyntaxFactory.makeToken(.nilKeyword, presence: .present)))
3434
)
3535

3636
// This is more real-world where the user wrote null instead of nil.
3737
let misspelledNil = SyntaxFactory.makeReturnStmt(
3838
returnKeyword: SyntaxFactory.makeReturnKeyword(trailingTrivia: [.spaces(1)]),
39-
expression: ExprSyntax(SyntaxFactory.makeNilLiteralExpr(garbage: SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("null"), presence: .present))]), nilKeyword: SyntaxFactory.makeToken(.nilKeyword, presence: .missing)))
39+
expression: ExprSyntax(SyntaxFactory.makeNilLiteralExpr(SyntaxFactory.makeGarbageNodes([Syntax(SyntaxFactory.makeToken(.identifier("null"), presence: .present))]), nilKeyword: SyntaxFactory.makeToken(.nilKeyword, presence: .missing)))
4040
)
4141

4242
// Test SyntaxVisitor

0 commit comments

Comments
 (0)