Module: RuboCop::Node::Traversal
- Included in:
- Cop::Commissioner
- Defined in:
- lib/rubocop/ast_node/traversal.rb
Overview
Provides methods for traversing an AST. Does not transform an AST; for that, use Parser::AST::Processor. Override methods to perform custom processing. Remember to call ‘super` if you want to recursively process descendant nodes.
Constant Summary collapse
- NO_CHILD_NODES =
[:true, :false, :nil, :int, :float, :complex, :rational, :str, :sym, :regopt, :self, :lvar, :ivar, :cvar, :gvar, :nth_ref, :back_ref, :cbase, :arg, :restarg, :blockarg, :shadowarg, :kwrestarg, :zsuper, :lambda, :redo, :retry].freeze
- ONE_CHILD_NODE =
[:splat, :kwsplat, :block_pass, :not, :break, :next, :return, :preexe, :postexe, :match_current_line, :defined?, :arg_expr].freeze
- MANY_CHILD_NODES =
[:dstr, :dsym, :xstr, :regexp, :array, :hash, :pair, :irange, :erange, :mlhs, :masgn, :or_asgn, :and_asgn, :undef, :alias, :args, :super, :yield, :or, :and, :while_post, :until_post, :iflipflop, :eflipflop, :match_with_lvasgn, :begin, :kwbegin].freeze
- SECOND_CHILD_ONLY =
[:lvasgn, :ivasgn, :cvasgn, :gvasgn, :optarg, :kwarg, :kwoptarg].freeze
Instance Method Summary collapse
- #on_block(node) ⇒ Object
- #on_case(node) ⇒ Object (also: #on_rescue, #on_resbody, #on_ensure, #on_for)
- #on_casgn(node) ⇒ Object
- #on_class(node) ⇒ Object
- #on_const(node) ⇒ Object
- #on_def(node) ⇒ Object
- #on_defs(node) ⇒ Object
- #on_if(node) ⇒ Object
- #on_op_asgn(node) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_csend)
- #on_while(node) ⇒ Object (also: #on_until, #on_when, #on_module, #on_sclass)
- #walk(node) ⇒ Object
Instance Method Details
#on_block(node) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/rubocop/ast_node/traversal.rb', line 150 def on_block(node) children = node.children child = children[0] send(:"on_#{child.type}", child) # can be send, zsuper... on_args(children[1]) return unless (child = children[2]) send(:"on_#{child.type}", child) end |
#on_case(node) ⇒ Object Also known as: on_rescue, on_resbody, on_ensure, on_for
159 160 161 162 163 164 |
# File 'lib/rubocop/ast_node/traversal.rb', line 159 def on_case(node) node.children.each do |child| send(:"on_#{child.type}", child) if child end nil end |
#on_casgn(node) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/rubocop/ast_node/traversal.rb', line 72 def on_casgn(node) children = node.children if (child = children[0]) # always const??? send(:"on_#{child.type}", child) end return unless (child = children[2]) send(:"on_#{child.type}", child) end |
#on_class(node) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rubocop/ast_node/traversal.rb', line 81 def on_class(node) children = node.children child = children[0] # always const??? send(:"on_#{child.type}", child) if (child = children[1]) send(:"on_#{child.type}", child) end return unless (child = children[2]) send(:"on_#{child.type}", child) end |
#on_const(node) ⇒ Object
67 68 69 70 |
# File 'lib/rubocop/ast_node/traversal.rb', line 67 def on_const(node) return unless (child = node.children[0]) send(:"on_#{child.type}", child) end |
#on_def(node) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/rubocop/ast_node/traversal.rb', line 92 def on_def(node) children = node.children on_args(children[1]) return unless (child = children[2]) send(:"on_#{child.type}", child) end |
#on_defs(node) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/rubocop/ast_node/traversal.rb', line 117 def on_defs(node) children = node.children child = children[0] send(:"on_#{child.type}", child) on_args(children[2]) return unless (child = children[3]) send(:"on_#{child.type}", child) end |
#on_if(node) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rubocop/ast_node/traversal.rb', line 126 def on_if(node) children = node.children child = children[0] send(:"on_#{child.type}", child) if (child = children[1]) send(:"on_#{child.type}", child) end return unless (child = children[2]) send(:"on_#{child.type}", child) end |
#on_op_asgn(node) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/rubocop/ast_node/traversal.rb', line 109 def on_op_asgn(node) children = node.children child = children[0] send(:"on_#{child.type}", child) child = children[2] send(:"on_#{child.type}", child) end |
#on_send(node) ⇒ Object Also known as: on_csend
99 100 101 102 103 104 105 |
# File 'lib/rubocop/ast_node/traversal.rb', line 99 def on_send(node) node.children.each_with_index do |child, i| next if i == 1 send(:"on_#{child.type}", child) if child end nil end |
#on_while(node) ⇒ Object Also known as: on_until, on_when, on_module, on_sclass
137 138 139 140 141 142 143 |
# File 'lib/rubocop/ast_node/traversal.rb', line 137 def on_while(node) children = node.children child = children[0] send(:"on_#{child.type}", child) return unless (child = children[1]) send(:"on_#{child.type}", child) end |
#walk(node) ⇒ Object
11 12 13 14 15 |
# File 'lib/rubocop/ast_node/traversal.rb', line 11 def walk(node) return if node.nil? send(:"on_#{node.type}", node) nil end |