Skip to content

Commit eec37f3

Browse files
authored
Merge pull request #465 from fwcd/improve-builder-inits
Clean up generation of builder-based convenience initializers
2 parents 0a0bec6 + ade5af4 commit eec37f3

22 files changed

+419
-937
lines changed

Sources/SwiftSyntaxBuilder/BuildableNodes.swift.gyb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%{
22
from gyb_syntax_support import SYNTAX_NODES
3-
from gyb_helpers import SyntaxBuildableNode, conformance_clause
3+
from gyb_helpers import SyntaxBuildableNode, SyntaxBuildableType, conformance_clause
44
# -*- mode: Swift -*-
55
# Ignore the following admonition it applies to the resulting .swift file only
66
}%
@@ -66,10 +66,15 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
6666
delegated_init_args = []
6767
for child in node.children():
6868
produce_expr = None # The expression that is used to call the default initializer defined above
69-
if child.type().is_syntax_collection(): # Allow initializing syntax collections with result builders
69+
if child.type().is_builder_initializable(): # Allow initializing certain syntax collections with result builders
7070
create_convenience_initializer = True
71-
default_value = ' = { nil }' if child.type().is_optional else ' = { %s([]) }' % (child.type().buildable())
72-
convenience_init_result_builder_parameters.append('@%s %sBuilder: () -> %s%s' % (child.type().non_optional().result_builder(), child.name(), child.type().expressible_as(), default_value))
71+
default_value = ' = { nil }' if child.type().is_optional else ' = { %s([]) }' % (child.type().builder_initializable_type().buildable())
72+
convenience_init_result_builder_parameters.append('@%s %sBuilder: () -> %s%s' % (
73+
child.type().builder_initializable_type().non_optional().result_builder(),
74+
child.name(),
75+
child.type().builder_initializable_type().expressible_as(),
76+
default_value
77+
))
7378
produce_expr = '%sBuilder()' % child.name()
7479
elif child.type().token() and not child.type().token().text: # Allow initializing identifier or a token without default text with String value
7580
create_convenience_initializer = True

Sources/SwiftSyntaxBuilder/ClassDeclConvenienceInitializers.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/DictionaryExprConvenienceInitializers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension DictionaryExpr {
1818
public init(
1919
leftSquare: TokenSyntax = .`leftSquareBracket`,
2020
rightSquare: TokenSyntax = .`rightSquareBracket`,
21-
@DictionaryElementListBuilder contentBuilder: () -> ExpressibleAsDictionaryElementList = { DictionaryElementList([]) }
21+
@DictionaryElementListBuilder contentBuilder: () -> ExpressibleAsDictionaryElementList = { [] }
2222
) {
2323
let elementList = contentBuilder().createDictionaryElementList()
2424
self.init(
@@ -27,4 +27,4 @@ extension DictionaryExpr {
2727
rightSquare: rightSquare
2828
)
2929
}
30-
}
30+
}

Sources/SwiftSyntaxBuilder/EnumDeclConvenienceInitializers.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/ExtensionDeclConvenienceInitializers.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/FunctionCallExprConvenienceInitializers.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ extension FunctionCallExpr {
2020
public init(
2121
_ calledExpression: ExpressibleAsExprBuildable,
2222
trailingClosure: ExpressibleAsClosureExpr? = nil,
23-
@TupleExprElementListBuilder argumentListBuilder: () -> ExpressibleAsTupleExprElementList = { TupleExprElementList([]) },
24-
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }
23+
additionalTrailingClosures: MultipleTrailingClosureElementList? = nil,
24+
@TupleExprElementListBuilder argumentList: () -> ExpressibleAsTupleExprElementList = { [] }
2525
) {
26-
let argumentList = argumentListBuilder().createTupleExprElementList()
26+
let argumentList = argumentList().createTupleExprElementList()
2727
let shouldOmitParens = argumentList.elements.isEmpty && trailingClosure != nil
2828
self.init(
2929
calledExpression: calledExpression.createExprBuildable(),
3030
leftParen: shouldOmitParens ? nil : .leftParen,
3131
argumentList: argumentList,
3232
rightParen: shouldOmitParens ? nil : .rightParen,
3333
trailingClosure: trailingClosure,
34-
additionalTrailingClosures: additionalTrailingClosuresBuilder()
34+
additionalTrailingClosures: additionalTrailingClosures
3535
)
3636
}
3737
}

Sources/SwiftSyntaxBuilder/FunctionDeclConvenienceInitializers.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/ProtocolDeclConvenienceInitializers.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/StructDeclConvenienceInitializers.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

Sources/SwiftSyntaxBuilder/VariableDeclConvenienceInitializers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ extension VariableDecl {
1616
public init(_ letOrVarKeyword: TokenSyntax,
1717
name: ExpressibleAsIdentifierPattern,
1818
type: ExpressibleAsTypeAnnotation) {
19-
self.init(letOrVarKeyword: letOrVarKeyword, bindingsBuilder: {
19+
self.init(letOrVarKeyword: letOrVarKeyword) {
2020
PatternBinding(pattern: name.createIdentifierPattern(),
2121
typeAnnotation: type.createTypeAnnotation())
22-
})
22+
}
2323
}
2424
}

0 commit comments

Comments
 (0)