Skip to content

Commit 14f3604

Browse files
committed
Add asserts in init methods
1 parent f17a5b5 commit 14f3604

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
@@ -159,6 +159,20 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
159159
% else:
160160
self.${child.swift_name} = ${child.swift_name}${create_method_dot}create${param_type}()
161161
% end
162+
% assert_choices = []
163+
% if child.text_choices:
164+
% assert_choices = map(lambda x : '"' + x + '"', child.text_choices)
165+
% elif child.token_choices:
166+
% assert_choices = map(lambda x : '"' + SYNTAX_TOKEN_MAP.get(x.name + 'Token').text + '"', child.token_choices)
167+
% # If a Token has an empty text, it can contain arbitrary text.
168+
% # If any of the token choices may contain arbitrary text, set assert_choices to [] so we don’t assert on the token’s text below.
169+
% assert_choices = [] if '""' in assert_choices else assert_choices
170+
% end
171+
% if assert_choices:
172+
% optional_condition = child.swift_name + ' == nil || ' if child.is_optional else ""
173+
% unwrap = "!" if child.is_optional else ""
174+
assert(${optional_condition}[${', '.join(assert_choices)}].contains(self.${child.swift_name}${unwrap}.text))
175+
% end
162176
% end
163177
}
164178

0 commit comments

Comments
 (0)