123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::NodePattern::Checker

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/rubocop/ast/node_pattern/checker.rb,
lib/rubocop/ast/node_pattern/checker/sequence_check.rb

Overview

Finds the parts of a pattern that can never match, or that are there twice:

  • node types that do not exist

  • sequences that have more or fewer children than a node of that type

  • children that are a node where a node of that type has a value like a symbol, and the other way around

  • predicates that nothing responds to

  • alternatives of a union that are repeated

    Checker.new.check('(def _ _)').map(&:message)
    # => ['`def` nodes have 3 children, not 2.']

A pattern that has none of these is not necessarily right.

Constant Summary

Class Method Summary

Instance Method Summary

Class Method Details

.type?(type) ⇒ Boolean

Returns:

  • (Boolean) —

    whether a pattern can have this node type in it

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 33

def self.type?(type)
  AST::Node.method_defined?(:"#{type.to_s.tr('-', '_')}_type?")
end

Instance Method Details

#check(pattern) ⇒ Array<Problem>

Parameters:

  • pattern (String)

Raises:

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 40

def check(pattern)
  @problems = []
  visit(Parser::WithMeta.new.parse(pattern))
  @problems
end

#check_alternatives(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 89

def check_alternatives(node)
  # What a union in the head of a sequence is rewritten to has no location.
  sources = node.children.grep(Node).filter_map { |child| child.loc&.expression&.source }
  sources.select { |source| sources.count(source) > 1 }.uniq.each do |source|
    problem(node, "`#{source}` is in the union more than once.")
  end
end

#check_predicate(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 72

def check_predicate(node)
  name = node.method_name
  return if receivers.any? { |klass| klass.method_defined?(name) }

  problem(node, "Neither a node nor a child of one responds to `#{name}`.")
end

#check_sequence(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 68

def check_sequence(node)
  SequenceCheck.new(node).problems.each { |term, message| problem(term, message) }
end

#check_type(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 64

def check_type(node)
  problem(node, "Unknown node type `#{node.child}`.") unless Checker.type?(node.child)
end

#node_classes (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 83

def node_classes
  AST.constants.reject { |name| AST.autoload?(name) }
     .map { |name| AST.const_get(name) }
     .select { |const| const.is_a?(Class) && const <= AST::Node }
end

#problem(node, message) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 60

def problem(node, message)
  @problems << Problem.new(node, message)
end

#receivers (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 79

def receivers
  @receivers ||= VALUES + node_classes
end

#visit(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 48

def visit(node)
  return unless node.is_a?(Node)

  case node.type
  when :node_type then check_type(node)
  when :sequence then check_sequence(node)
  when :predicate then check_predicate(node)
  when :union, :set then check_alternatives(node)
  end
  node.children.each { |child| visit(child) }
end