Skip to content

Commit

Permalink
[Fixes #117] Future-proof Traversal by detecting unknown Node types
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Oct 11, 2020
1 parent 4c5c0f4 commit 9595a51
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_futureproof_traversal_by_detecting.md
@@ -0,0 +1 @@
* [#117](https://github.com/rubocop-hq/rubocop-ast/pull/117): Future-proof `AST::Traversal` by detecting unknown `Node` types. ([@marcandre][])
21 changes: 21 additions & 0 deletions lib/rubocop/ast/traversal.rb
Expand Up @@ -201,6 +201,27 @@ def on_numblock(node)

send(TYPE_TO_METHOD[child.type], child)
end

defined = instance_methods(false)
.grep(/^on_/)
.map { |s| s.to_s[3..-1].to_sym } # :on_foo => :foo

to_define = ::Parser::Meta::NODE_TYPES.to_a
to_define -= defined
to_define -= %i[numargs ident] # transient
to_define -= %i[blockarg_expr restarg_expr] # obsolete
to_define -= %i[objc_kwarg objc_restarg objc_varargs] # mac_ruby
to_define.each do |type|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def on_#{type}(node)
node.children.each do |child|
next unless child.class == Node
send(TYPE_TO_METHOD[child.type], child)
end
nil
end
RUBY
end
end
end
end
Expand Down

0 comments on commit 9595a51

Please sign in to comment.