Class: RuboCop::AST::Node
- Inherits:
-
Parser::AST::Node
- Object
- Parser::AST::Node
- RuboCop::AST::Node
- Extended by:
- RuboCop::AST::NodePattern::Macros
- Includes:
- Descendence, Sexp
- Defined in:
- lib/rubocop/ast/node.rb
Overview
RuboCop::AST::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:
Direct Known Subclasses
AliasNode, AndNode, ArgNode, ArgsNode, ArrayNode, AsgnNode, BlockNode, BreakNode, CaseMatchNode, CaseNode, CasgnNode, ClassNode, ComplexNode, ConstNode, DefNode, DefinedNode, EnsureNode, FloatNode, ForNode, ForwardArgsNode, HashNode, IfNode, InPatternNode, IndexNode, IndexasgnNode, IntNode, KeywordBeginNode, KeywordSplatNode, LambdaNode, MasgnNode, MlhsNode, ModuleNode, NextNode, RuboCop::AST::NodePattern::Node::AnyOrder, RuboCop::AST::NodePattern::Node::Capture, RuboCop::AST::NodePattern::Node::Predicate, RuboCop::AST::NodePattern::Node::Repetition, RuboCop::AST::NodePattern::Node::Rest, RuboCop::AST::NodePattern::Node::Sequence, RuboCop::AST::NodePattern::Node::Subsequence, RuboCop::AST::NodePattern::Node::Union, OpAsgnNode, OrNode, PairNode, RangeNode, RationalNode, RegexpNode, ResbodyNode, RescueNode, ReturnNode, SelfClassNode, SendNode, StrNode, SuperNode, SymbolNode, UntilNode, VarNode, WhenNode, WhileNode, YieldNode
Constant Summary collapse
- COMPARISON_OPERATORS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
<=> isn't included here, because it doesn't return a boolean.
%i[== === != <= >= > <].to_set.freeze
- TRUTHY_LITERALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[str dstr xstr int float sym dsym array hash regexp true irange erange complex rational regopt].to_set.freeze
- FALSEY_LITERALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[false nil].to_set.freeze
- LITERALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
(TRUTHY_LITERALS + FALSEY_LITERALS).freeze
- COMPOSITE_LITERALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[dstr xstr dsym array hash irange erange regexp].to_set.freeze
- BASIC_LITERALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
(LITERALS - COMPOSITE_LITERALS).freeze
- MUTABLE_LITERALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[str dstr xstr array hash regexp irange erange].to_set.freeze
- IMMUTABLE_LITERALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
(LITERALS - MUTABLE_LITERALS).freeze
- EQUALS_ASSIGNMENTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[lvasgn ivasgn cvasgn gvasgn casgn masgn].to_set.freeze
- SHORTHAND_ASSIGNMENTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[op_asgn or_asgn and_asgn].to_set.freeze
- ASSIGNMENTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
(EQUALS_ASSIGNMENTS + SHORTHAND_ASSIGNMENTS).freeze
- BASIC_CONDITIONALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[if while until].to_set.freeze
- CONDITIONALS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
(BASIC_CONDITIONALS + %i[case case_match]).freeze
- POST_CONDITION_LOOP_TYPES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[while_post until_post].to_set.freeze
- LOOP_TYPES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
(POST_CONDITION_LOOP_TYPES + %i[while until for]).freeze
- VARIABLES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[ivar gvar cvar lvar].to_set.freeze
- REFERENCES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[nth_ref back_ref].to_set.freeze
- KEYWORDS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[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].to_set.freeze
- OPERATOR_KEYWORDS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%i[and or].to_set.freeze
- SPECIAL_KEYWORDS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
%w[__FILE__ __LINE__ __ENCODING__].to_set.freeze
Instance Method Summary collapse
-
#ancestors ⇒ Array<Node>
Returns an array of ancestor nodes.
- #any_block_type? ⇒ Boolean
- #any_def_type? ⇒ Boolean
- #any_match_pattern_type? ⇒ Boolean
- #any_str_type? ⇒ Boolean
- #any_sym_type? ⇒ Boolean
- #argument? ⇒ Boolean
- #argument_type? ⇒ Boolean
- #assignment? ⇒ Boolean
-
#assignment_or_similar?(node = self) ⇒ Object
Some cops treat the shovel operator as a kind of assignment.
- #basic_conditional? ⇒ Boolean
- #basic_literal? ⇒ Boolean
- #boolean_type? ⇒ Boolean
- #call_type? ⇒ Boolean
- #chained? ⇒ Boolean
- #class_constructor?(node = self) ⇒ Object
- #class_definition?(node = self) ⇒ Object
- #complete! ⇒ Object
- #complete? ⇒ Boolean
- #conditional? ⇒ Boolean
- #const_name ⇒ Object
- #defined_module ⇒ Object
- #defined_module_name ⇒ Object
-
#each_ancestor(*types) {|node| ... } ⇒ self, Enumerator
Calls the given block for each ancestor node from parent to root.
- #empty_source? ⇒ Boolean
- #equals_asgn? ⇒ Boolean
- #falsey_literal? ⇒ Boolean
- #first_line ⇒ Object
- #global_const?(node = self, name) ⇒ Object
- #guard_clause? ⇒ Boolean
- #immutable_literal? ⇒ Boolean
-
#initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_PROPERTIES) ⇒ Node
constructor
A new instance of Node.
- #keyword? ⇒ Boolean
- #lambda?(node = self) ⇒ Object
- #lambda_or_proc?(node = self) ⇒ Object
- #last_line ⇒ Object
-
#left_sibling ⇒ Node?
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.
-
#left_siblings ⇒ Array<Node>
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.
- #line_count ⇒ Object
- #literal? ⇒ Boolean
-
#loc?(which_loc) ⇒ Boolean
Shortcut to safely check if a location is present.
-
#loc_is?(which_loc, str) ⇒ Boolean
Shortcut to safely test a particular location, even if this location does not exist or is
nil. -
#loop_keyword? ⇒ Boolean
NOTE:
loop { }is a normal method call and thus not a loop keyword. - #match_guard_clause?(node = self) ⇒ Object
- #module_definition?(node = self) ⇒ Object
-
#multiline? ⇒ Boolean
Predicates.
- #mutable_literal? ⇒ Boolean
- #nonempty_line_count ⇒ Object
- #numeric_type? ⇒ Boolean
- #operator_keyword? ⇒ Boolean
-
#parent ⇒ Node?
Returns the parent node, or
nilif the receiver is a root node. - #parent? ⇒ Boolean
-
#parent_module_name ⇒ Object
Searching the AST.
- #parenthesized_call? ⇒ Boolean
- #post_condition_loop? ⇒ Boolean
- #proc?(node = self) ⇒ Object
-
#pure? ⇒ Boolean
Some expressions are evaluated for their value, some for their side effects, and some for both.
- #range_type? ⇒ Boolean
- #receiver(node = self) ⇒ Object
- #recursive_basic_literal? ⇒ Boolean
- #recursive_literal? ⇒ Boolean
- #reference? ⇒ Boolean
-
#right_sibling ⇒ Node?
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.
-
#right_siblings ⇒ Array<Node>
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms.
- #root? ⇒ Boolean
-
#send_type? ⇒ Boolean
Most nodes are of 'send' type, so this method is defined separately to make this check as fast as possible.
- #shorthand_asgn? ⇒ Boolean
-
#sibling_index ⇒ Integer?
Returns the index of the receiver node in its siblings.
- #single_line? ⇒ Boolean
-
#source ⇒ String?
NOTE: Some rare nodes may have no source, like
s(:args)infoo {}. - #source_length ⇒ Object
- #source_range ⇒ Object
- #special_keyword? ⇒ Boolean
- #str_content(node = self) ⇒ Object
-
#struct_constructor?(node = self) ⇒ Object
deprecated
Deprecated.
Use
:class_constructor? - #truthy_literal? ⇒ Boolean
-
#type?(*types) ⇒ Boolean
Determine if the node is one of several node types in a single query Allows specific single node types, as well as "grouped" types (e.g.
:booleanfor:trueor:false). -
#type_in?(types) ⇒ Boolean
private
Non-splatting variant of
type?, used by the traversal hot paths to avoid allocating an array per visited node. -
#updated(type = nil, children = nil, properties = {}) ⇒ Object
Override
AST::Node#updatedso thatAST::Processordoes 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 RuboCop::AST::NodePattern::Macros
def_node_matcher, def_node_search
Methods included from Descendence
#child_nodes, #descendants, #each_child_node, #each_descendant, #each_node
Methods included from Sexp
Constructor Details
#initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_PROPERTIES) ⇒ Node
Returns a new instance of Node.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rubocop/ast/node.rb', line 162 def initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_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 |
Instance Method Details
#ancestors ⇒ Array<Node>
Returns an array of ancestor nodes.
This is a shorthand for node.each_ancestor.to_a.
334 335 336 |
# File 'lib/rubocop/ast/node.rb', line 334 def ancestors each_ancestor.to_a end |
#any_block_type? ⇒ Boolean
565 566 567 |
# File 'lib/rubocop/ast/node.rb', line 565 def any_block_type? GROUP_FOR_TYPE[type] == :any_block end |
#any_def_type? ⇒ Boolean
545 546 547 |
# File 'lib/rubocop/ast/node.rb', line 545 def any_def_type? GROUP_FOR_TYPE[type] == :any_def end |
#any_match_pattern_type? ⇒ Boolean
569 570 571 |
# File 'lib/rubocop/ast/node.rb', line 569 def any_match_pattern_type? GROUP_FOR_TYPE[type] == :any_match_pattern end |
#any_str_type? ⇒ Boolean
573 574 575 |
# File 'lib/rubocop/ast/node.rb', line 573 def any_str_type? GROUP_FOR_TYPE[type] == :any_str end |
#any_sym_type? ⇒ Boolean
577 578 579 |
# File 'lib/rubocop/ast/node.rb', line 577 def any_sym_type? GROUP_FOR_TYPE[type] == :any_sym end |
#argument? ⇒ Boolean
539 540 541 542 543 |
# File 'lib/rubocop/ast/node.rb', line 539 def argument? return false unless parent&.send_type? parent.arguments.any? { |argument| argument.equal?(self) } end |
#argument_type? ⇒ Boolean
549 550 551 |
# File 'lib/rubocop/ast/node.rb', line 549 def argument_type? GROUP_FOR_TYPE[type] == :argument end |
#assignment? ⇒ Boolean
491 492 493 |
# File 'lib/rubocop/ast/node.rb', line 491 def assignment? ASSIGNMENTS.include?(type) end |
#assignment_or_similar?(node = self) ⇒ Object
Some cops treat the shovel operator as a kind of assignment.
441 442 443 |
# File 'lib/rubocop/ast/node.rb', line 441 def_node_matcher :assignment_or_similar?, <<~PATTERN {assignment? (send _recv :<< ...)} PATTERN |
#basic_conditional? ⇒ Boolean
495 496 497 |
# File 'lib/rubocop/ast/node.rb', line 495 def basic_conditional? BASIC_CONDITIONALS.include?(type) end |
#basic_literal? ⇒ Boolean
449 450 451 |
# File 'lib/rubocop/ast/node.rb', line 449 def basic_literal? BASIC_LITERALS.include?(type) end |
#boolean_type? ⇒ Boolean
553 554 555 |
# File 'lib/rubocop/ast/node.rb', line 553 def boolean_type? GROUP_FOR_TYPE[type] == :boolean end |
#call_type? ⇒ Boolean
531 532 533 |
# File 'lib/rubocop/ast/node.rb', line 531 def call_type? GROUP_FOR_TYPE[type] == :call end |
#chained? ⇒ Boolean
535 536 537 |
# File 'lib/rubocop/ast/node.rb', line 535 def chained? parent&.call_type? && equal?(parent.receiver) end |
#class_constructor?(node = self) ⇒ Object
625 626 627 628 629 630 631 632 633 634 |
# File 'lib/rubocop/ast/node.rb', line 625 def_node_matcher :class_constructor?, <<~PATTERN { (send #global_const?({:Class :Module :Struct}) :new ...) (send #global_const?(:Data) :define ...) (any_block { (send #global_const?({:Class :Module :Struct}) :new ...) (send #global_const?(:Data) :define ...) } ...) } PATTERN |
#class_definition?(node = self) ⇒ Object
643 644 645 646 647 |
# File 'lib/rubocop/ast/node.rb', line 643 def_node_matcher :class_definition?, <<~PATTERN {(class _ _ $_) (sclass _ $_) (any_block (send #global_const?({:Struct :Class}) :new ...) _ $_)} PATTERN |
#complete! ⇒ Object
231 232 233 234 |
# File 'lib/rubocop/ast/node.rb', line 231 def complete! @mutable_attributes.freeze each_child_node(&:complete!) end |
#complete? ⇒ Boolean
236 237 238 |
# File 'lib/rubocop/ast/node.rb', line 236 def complete? @mutable_attributes.frozen? end |
#conditional? ⇒ Boolean
499 500 501 |
# File 'lib/rubocop/ast/node.rb', line 499 def conditional? CONDITIONALS.include?(type) end |
#const_name ⇒ Object
380 381 382 383 384 385 386 387 388 |
# File 'lib/rubocop/ast/node.rb', line 380 def const_name return unless const_type? || casgn_type? if namespace && !namespace.cbase_type? "#{namespace.const_name}::#{short_name}" else short_name.to_s end end |
#defined_module ⇒ Object
400 401 402 403 |
# File 'lib/rubocop/ast/node.rb', line 400 def defined_module namespace, name = *defined_module0 s(:const, namespace, name) if name end |
#defined_module_name ⇒ Object
405 406 407 |
# File 'lib/rubocop/ast/node.rb', line 405 def defined_module_name (const = defined_module) && const.const_name end |
#each_ancestor ⇒ self, Enumerator #each_ancestor(type) ⇒ self, Enumerator #each_ancestor(type_a, type_b, ...) ⇒ self, Enumerator
Calls the given block for each ancestor node from parent to root.
If no block is given, an Enumerator is returned.
322 323 324 325 326 327 328 |
# File 'lib/rubocop/ast/node.rb', line 322 def each_ancestor(*types, &block) return to_enum(__method__, *types) unless block visit_ancestors(types, &block) self end |
#empty_source? ⇒ Boolean
435 436 437 |
# File 'lib/rubocop/ast/node.rb', line 435 def empty_source? source_length.zero? end |
#equals_asgn? ⇒ Boolean
483 484 485 |
# File 'lib/rubocop/ast/node.rb', line 483 def equals_asgn? EQUALS_ASSIGNMENTS.include?(type) end |
#falsey_literal? ⇒ Boolean
457 458 459 |
# File 'lib/rubocop/ast/node.rb', line 457 def falsey_literal? FALSEY_LITERALS.include?(type) end |
#first_line ⇒ Object
348 349 350 |
# File 'lib/rubocop/ast/node.rb', line 348 def first_line loc.line end |
#global_const?(node = self, name) ⇒ Object
622 |
# File 'lib/rubocop/ast/node.rb', line 622 def_node_matcher :global_const?, '(const {nil? cbase} %1)' |
#guard_clause? ⇒ Boolean
581 582 583 584 585 |
# File 'lib/rubocop/ast/node.rb', line 581 def guard_clause? node = operator_keyword? ? rhs : self node.match_guard_clause? end |
#immutable_literal? ⇒ Boolean
465 466 467 |
# File 'lib/rubocop/ast/node.rb', line 465 def immutable_literal? IMMUTABLE_LITERALS.include?(type) end |
#keyword? ⇒ Boolean
512 513 514 515 516 517 |
# File 'lib/rubocop/ast/node.rb', line 512 def keyword? return true if special_keyword? || (send_type? && prefix_not?) return false unless KEYWORDS.include?(type) !OPERATOR_KEYWORDS.include?(type) || loc.operator.is?(type.to_s) end |
#lambda?(node = self) ⇒ Object
616 |
# File 'lib/rubocop/ast/node.rb', line 616 def_node_matcher :lambda?, '(any_block (send nil? :lambda) ...)' |
#lambda_or_proc?(node = self) ⇒ Object
619 |
# File 'lib/rubocop/ast/node.rb', line 619 def_node_matcher :lambda_or_proc?, '{lambda? proc?}' |
#last_line ⇒ Object
352 353 354 |
# File 'lib/rubocop/ast/node.rb', line 352 def last_line loc.last_line end |
#left_sibling ⇒ Node?
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms
274 275 276 277 278 279 |
# File 'lib/rubocop/ast/node.rb', line 274 def left_sibling i = sibling_index return if i.nil? || i.zero? parent.children[i - 1].freeze end |
#left_siblings ⇒ Array<Node>
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms
284 285 286 287 288 |
# File 'lib/rubocop/ast/node.rb', line 284 def left_siblings return [].freeze unless parent parent.children[0...sibling_index].freeze end |
#line_count ⇒ Object
356 357 358 359 360 |
# File 'lib/rubocop/ast/node.rb', line 356 def line_count return 0 unless source_range source_range.last_line - source_range.first_line + 1 end |
#literal? ⇒ Boolean
445 446 447 |
# File 'lib/rubocop/ast/node.rb', line 445 def literal? LITERALS.include?(type) end |
#loc?(which_loc) ⇒ Boolean
Shortcut to safely check if a location is present
589 590 591 592 593 |
# File 'lib/rubocop/ast/node.rb', line 589 def loc?(which_loc) return false unless loc.respond_to?(which_loc) !loc.public_send(which_loc).nil? end |
#loc_is?(which_loc, str) ⇒ Boolean
Shortcut to safely test a particular location, even if
this location does not exist or is nil
597 598 599 600 601 |
# File 'lib/rubocop/ast/node.rb', line 597 def loc_is?(which_loc, str) return false unless loc?(which_loc) loc.public_send(which_loc).is?(str) end |
#loop_keyword? ⇒ Boolean
NOTE: loop { } is a normal method call and thus not a loop keyword.
508 509 510 |
# File 'lib/rubocop/ast/node.rb', line 508 def loop_keyword? LOOP_TYPES.include?(type) end |
#match_guard_clause?(node = self) ⇒ Object
604 605 606 |
# File 'lib/rubocop/ast/node.rb', line 604 def_node_matcher :match_guard_clause?, <<~PATTERN [${(send nil? {:raise :fail} ...) return break next} single_line?] PATTERN |
#module_definition?(node = self) ⇒ Object
650 651 652 653 |
# File 'lib/rubocop/ast/node.rb', line 650 def_node_matcher :module_definition?, <<~PATTERN {(module _ $_) (any_block (send #global_const?(:Module) :new ...) _ $_)} PATTERN |
#multiline? ⇒ Boolean
Predicates
427 428 429 |
# File 'lib/rubocop/ast/node.rb', line 427 def multiline? line_count > 1 end |
#mutable_literal? ⇒ Boolean
461 462 463 |
# File 'lib/rubocop/ast/node.rb', line 461 def mutable_literal? MUTABLE_LITERALS.include?(type) end |
#nonempty_line_count ⇒ Object
362 363 364 |
# File 'lib/rubocop/ast/node.rb', line 362 def nonempty_line_count source.lines.grep(/\S/).size end |
#numeric_type? ⇒ Boolean
557 558 559 |
# File 'lib/rubocop/ast/node.rb', line 557 def numeric_type? GROUP_FOR_TYPE[type] == :numeric end |
#operator_keyword? ⇒ Boolean
523 524 525 |
# File 'lib/rubocop/ast/node.rb', line 523 def operator_keyword? OPERATOR_KEYWORDS.include?(type) end |
#parent ⇒ Node?
Returns the parent node, or nil if the receiver is a root node.
213 214 215 |
# File 'lib/rubocop/ast/node.rb', line 213 def parent @mutable_attributes[:parent] end |
#parent? ⇒ Boolean
222 223 224 |
# File 'lib/rubocop/ast/node.rb', line 222 def parent? !!parent end |
#parent_module_name ⇒ Object
Searching the AST
411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/rubocop/ast/node.rb', line 411 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.filter_map do |ancestor| parent_module_name_part(ancestor) do |full_name| return nil unless full_name full_name end end.reverse.join('::') result.empty? ? 'Object' : result end |
#parenthesized_call? ⇒ Boolean
527 528 529 |
# File 'lib/rubocop/ast/node.rb', line 527 def parenthesized_call? loc_is?(:begin, '(') end |
#post_condition_loop? ⇒ Boolean
503 504 505 |
# File 'lib/rubocop/ast/node.rb', line 503 def post_condition_loop? POST_CONDITION_LOOP_TYPES.include?(type) end |
#proc?(node = self) ⇒ Object
609 610 611 612 613 |
# File 'lib/rubocop/ast/node.rb', line 609 def_node_matcher :proc?, <<~PATTERN {(block (send nil? :proc) ...) (block (send #global_const?(:Proc) :new) ...) (send #global_const?(:Proc) :new)} PATTERN |
#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?
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'lib/rubocop/ast/node.rb', line 693 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 |
#range_type? ⇒ Boolean
561 562 563 |
# File 'lib/rubocop/ast/node.rb', line 561 def range_type? GROUP_FOR_TYPE[type] == :range end |
#receiver(node = self) ⇒ Object
373 374 375 |
# File 'lib/rubocop/ast/node.rb', line 373 def_node_matcher :receiver, <<~PATTERN {(send $_ ...) (any_block (call $_ ...) ...)} PATTERN |
#recursive_basic_literal? ⇒ Boolean
473 |
# File 'lib/rubocop/ast/node.rb', line 473 def_recursive_literal_predicate :basic_literal |
#recursive_literal? ⇒ Boolean
472 |
# File 'lib/rubocop/ast/node.rb', line 472 def_recursive_literal_predicate :literal |
#reference? ⇒ Boolean
479 480 481 |
# File 'lib/rubocop/ast/node.rb', line 479 def reference? REFERENCES.include?(type) end |
#right_sibling ⇒ Node?
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms
265 266 267 268 269 |
# File 'lib/rubocop/ast/node.rb', line 265 def right_sibling return unless parent parent.children[sibling_index + 1].freeze end |
#right_siblings ⇒ Array<Node>
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms
293 294 295 296 297 |
# File 'lib/rubocop/ast/node.rb', line 293 def right_siblings return [].freeze unless parent parent.children[(sibling_index + 1)..].freeze end |
#root? ⇒ Boolean
227 228 229 |
# File 'lib/rubocop/ast/node.rb', line 227 def root? !parent end |
#send_type? ⇒ Boolean
Most nodes are of 'send' type, so this method is defined separately to make this check as fast as possible.
206 207 208 |
# File 'lib/rubocop/ast/node.rb', line 206 def send_type? false end |
#shorthand_asgn? ⇒ Boolean
487 488 489 |
# File 'lib/rubocop/ast/node.rb', line 487 def shorthand_asgn? SHORTHAND_ASSIGNMENTS.include?(type) end |
#sibling_index ⇒ Integer?
Returns the index of the receiver node in its siblings. (Sibling index uses zero based numbering.) Use is discouraged, this is a potentially slow method.
258 259 260 |
# File 'lib/rubocop/ast/node.rb', line 258 def sibling_index parent&.children&.index { |sibling| sibling.equal?(self) } end |
#single_line? ⇒ Boolean
431 432 433 |
# File 'lib/rubocop/ast/node.rb', line 431 def single_line? line_count == 1 end |
#source ⇒ String?
NOTE: Some rare nodes may have no source, like s(:args) in foo {}
340 341 342 |
# File 'lib/rubocop/ast/node.rb', line 340 def source loc.expression&.source end |
#source_length ⇒ Object
366 367 368 |
# File 'lib/rubocop/ast/node.rb', line 366 def source_length source_range ? source_range.size : 0 end |
#source_range ⇒ Object
344 345 346 |
# File 'lib/rubocop/ast/node.rb', line 344 def source_range loc.expression end |
#special_keyword? ⇒ Boolean
519 520 521 |
# File 'lib/rubocop/ast/node.rb', line 519 def special_keyword? SPECIAL_KEYWORD_TYPES.include?(type) && SPECIAL_KEYWORDS.include?(source) end |
#str_content(node = self) ⇒ Object
378 |
# File 'lib/rubocop/ast/node.rb', line 378 def_node_matcher :str_content, '(str $_)' |
#struct_constructor?(node = self) ⇒ Object
Use :class_constructor?
638 639 640 |
# File 'lib/rubocop/ast/node.rb', line 638 def_node_matcher :struct_constructor?, <<~PATTERN (any_block (send #global_const?(:Struct) :new ...) _ $_) PATTERN |
#truthy_literal? ⇒ Boolean
453 454 455 |
# File 'lib/rubocop/ast/node.rb', line 453 def truthy_literal? TRUTHY_LITERALS.include?(type) end |
#type?(*types) ⇒ Boolean
Determine if the node is one of several node types in a single query
Allows specific single node types, as well as "grouped" types
(e.g. :boolean for :true or :false)
181 182 183 |
# File 'lib/rubocop/ast/node.rb', line 181 def type?(*types) type_in?(types) end |
#type_in?(types) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Non-splatting variant of type?, used by the traversal hot paths to
avoid allocating an array per visited node.
188 189 190 191 192 193 |
# File 'lib/rubocop/ast/node.rb', line 188 def type_in?(types) return true if types.include?(type) group_type = GROUP_FOR_TYPE[type] !group_type.nil? && types.include?(group_type) 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.
247 248 249 250 251 |
# File 'lib/rubocop/ast/node.rb', line 247 def updated(type = nil, children = nil, properties = {}) properties[:location] ||= @location klass = RuboCop::AST::Builder::NODE_MAP[type || @type] || Node klass.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?
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
# File 'lib/rubocop/ast/node.rb', line 663 def value_used? # rubocop:disable Metrics/MethodLength # 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
475 476 477 |
# File 'lib/rubocop/ast/node.rb', line 475 def variable? VARIABLES.include?(type) end |