Module: RuboCop::AST::Traversal::CallbackCompiler Private
Do not use. This module is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Defined in: | lib/rubocop/ast/traversal.rb |
Constant Summary
-
SEND =
# File 'lib/rubocop/ast/traversal.rb', line 26'send(TYPE_TO_METHOD[child.type], child)'
-
TEMPLATE =
How a particular child node should be visited. For example, if a child node can be nil it should be guarded behind a nil check. Or, if a child node is a literal (like a symbol) then the literal itself should not be visited.
{ skip: '', always: code, nil?: "#{code} if child" }.freeze
Instance Method Summary
- #body(child_node_types, expected_children_count) Internal use only
- #children_count_check_code(range) Internal use only
- #def_callback(type, *child_node_types, expected_children_count: child_node_types.size..child_node_types.size, body: self.body(child_node_types, expected_children_count)) Internal use only
Instance Method Details
#body(child_node_types, expected_children_count)
[ GitHub ]# File 'lib/rubocop/ast/traversal.rb', line 54
def body(child_node_types, expected_children_count) visit_children_code = child_node_types .map.with_index do |child_type, i| TEMPLATE.fetch(child_type).gsub('%<index>i', i.to_s) end .join("\n") <<~BODY #{children_count_check_code(expected_children_count)} #{visit_children_code} BODY end
#children_count_check_code(range)
[ GitHub ]# File 'lib/rubocop/ast/traversal.rb', line 68
def children_count_check_code(range) return '' unless ENV.fetch('RUBOCOP_DEBUG', false) <<~RUBY n = node.children.size raise DebugError, [ 'Expected #{range} children, got', n, 'for', node.inspect ].join(' ') unless (#{range}).cover?(node.children.size) RUBY end
#def_callback(type, *child_node_types, expected_children_count: child_node_types.size..child_node_types.size, body: self.body(child_node_types, expected_children_count))
[ GitHub ]# File 'lib/rubocop/ast/traversal.rb', line 38
def def_callback(type, *child_node_types, expected_children_count: child_node_types.size..child_node_types.size, body: self.body(child_node_types, expected_children_count)) type, *aliases = type lineno = caller_locations(1, 1).first.lineno module_eval(<<~RUBY, __FILE__, lineno) def on_#{type}(node) # def on_send(node) #{body} # # body ... nil # nil end # end RUBY aliases.each do |m| alias_method :"on_#{m}", :"on_#{type}" end end