Class: RuboCop::Node
- Inherits:
-
Parser::AST::Node
- Object
- Parser::AST::Node
- RuboCop::Node
- Includes:
- Sexp
- Defined in:
- lib/rubocop/ast_node.rb,
lib/rubocop/ast_node/builder.rb,
lib/rubocop/ast_node/traversal.rb
Overview
‘RuboCop::Node` is a subclass of `Parser::AST::Node`. It provides access to parent nodes and an object-oriented way to traverse an AST with the power of `Enumerable`.
It has predicate methods for every node type, like this:
Defined Under Namespace
Modules: Traversal Classes: Builder
Constant Summary collapse
- COMPARISON_OPERATORS =
[:!, :==, :===, :!=, :<=, :>=, :>, :<, :<=>].freeze
- TRUTHY_LITERALS =
[:str, :dstr, :xstr, :int, :float, :sym, :dsym, :array, :hash, :regexp, :true, :irange, :erange, :complex, :rational, :regopt].freeze
- FALSEY_LITERALS =
[:false, :nil].freeze
- LITERALS =
(TRUTHY_LITERALS + FALSEY_LITERALS).freeze
- COMPOSITE_LITERALS =
[:dstr, :xstr, :dsym, :array, :hash, :irange, :erange, :regexp].freeze
- BASIC_LITERALS =
(LITERALS - COMPOSITE_LITERALS).freeze
- MUTABLE_LITERALS =
[:str, :dstr, :xstr, :array, :hash].freeze
- IMMUTABLE_LITERALS =
(LITERALS - MUTABLE_LITERALS).freeze
- VARIABLES =
[:ivar, :gvar, :cvar, :lvar].freeze
- REFERENCES =
[:nth_ref, :back_ref].freeze
- KEYWORDS =
[:alias, :and, :break, :case, :class, :def, :defs, :defined?, :kwbegin, :do, :else, :ensure, :for, :if, :module, :next, :not, :or, :postexe, :redo, :rescue, :retry, :return, :self, :super, :zsuper, :then, :undef, :until, :when, :while, :yield].freeze
- OPERATOR_KEYWORDS =
[:and, :or].freeze
- SPECIAL_KEYWORDS =
%w(__FILE__ __LINE__ __ENCODING__).freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#ancestors ⇒ Array<Node>
Returns an array of ancestor nodes.
- #asgn_method_call? ⇒ Boolean
- #basic_literal? ⇒ Boolean
- #binary_operation? ⇒ Boolean
- #chained? ⇒ Boolean
-
#child_nodes ⇒ Array<Node>
Returns an array of child nodes.
- #complete! ⇒ Object
- #complete? ⇒ Boolean
- #const_name ⇒ Object
- #defined_module ⇒ Object
- #defined_module_name ⇒ Object
-
#descendants ⇒ Array<Node>
Returns an array of descendant nodes.
-
#each_ancestor(*types) {|node| ... } ⇒ self, Enumerator
Calls the given block for each ancestor node from parent to root.
-
#each_child_node(*types) {|node| ... } ⇒ self, Enumerator
Calls the given block for each child node.
-
#each_descendant(*types) {|node| ... } ⇒ self, Enumerator
Calls the given block for each descendant node with depth first order.
-
#each_node(*types) {|node| ... } ⇒ self, Enumerator
Calls the given block for the receiver and each descendant node in depth-first order.
- #falsey_literal? ⇒ Boolean
- #immutable_literal? ⇒ Boolean
-
#initialize(type, children = [], properties = {}) ⇒ Node
constructor
A new instance of Node.
- #keyword? ⇒ Boolean
- #keyword_bang? ⇒ Boolean
- #keyword_not? ⇒ Boolean
- #literal? ⇒ Boolean
-
#modifier_form? ⇒ Boolean
Predicates.
- #multiline? ⇒ Boolean
- #mutable_literal? ⇒ Boolean
-
#parent ⇒ Node?
Returns the parent node, or ‘nil` if the receiver is a root node.
-
#parent_module_name ⇒ Object
Searching the AST.
-
#pure? ⇒ Boolean
Some expressions are evaluated for their value, some for their side effects, and some for both If we know that expressions are useful only for their return values, and have no side effects, that means we can reorder them, change the number of times they are evaluated, or replace them with other expressions which are equivalent in value So, is evaluation of this node free of side effects?.
-
#receiver ⇒ Object
Destructuring.
- #reference? ⇒ Boolean
-
#sibling_index ⇒ Integer
Returns the index of the receiver node in its siblings.
- #single_line? ⇒ Boolean
- #source ⇒ Object
- #source_range ⇒ Object
- #special_keyword? ⇒ Boolean
- #truthy_literal? ⇒ Boolean
- #unary_operation? ⇒ Boolean
-
#updated(type = nil, children = nil, properties = {}) ⇒ Object
Override ‘AST::Node#updated` so that `AST::Processor` does not try to mutate our ASTs.
-
#value_used? ⇒ Boolean
Some expressions are evaluated for their value, some for their side effects, and some for both If we know that an expression is useful only for its side effects, that means we can transform it in ways which preserve the side effects, but change the return value So, does the return value of this node matter? If we changed it to ‘(…; nil)`, might that affect anything?.
- #variable? ⇒ Boolean
Methods included from Sexp
Constructor Details
#initialize(type, children = [], properties = {}) ⇒ Node
Returns a new instance of Node.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rubocop/ast_node.rb', line 60 def initialize(type, children = [], properties = {}) @mutable_attributes = {} # ::AST::Node#initialize freezes itself. super # #parent= may be invoked multiple times for a node because there are # pending nodes while constructing AST and they are replaced later. # For example, `lvar` and `send` type nodes are initially created as an # `ident` type node and fixed to the appropriate type later. # So, the #parent attribute needs to be mutable. each_child_node do |child_node| child_node.parent = self unless child_node.complete? end end |
Class Method Details
.def_matcher(method_name, pattern_str) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/rubocop/ast_node.rb', line 48 def def_matcher(method_name, pattern_str) compiler = RuboCop::NodePattern::Compiler.new(pattern_str, 'self') src = "def #{method_name}(" \ "#{compiler.emit_param_list});" \ "#{compiler.emit_method_code};end" file, lineno = *caller.first.split(':') class_eval(src, file, lineno.to_i) end |
Instance Method Details
#ancestors ⇒ Array<Node>
Returns an array of ancestor nodes. This is a shorthand for ‘node.each_ancestor.to_a`.
158 159 160 |
# File 'lib/rubocop/ast_node.rb', line 158 def ancestors each_ancestor.to_a end |
#asgn_method_call? ⇒ Boolean
347 348 349 350 |
# File 'lib/rubocop/ast_node.rb', line 347 def asgn_method_call? !COMPARISON_OPERATORS.include?(method_name) && method_name.to_s.end_with?('='.freeze) end |
#basic_literal? ⇒ Boolean
360 361 362 |
# File 'lib/rubocop/ast_node.rb', line 360 def basic_literal? BASIC_LITERALS.include?(type) end |
#binary_operation? ⇒ Boolean
433 434 435 436 437 |
# File 'lib/rubocop/ast_node.rb', line 433 def binary_operation? return false unless loc.respond_to?(:selector) && loc.selector Cop::Util.operator?(method_name) && source_range.begin_pos != loc.selector.begin_pos end |
#chained? ⇒ Boolean
439 440 441 442 443 |
# File 'lib/rubocop/ast_node.rb', line 439 def chained? return false if parent.nil? || !parent.send_type? receiver, _method_name, *_args = *parent equal?(receiver) end |
#child_nodes ⇒ Array<Node>
Returns an array of child nodes. This is a shorthand for ‘node.each_child_node.to_a`.
198 199 200 |
# File 'lib/rubocop/ast_node.rb', line 198 def child_nodes each_child_node.to_a end |
#complete! ⇒ Object
94 95 96 97 |
# File 'lib/rubocop/ast_node.rb', line 94 def complete! @mutable_attributes.freeze each_child_node(&:complete!) end |
#complete? ⇒ Boolean
99 100 101 |
# File 'lib/rubocop/ast_node.rb', line 99 def complete? @mutable_attributes.frozen? end |
#const_name ⇒ Object
293 294 295 296 297 298 299 300 301 |
# File 'lib/rubocop/ast_node.rb', line 293 def const_name return unless const_type? namespace, name = *self if namespace && !namespace.cbase_type? "#{namespace.const_name}::#{name}" else name.to_s end end |
#defined_module ⇒ Object
311 312 313 314 |
# File 'lib/rubocop/ast_node.rb', line 311 def defined_module namespace, name = *defined_module0 s(:const, namespace, name) if name end |
#defined_module_name ⇒ Object
316 317 318 |
# File 'lib/rubocop/ast_node.rb', line 316 def defined_module_name (const = defined_module) && const.const_name end |
#descendants ⇒ Array<Node>
Returns an array of descendant nodes. This is a shorthand for ‘node.each_descendant.to_a`.
236 237 238 |
# File 'lib/rubocop/ast_node.rb', line 236 def descendants each_descendant.to_a end |
#each_ancestor ⇒ self, Enumerator #each_ancestor(type) ⇒ self, Enumerator #each_ancestor(type_a, type_b, ...) ⇒ self, Enumerator #each_ancestor(types) ⇒ self, Enumerator
Calls the given block for each ancestor node from parent to root. If no block is given, an ‘Enumerator` is returned.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rubocop/ast_node.rb', line 142 def each_ancestor(*types, &block) return to_enum(__method__, *types) unless block_given? if types.empty? visit_ancestors(&block) else visit_ancestors_with_types(types, &block) end self end |
#each_child_node ⇒ self, Enumerator #each_child_node(type) ⇒ self, Enumerator #each_child_node(type_a, type_b, ...) ⇒ self, Enumerator #each_child_node(types) ⇒ self, Enumerator
Calls the given block for each child node. If no block is given, an ‘Enumerator` is returned.
Note that this is different from ‘node.children.each { |child| … }` which yields all children including non-node elements.
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/rubocop/ast_node.rb', line 183 def each_child_node(*types) return to_enum(__method__, *types) unless block_given? children.each do |child| next unless child.is_a?(Node) yield child if types.empty? || types.include?(child.type) end self end |
#each_descendant ⇒ self, Enumerator #each_descendant(type) ⇒ self, Enumerator #each_descendant(type_a, type_b, ...) ⇒ self, Enumerator #each_descendant(types) ⇒ self, Enumerator
Calls the given block for each descendant node with depth first order. If no block is given, an ‘Enumerator` is returned.
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/rubocop/ast_node.rb', line 220 def each_descendant(*types, &block) return to_enum(__method__, *types) unless block_given? if types.empty? visit_descendants(&block) else visit_descendants_with_types(types, &block) end self end |
#each_node ⇒ self, Enumerator #each_node(type) ⇒ self, Enumerator #each_node(type_a, type_b, ...) ⇒ self, Enumerator #each_node(types) ⇒ self, Enumerator
Calls the given block for the receiver and each descendant node in depth-first order. If no block is given, an ‘Enumerator` is returned.
This method would be useful when you treat the receiver node as the root of a tree and want to iterate over all nodes in the tree.
262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/rubocop/ast_node.rb', line 262 def each_node(*types, &block) return to_enum(__method__, *types) unless block_given? yield self if types.empty? || types.include?(type) if types.empty? visit_descendants(&block) else visit_descendants_with_types(types, &block) end self end |
#falsey_literal? ⇒ Boolean
368 369 370 |
# File 'lib/rubocop/ast_node.rb', line 368 def falsey_literal? FALSEY_LITERALS.include?(type) end |
#immutable_literal? ⇒ Boolean
376 377 378 |
# File 'lib/rubocop/ast_node.rb', line 376 def immutable_literal? IMMUTABLE_LITERALS.include?(type) end |
#keyword? ⇒ Boolean
406 407 408 409 410 411 |
# File 'lib/rubocop/ast_node.rb', line 406 def keyword? return true if special_keyword? || keyword_not? return false unless KEYWORDS.include?(type) !OPERATOR_KEYWORDS.include?(type) || loc.operator.is?(type.to_s) end |
#keyword_bang? ⇒ Boolean
422 423 424 425 |
# File 'lib/rubocop/ast_node.rb', line 422 def keyword_bang? _receiver, method_name, *args = *self args.empty? && method_name == :! && loc.selector.is?('!'.freeze) end |
#keyword_not? ⇒ Boolean
417 418 419 420 |
# File 'lib/rubocop/ast_node.rb', line 417 def keyword_not? _receiver, method_name, *args = *self args.empty? && method_name == :! && loc.selector.is?('not'.freeze) end |
#literal? ⇒ Boolean
356 357 358 |
# File 'lib/rubocop/ast_node.rb', line 356 def literal? LITERALS.include?(type) end |
#modifier_form? ⇒ Boolean
Predicates
334 335 336 |
# File 'lib/rubocop/ast_node.rb', line 334 def modifier_form? loc.respond_to?(:end) && loc.end.nil? end |
#multiline? ⇒ Boolean
338 339 340 341 |
# File 'lib/rubocop/ast_node.rb', line 338 def multiline? expr = loc.expression expr && (expr.first_line != expr.last_line) end |
#mutable_literal? ⇒ Boolean
372 373 374 |
# File 'lib/rubocop/ast_node.rb', line 372 def mutable_literal? MUTABLE_LITERALS.include?(type) end |
#parent ⇒ Node?
Returns the parent node, or ‘nil` if the receiver is a root node.
86 87 88 |
# File 'lib/rubocop/ast_node.rb', line 86 def parent @mutable_attributes[:parent] end |
#parent_module_name ⇒ Object
Searching the AST
322 323 324 325 326 327 328 329 330 |
# File 'lib/rubocop/ast_node.rb', line 322 def parent_module_name # what class or module is this method/constant/etc definition in? # returns nil if answer cannot be determined ancestors = each_ancestor(:class, :module, :sclass, :casgn, :block) result = ancestors.map do |ancestor| parent_module_name_part(ancestor) { |full_name| return full_name } end.compact.reverse.join('::') result.empty? ? 'Object' : result end |
#pure? ⇒ Boolean
Some expressions are evaluated for their value, some for their side effects, and some for both If we know that expressions are useful only for their return values, and have no side effects, that means we can reorder them, change the number of times they are evaluated, or replace them with other expressions which are equivalent in value So, is evaluation of this node free of side effects?
503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/rubocop/ast_node.rb', line 503 def pure? # Be conservative and return false if we're not sure case type when :__FILE__, :__LINE__, :const, :cvar, :defined?, :false, :float, :gvar, :int, :ivar, :lvar, :nil, :str, :sym, :true, :regopt true when :and, :array, :begin, :case, :dstr, :dsym, :eflipflop, :ensure, :erange, :for, :hash, :if, :iflipflop, :irange, :kwbegin, :not, :or, :pair, :regexp, :until, :until_post, :when, :while, :while_post child_nodes.all?(&:pure?) else false end end |
#receiver ⇒ Object
Destructuring
286 |
# File 'lib/rubocop/ast_node.rb', line 286 def_matcher :receiver, '{(send $_ ...) (block (send $_ ...) ...)}' |
#reference? ⇒ Boolean
402 403 404 |
# File 'lib/rubocop/ast_node.rb', line 402 def reference? REFERENCES.include?(type) end |
#sibling_index ⇒ Integer
Returns the index of the receiver node in its siblings. (Sibling index uses zero based numbering.)
120 121 122 |
# File 'lib/rubocop/ast_node.rb', line 120 def sibling_index parent.children.index { |sibling| sibling.equal?(self) } end |
#single_line? ⇒ Boolean
343 344 345 |
# File 'lib/rubocop/ast_node.rb', line 343 def single_line? !multiline? end |
#source ⇒ Object
276 277 278 |
# File 'lib/rubocop/ast_node.rb', line 276 def source loc.expression.source end |
#source_range ⇒ Object
280 281 282 |
# File 'lib/rubocop/ast_node.rb', line 280 def source_range loc.expression end |
#special_keyword? ⇒ Boolean
413 414 415 |
# File 'lib/rubocop/ast_node.rb', line 413 def special_keyword? SPECIAL_KEYWORDS.include?(source) end |
#truthy_literal? ⇒ Boolean
364 365 366 |
# File 'lib/rubocop/ast_node.rb', line 364 def truthy_literal? TRUTHY_LITERALS.include?(type) end |
#unary_operation? ⇒ Boolean
427 428 429 430 431 |
# File 'lib/rubocop/ast_node.rb', line 427 def unary_operation? return false unless loc.respond_to?(:selector) && loc.selector Cop::Util.operator?(loc.selector.source.to_sym) && source_range.begin_pos == loc.selector.begin_pos end |
#updated(type = nil, children = nil, properties = {}) ⇒ Object
Override ‘AST::Node#updated` so that `AST::Processor` does not try to mutate our ASTs. Since we keep references from children to parents and not just the other way around, we cannot update an AST and share identical subtrees. Rather, the entire AST must be copied any time any part of it is changed.
111 112 113 114 |
# File 'lib/rubocop/ast_node.rb', line 111 def updated(type = nil, children = nil, properties = {}) properties[:location] ||= @location Node.new(type || @type, children || @children, properties) end |
#value_used? ⇒ Boolean
Some expressions are evaluated for their value, some for their side effects, and some for both If we know that an expression is useful only for its side effects, that means we can transform it in ways which preserve the side effects, but change the return value So, does the return value of this node matter? If we changed it to ‘(…; nil)`, might that affect anything?
rubocop:disable Metrics/MethodLength
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/rubocop/ast_node.rb', line 472 def value_used? # Be conservative and return true if we're not sure return false if parent.nil? case parent.type when :array, :defined?, :dstr, :dsym, :eflipflop, :erange, :float, :hash, :iflipflop, :irange, :not, :pair, :regexp, :str, :sym, :when, :xstr parent.value_used? when :begin, :kwbegin begin_value_used? when :for for_value_used? when :case, :if case_if_value_used? when :while, :until, :while_post, :until_post while_until_value_used? else true end end |
#variable? ⇒ Boolean
398 399 400 |
# File 'lib/rubocop/ast_node.rb', line 398 def variable? VARIABLES.include?(type) end |