Skip to content

Commit f731edc

Browse files
committed
Rewrite logic for CHECK_FIELD_KIND to improve readability
1 parent b795929 commit f731edc

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

templates/lib/prism/node.rb.erb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ module Prism
9898
raise NoMethodError, "undefined method `type' for #{inspect}"
9999
end
100100
end
101-
<%- if ENV["CHECK_FIELD_KIND"] -%>
102-
CHECK_FIELD_KIND = ENV["CHECK_FIELD_KIND"]
103-
<%- end -%>
104101
<%- nodes.each do |node| -%>
105102

106103
<%- node.each_comment_line do |line| -%>
@@ -113,26 +110,8 @@ module Prism
113110
@newline = false
114111
@location = location
115112
<%- node.fields.each do |field| -%>
116-
<%- if ENV["CHECK_FIELD_KIND"] -%>
117-
<%- if field.respond_to?(:union_kind) && field.union_kind -%>
118-
<%- case field -%>
119-
<%- when Prism::NodeField -%>
120-
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && ![<%= field.union_kind.join(', ') %>].include?(<%= field.name %>.class)
121-
<%- when Prism::OptionalNodeField -%>
122-
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && ![<%= field.union_kind.join(', ') %>, NilClass].include?(<%= field.name %>.class)
123-
<%- when Prism::NodeListField -%>
124-
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.all? { |n| [<%= field.union_kind.join(', ') %>].include?(n.class) }
125-
<%- end -%>
126-
<%- elsif field.respond_to?(:specific_kind) && field.specific_kind -%>
127-
<%- case field -%>
128-
<%- when Prism::NodeField -%>
129-
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.is_a?(<%= field.specific_kind %>)
130-
<%- when Prism::OptionalNodeField -%>
131-
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.nil? && !<%= field.name %>.is_a?(<%= field.specific_kind %>)
132-
<%- when Prism::NodeListField -%>
133-
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.all? { |n| n.is_a?(<%= field.specific_kind %>) }
134-
<%- end -%>
135-
<%- end -%>
113+
<%- if Prism::CHECK_FIELD_KIND && field.respond_to?(:check_field_kind) -%>
114+
raise <%= field.name %>.inspect unless <%= field.check_field_kind %>
136115
<%- end -%>
137116
@<%= field.name %> = <%= field.name %>
138117
<%- end -%>

templates/template.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
module Prism
88
SERIALIZE_ONLY_SEMANTICS_FIELDS = ENV.fetch("PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS", false)
9+
CHECK_FIELD_KIND = ENV.fetch("CHECK_FIELD_KIND", false)
910

1011
JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "truffleruby"
1112
JAVA_STRING_TYPE = JAVA_BACKEND == "jruby" ? "org.jruby.RubySymbol" : "String"
@@ -123,6 +124,14 @@ def rbs_class
123124
def rbi_class
124125
"Prism::#{ruby_type}"
125126
end
127+
128+
def check_field_kind
129+
if union_kind
130+
"[#{union_kind.join(', ')}].include?(#{name}.class)"
131+
else
132+
"#{name}.is_a?(#{ruby_type})"
133+
end
134+
end
126135
end
127136

128137
# This represents a field on a node that is itself a node and can be
@@ -141,6 +150,14 @@ def rbs_class
141150
def rbi_class
142151
"T.nilable(Prism::#{ruby_type})"
143152
end
153+
154+
def check_field_kind
155+
if union_kind
156+
"[#{union_kind.join(', ')}, NilClass].include?(#{name}.class)"
157+
else
158+
"#{name}.nil? || #{name}.is_a?(#{ruby_type})"
159+
end
160+
end
144161
end
145162

146163
# This represents a field on a node that is a list of nodes. We pass them as
@@ -163,6 +180,14 @@ def rbi_class
163180
def java_type
164181
"#{super}[]"
165182
end
183+
184+
def check_field_kind
185+
if union_kind
186+
"#{name}.all? { |n| [#{union_kind.join(', ')}].include?(n.class) }"
187+
else
188+
"#{name}.all? { |n| n.is_a?(#{ruby_type}) }"
189+
end
190+
end
166191
end
167192

168193
# This represents a field on a node that is the ID of a string interned

0 commit comments

Comments
 (0)