Skip to content

Commit 1a2c698

Browse files
committed
Add asserts in init methods
1 parent e73e11b commit 1a2c698

File tree

4 files changed

+330
-2
lines changed

4 files changed

+330
-2
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
136136
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
137137
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token())
138138
self.${child.swift_name} = ${child.swift_name}${create_method_dot}create${param_type}()
139+
% assert_choices = []
140+
% if child.text_choices:
141+
% assert_choices = map(lambda x : '"' + x + '"', child.text_choices)
142+
% elif child.token_choices:
143+
% assert_choices = map(lambda x : '"' + SYNTAX_TOKEN_MAP.get(x.name + 'Token').text + '"', child.token_choices)
144+
% # some cases, like IdentifierToken, will add an empty string.
145+
% # So if there is any empty string we just make it empty
146+
% assert_choices = [] if '""' in assert_choices else assert_choices
147+
% end
148+
% if assert_choices:
149+
% optional_condition = child.swift_name + ' == nil || ' if child.is_optional else ""
150+
% unwrap = "!" if child.is_optional else ""
151+
assert(${optional_condition}[${', '.join(assert_choices)}].contains(self.${child.swift_name}${unwrap}.text))
152+
% end
139153
% end
140154
}
141155

0 commit comments

Comments
 (0)