123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::NodePattern::Checker::SequenceCheck Private

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

Overview

Checks a sequence, like (send _ :foo), against the layouts of the node types that it is for.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(node) ⇒ SequenceCheck

[ GitHub ]

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

def initialize(node)
  @node = node
  @terms = node.children.drop(1)
  @problems = []
  alternatives = head_types(node.child)
  return unless alternatives

  fitting = check_arity(alternatives.map { |types| types.select { |type| NodeLayout.of(type) } })
  # The children of a type that has no layout can be anything.
  check_children(fitting) if alternatives.flatten.all? { |type| NodeLayout.of(type) }
end

Instance Attribute Details

#problems ⇒ Array<(NodePattern::Node, String)> (readonly)

Returns:

[ GitHub ]

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

attr_reader :problems

Instance Method Details

#arity ⇒ Range (private)

Returns:

  • (Range) —

    how many children the terms match

[ GitHub ]

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

def arity
  ranges = @terms.map(&:arity_range)
  ranges.sum(&:begin)..ranges.sum(&:end)
end

#arity_problem(type, wanted) (private)

[ GitHub ]

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

def arity_problem(type, wanted)
  has = count(NodeLayout.of(type).arity, 'children')
  problem(@node, "`#{type}` nodes have #{has}, not #{count(wanted)}.")
end

#check_arity(alternatives) ⇒ Array<Symbol> (private)

Returns:

  • (Array<Symbol>) —

    the types that can have as many children as the terms match

[ GitHub ]

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

def check_arity(alternatives)
  wanted = arity
  alternatives.flat_map do |types|
    fitting = types.select { |type| overlap?(NodeLayout.of(type).arity, wanted) }
    # A group is fine as long as one of its types fits.
    types.each { |type| arity_problem(type, wanted) } if fitting.empty?
    fitting
  end.uniq
end

#check_children(types) (private)

[ GitHub ]

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

def check_children(types)
  @terms.each_with_index do |term, index|
    # From here on, which child a term matches is not known.
    break unless term.arity == 1

    kinds = types.flat_map { |type| NodeLayout.of(type).kinds(index) }.uniq
    next if kinds.empty? || possible?(term, kinds)

    problem(term, "This never matches child #{index + 1} of `#{types.join('`, `')}` " \
                  "nodes, which is #{kinds.map { |kind| describe(kind) }.join(' or ')}.")
  end
end

#count(range, noun = nil) (private)

[ GitHub ]

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

def count(range, noun = nil)
  number = if range.end == Float::INFINITY
             "#{range.begin} or more"
           elsif range.begin == range.end
             range.begin.zero? && noun ? 'no' : range.begin.to_s
           else
             "#{range.begin} to #{range.end}"
           end
  noun = 'child' if noun && range == (1..1)
  [number, noun].compact.join(' ')
end

#describe(kind) (private)

[ GitHub ]

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

def describe(kind)
  return 'a node' if kind == :node
  return 'nil' if kind == NilClass

  "#{kind == Integer ? 'an' : 'a'} #{kind.name.downcase}"
end

#head_types(head) ⇒ Array<Array<Symbol>>? (private)

Returns:

  • (Array<Array<Symbol>>, nil) —

    the node types that each alternative of the head stands for, if the head is nothing but node types

[ GitHub ]

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

def head_types(head)
  heads = head.type == :union ? head.children : [head]
  return unless heads.all? { |child| node_type?(child) }

  heads.map { |child| members(child.child) }
end

#kind?(term, kinds) ⇒ Boolean (private)

[ GitHub ]

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

def kind?(term, kinds)
  case term.type
  when :symbol, :number, :string then !(kinds & LITERALS.fetch(term.type)).empty?
  when :node_type then !Checker.type?(term.child) || kinds.include?(:node)
  when :sequence then kinds.include?(:node)
  when :predicate then term.children != [:nil?] || kinds.include?(NilClass)
  else true
  end
end

#members(type) ⇒ Array<Symbol> (private)

Returns:

  • (Array<Symbol>) —

    the types that a node type or a group stands for

[ GitHub ]

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

def members(type)
  type = type.to_s.tr('-', '_').to_sym
  GROUPS.fetch(type) { [type] }
end

#node_type?(node) ⇒ Boolean (private)

[ GitHub ]

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

def node_type?(node)
  node.type == :node_type && Checker.type?(node.child)
end

#overlap?(range, other) ⇒ Boolean (private)

[ GitHub ]

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

def overlap?(range, other)
  range.begin <= other.end && other.begin <= range.end
end

#possible?(term, kinds) ⇒ Boolean (private)

Returns:

  • (Boolean) —

    whether a term can match a child that is one of kinds

[ GitHub ]

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

def possible?(term, kinds)
  case term.type
  when :capture then possible?(term.child, kinds)
  when :union, :set then term.children.any? { |child| possible?(child, kinds) }
  when :intersection then term.children.all? { |child| possible?(child, kinds) }
  else kind?(term, kinds)
  end
end

#problem(node, message) (private)

[ GitHub ]

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

def problem(node, message)
  @problems << [node, message]
end