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
-
GROUPS =
private
# File 'lib/rubocop/ast/node_pattern/checker/sequence_check.rb', line 13AST::Node::GROUP_FOR_TYPE.group_by(&:last) .transform_values { |pairs| pairs.map(&:first) } .freeze
-
LITERALS =
private
# File 'lib/rubocop/ast/node_pattern/checker/sequence_check.rb', line 12{ symbol: [Symbol], number: [Integer, Float, Rational, Complex], string: [String] }.freeze
Class Method Summary
- .new(node) ⇒ SequenceCheck constructor Internal use only
Instance Attribute Summary
- #problems ⇒ Array<(NodePattern::Node, String)> readonly Internal use only
Instance Method Summary
- #arity ⇒ Range private Internal use only
- #arity_problem(type, wanted) private Internal use only
- #check_arity(alternatives) ⇒ Array<Symbol> private Internal use only
- #check_children(types) private Internal use only
- #count(range, noun = nil) private Internal use only
- #describe(kind) private Internal use only
- #head_types(head) ⇒ Array<Array<Symbol>>? private Internal use only
- #kind?(term, kinds) ⇒ Boolean private Internal use only
- #members(type) ⇒ Array<Symbol> private Internal use only
- #node_type?(node) ⇒ Boolean private Internal use only
- #overlap?(range, other) ⇒ Boolean private Internal use only
- #possible?(term, kinds) ⇒ Boolean private Internal use only
- #problem(node, message) private Internal use only
Constructor Details
.new(node) ⇒ SequenceCheck
# 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)
# File 'lib/rubocop/ast/node_pattern/checker/sequence_check.rb', line 19
attr_reader :problems
Instance Method Details
#arity ⇒ Range (private)
# 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 ]
#check_arity(alternatives) ⇒ Array<Symbol> (private)
# 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)
# 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)
# 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)
#node_type?(node) ⇒ Boolean (private)
#overlap?(range, other) ⇒ Boolean (private)
# 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)
# 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, ) @problems << [node, ] end