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
A pattern that has none of these is not necessarily right.
Constant Summary
-
VALUES =
private
# File 'lib/rubocop/ast/node_pattern/checker.rb', line 29[Symbol, String, Integer, Float, Rational, Complex, NilClass].freeze
Class Method Summary
Instance Method Summary
- #check(pattern) ⇒ Array<Problem>
- #check_alternatives(node) private
- #check_predicate(node) private
- #check_sequence(node) private
- #check_type(node) private
- #node_classes private
- #problem(node, message) private
- #receivers private
- #visit(node) private
Class Method Details
.type?(type) ⇒ Boolean
Instance Method Details
#check(pattern) ⇒ Array<Problem>
#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 ]#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, | problem(term, ) } end
#check_type(node) (private)
[ GitHub ]#node_classes (private)
[ GitHub ]#problem(node, message) (private)
[ GitHub ]#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