[ruby/prism] Add a simple way to check field kinds are correct by setting $CHECK_FIELD_KIND

https://github.com/ruby/prism/commit/33e987ba3c
This commit is contained in:
Benoit Daloze 2024-02-25 13:48:08 +01:00 коммит произвёл git
Родитель ea2cb00250
Коммит 1b0a5bcb0a
1 изменённых файлов: 24 добавлений и 0 удалений

Просмотреть файл

@ -98,6 +98,9 @@ module Prism
raise NoMethodError, "undefined method `type' for #{inspect}"
end
end
<%- if ENV["CHECK_FIELD_KIND"] -%>
CHECK_FIELD_KIND = ENV["CHECK_FIELD_KIND"]
<%- end -%>
<%- nodes.each do |node| -%>
<%- node.each_comment_line do |line| -%>
@ -110,6 +113,27 @@ module Prism
@newline = false
@location = location
<%- node.fields.each do |field| -%>
<%- if ENV["CHECK_FIELD_KIND"] -%>
<%- if field.respond_to?(:union_kind) && field.union_kind -%>
<%- case field -%>
<%- when Prism::NodeField -%>
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && ![<%= field.union_kind.join(', ') %>].include?(<%= field.name %>.class)
<%- when Prism::OptionalNodeField -%>
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && ![<%= field.union_kind.join(', ') %>, NilClass].include?(<%= field.name %>.class)
<%- when Prism::NodeListField -%>
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.all? { |n| [<%= field.union_kind.join(', ') %>].include?(n.class) }
<%- end -%>
<%- elsif field.respond_to?(:specific_kind) && field.specific_kind -%>
<%- case field -%>
<%- when Prism::NodeField -%>
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.is_a?(<%= field.specific_kind %>)
<%- when Prism::OptionalNodeField -%>
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.nil? && !<%= field.name %>.is_a?(<%= field.specific_kind %>)
<%- when Prism::NodeListField -%>
raise <%= field.name %>.inspect if CHECK_FIELD_KIND && !<%= field.name %>.all? { |n| n.is_a?(<%= field.specific_kind %>) }
<%- end -%>
<%- end -%>
<%- end -%>
@<%= field.name %> = <%= field.name %>
<%- end -%>
end