Skip to content

Commit

Permalink
Add a simple way to check field kinds are correct by setting $CHECK_F…
Browse files Browse the repository at this point in the history
…IELD_KIND
  • Loading branch information
eregon committed Feb 28, 2024
1 parent f3e3310 commit 33e987b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
bundle exec rake compile
bundle exec rake check_annotations
bundle exec rake typecheck:steep
rm lib/prism/node.rb && CHECK_FIELD_KIND=true bundle exec rake
build:
strategy:
Expand Down
24 changes: 24 additions & 0 deletions templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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| -%>
Expand All @@ -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
Expand Down

0 comments on commit 33e987b

Please sign in to comment.