@@ -27,34 +27,36 @@ let buildableNodesFile = SourceFile {
27
27
let hasTrailingComma = node. traits. contains ( " WithTrailingComma " )
28
28
29
29
let docComment : SwiftSyntax . Trivia = node. documentation. isEmpty ? [ ] : . docLineComment( " /// \( node. documentation) " ) + . newline
30
- ExtensionDecl (
31
- leadingTrivia: docComment,
32
- extendedType: Type ( type. shorthandName) ,
33
- inheritanceClause: hasTrailingComma ? TypeInheritanceClause { InheritedType ( typeName: Type ( " HasTrailingComma " ) ) } : nil
34
- ) {
35
- // Generate initializers
36
- createDefaultInitializer ( node: node)
37
- if let convenienceInit = createConvenienceInitializer ( node: node) {
38
- convenienceInit
39
- }
30
+ let convenienceInit = createConvenienceInitializer ( node: node)
31
+
32
+ if convenienceInit != nil || hasTrailingComma {
33
+ ExtensionDecl (
34
+ leadingTrivia: docComment,
35
+ extendedType: Type ( type. shorthandName) ,
36
+ inheritanceClause: hasTrailingComma ? TypeInheritanceClause { InheritedType ( typeName: Type ( " HasTrailingComma " ) ) } : nil
37
+ ) {
38
+ if let convenienceInit = convenienceInit {
39
+ convenienceInit
40
+ }
40
41
41
- if hasTrailingComma {
42
- VariableDecl (
42
+ if hasTrailingComma {
43
+ VariableDecl (
43
44
"""
44
45
var hasTrailingComma: Bool {
45
46
return trailingComma != nil
46
47
}
47
48
"""
48
- )
49
+ )
49
50
50
- FunctionDecl (
51
+ FunctionDecl (
51
52
"""
52
53
/// Conformance to `HasTrailingComma`.
53
54
public func withTrailingComma(_ withComma: Bool) -> Self {
54
55
return withTrailingComma(withComma ? .commaToken() : nil)
55
56
}
56
57
"""
57
- )
58
+ )
59
+ }
58
60
}
59
61
}
60
62
}
@@ -68,57 +70,6 @@ private func convertFromSyntaxProtocolToSyntaxType(child: Child) -> Expr {
68
70
}
69
71
}
70
72
71
- /// Create the default initializer for the given node.
72
- private func createDefaultInitializer( node: Node ) -> InitializerDecl {
73
- let type = node. type
74
- return InitializerDecl (
75
- leadingTrivia: ( [
76
- " /// Creates a ` \( type. shorthandName) ` using the provided parameters. " ,
77
- " /// - Parameters: " ,
78
- ] + node. children. map { child in
79
- " /// - \( child. swiftName) : \( child. documentation) "
80
- } ) . map { . docLineComment( $0) + . newline } . reduce ( [ ] , + ) ,
81
- // FIXME: If all parameters are specified, the SwiftSyntaxBuilder initializer is ambigious
82
- // with the memberwise initializer in SwiftSyntax.
83
- // Hot-fix this by preferring the overload in SwiftSyntax. In the long term, consider sinking
84
- // this initializer to SwiftSyntax.
85
- attributes: AttributeList { CustomAttribute ( " _disfavoredOverload " ) . withTrailingTrivia ( . space) } ,
86
- modifiers: [ DeclModifier ( name: . public) ] ,
87
- signature: FunctionSignature (
88
- input: ParameterClause {
89
- for trivia in [ " leadingTrivia " , " trailingTrivia " ] {
90
- FunctionParameter ( " \( trivia) : Trivia = [] " , for: . functionParameters)
91
- }
92
- for child in node. children {
93
- FunctionParameter (
94
- firstName: . identifier( child. swiftName) ,
95
- colon: . colon,
96
- type: child. parameterType,
97
- defaultArgument: child. type. defaultInitialization. map { InitializerClause ( value: $0) }
98
- )
99
- }
100
- }
101
- )
102
- ) {
103
- for child in node. children {
104
- if let assertStmt = child. generateAssertStmtTextChoices ( varName: child. swiftName) {
105
- assertStmt
106
- }
107
- }
108
- let nodeConstructorCall = FunctionCallExpr ( callee: type. syntaxBaseName) {
109
- for child in node. children {
110
- TupleExprElement (
111
- label: child. isUnexpectedNodes ? nil : child. swiftName,
112
- expression: convertFromSyntaxProtocolToSyntaxType ( child: child)
113
- )
114
- }
115
- }
116
- SequenceExpr ( " self = \( nodeConstructorCall) " )
117
- SequenceExpr ( " self.leadingTrivia = leadingTrivia + (self.leadingTrivia ?? []) " )
118
- SequenceExpr ( " self.trailingTrivia = trailingTrivia + (self.trailingTrivia ?? []) " )
119
- }
120
- }
121
-
122
73
/// Create a builder-based convenience initializer, if needed.
123
74
private func createConvenienceInitializer( node: Node ) -> InitializerDecl ? {
124
75
// Only create the convenience initializer if at least one parameter
@@ -193,28 +144,24 @@ private func createConvenienceInitializer(node: Node) -> InitializerDecl? {
193
144
" /// - Initializing syntax collections using result builders " ,
194
145
" /// - Initializing tokens without default text using strings " ,
195
146
] . map { . docLineComment( $0) + . newline } . reduce ( [ ] , + ) ,
196
- // FIXME: If all parameters are specified, the SwiftSyntaxBuilder initializer is ambigious
197
- // with the memberwise initializer in SwiftSyntax.
198
- // Hot-fix this by preferring the overload in SwiftSyntax. In the long term, consider sinking
199
- // this initializer to SwiftSyntax.
200
- attributes: AttributeList { CustomAttribute ( " _disfavoredOverload " ) . withTrailingTrivia ( . space) } ,
201
147
modifiers: [ DeclModifier ( name: . public) ] ,
202
148
signature: FunctionSignature (
203
149
input: ParameterClause {
204
- FunctionParameter ( " leadingTrivia: Trivia = [] " , for: . functionParameters)
150
+ FunctionParameter ( " leadingTrivia: Trivia? = nil " , for: . functionParameters)
205
151
for param in normalParameters + builderParameters {
206
152
param
207
153
}
154
+ FunctionParameter ( " trailingTrivia: Trivia? = nil " , for: . functionParameters)
208
155
}
209
156
)
210
157
) {
211
158
FunctionCallExpr ( callee: " self.init " ) {
159
+ TupleExprElement ( label: " leadingTrivia " , expression: " leadingTrivia " )
212
160
for arg in delegatedInitArgs {
213
161
arg
214
162
}
163
+ TupleExprElement ( label: " trailingTrivia " , expression: " trailingTrivia " )
215
164
}
216
-
217
- SequenceExpr ( " self.leadingTrivia = leadingTrivia + (self.leadingTrivia ?? []) " )
218
165
}
219
166
}
220
167
0 commit comments