Class: Collie::Linter::Rules::UnreachableRule

Inherits:
Base
  • Object
show all
Defined in:
lib/collie/linter/rules/unreachable_rule.rb

Overview

Detects rules that are not reachable from the start symbol

Instance Method Summary collapse

Methods inherited from Base

#autocorrectable?, #initialize

Constructor Details

This class inherits a constructor from Collie::Linter::Base

Instance Method Details

#check(ast, _context = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/collie/linter/rules/unreachable_rule.rb', line 15

def check(ast, _context = {})
  return @offenses if ast.rules.empty?

  analyzer = Analyzer::Reachability.new(ast)
  start_symbol = find_start_symbol(ast)
  analyzer.analyze(start_symbol)

  unreachable = analyzer.unreachable_rules

  unreachable.each do |rule_name|
    rule = ast.rules.find { |r| r.name == rule_name }
    next unless rule

    add_offense(rule,
                message: "Rule '#{rule_name}' is not reachable from start symbol '#{start_symbol}'")
  end

  @offenses
end