Skip to content

Commit d7d87aa

Browse files
committed
Add convenience initializers for more nodes
1 parent 1888022 commit d7d87aa

File tree

6 files changed

+1227
-19
lines changed

6 files changed

+1227
-19
lines changed

Sources/SwiftSyntaxBuilder/BuildablesConvenienceInitializers.swift.gyb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# -*- mode: Swift -*-
66
# Ignore the following admonition it applies to the resulting .swift file only
77
}%
8-
//// Automatically Generated From DeclBuildables.swift.gyb.
8+
//// Automatically Generated From BuildablesConvenienceInitializers.swift.gyb.
99
//// Do Not Edit Directly!
1010
//===----------------------------------------------------------------------===//
1111
//
@@ -24,13 +24,19 @@ import SwiftSyntax
2424
% for node in SYNTAX_NODES:
2525
% if node.is_buildable():
2626
% has_syntax_collection_child = False
27+
% has_token_child = False
2728
% for child in node.children:
2829
% child_node = NODE_MAP.get(child.syntax_kind)
30+
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
2931
% if child_node and child_node.is_syntax_collection():
32+
% # If node have syntax collection as child, we can create a convenient initializer
3033
% has_syntax_collection_child = True
34+
% elif (child_token and (not child.is_optional or not child_token.text)) or child.syntax_kind is "IdentifierToken":
35+
% # If the node have at least one child that is a token or identifier token, we can create a Cconvenient initializer
36+
% has_token_child = True
3137
% end
3238
% end
33-
% if has_syntax_collection_child:
39+
% if has_syntax_collection_child or has_token_child:
3440
extension ${node.syntax_kind} {
3541
public init(
3642
% init_parameters = []
@@ -45,18 +51,27 @@ extension ${node.syntax_kind} {
4551
% # Allow initializing identifier or a token without a text with String value
4652
% param_type = "String?" if child.is_optional else "String"
4753
% init_parameters.append("%s: %s" % (child.swift_name, param_type))
48-
% else:
49-
% # When type is not handled above, use default value
54+
% elif has_syntax_collection_child:
55+
% # When node have a syntax collection as child we can use all parameters
5056
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
5157
% default_value = ""
5258
% if token and token.text and not child.is_optional:
5359
% default_value = " = Tokens.`%s`" % lowercase_first_word(token.name)
5460
% elif child.is_optional:
5561
% default_value = " = nil"
5662
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
63+
% elif not token or child.is_optional:
64+
% # Allow initializing with parameter when there is no token or the parameter is optional
65+
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
66+
% default_value = ""
67+
% if child.is_optional:
68+
% default_value = " = nil"
69+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
5770
% end
5871
% end
72+
% if bool(init_parameters):
5973
${',\n '.join(init_parameters)}
74+
% end
6075
) {
6176
self.init(
6277
% init_parameters = []
@@ -71,11 +86,13 @@ extension ${node.syntax_kind} {
7186
% else:
7287
% init_parameters.append("%s: SyntaxFactory.makeIdentifier(%s)" % (child.swift_name, child.swift_name))
7388
% end
89+
% elif not child.is_optional and not has_syntax_collection_child and token and token.text:
90+
% init_parameters.append("%s: Tokens.`%s`" % (child.swift_name, lowercase_first_word(token.name)))
7491
% elif token and not token.text:
7592
% if child.is_optional:
7693
% init_parameters.append("%s: %s.map(Tokens.%s)" % (child.swift_name, child.swift_name, lowercase_first_word(token.name)))
7794
% else:
78-
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, token.name, child.swift_name))
95+
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, lowercase_first_word(token.name), child.swift_name))
7996
% end
8097
% else:
8198
% init_parameters.append("%s: %s" % (child.swift_name, child.swift_name))
@@ -88,4 +105,4 @@ extension ${node.syntax_kind} {
88105

89106
% end
90107
% end
91-
% end
108+
% end

0 commit comments

Comments
 (0)