Class: Collie::Linter::Rules::UnusedToken

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

Overview

Detects tokens that are declared but never used

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
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/collie/linter/rules/unused_token.rb', line 15

def check(ast, context = {})
  symbol_table = context[:symbol_table] || build_symbol_table(ast)

  # Track token usage in normal rules
  ast.rules.each do |rule|
    rule.alternatives.each do |alt|
      alt.symbols.each do |symbol|
        symbol_table.use_token(symbol.name) if symbol.terminal?
      end
    end
  end

  # Track token usage in parameterized rules (%rule)
  ast.declarations.each do |decl|
    next unless decl.is_a?(AST::ParameterizedRule)

    decl.alternatives.each do |alt|
      alt.symbols.each do |symbol|
        symbol_table.use_token(symbol.name) if symbol.terminal?
      end
    end
  end

  # Find unused tokens
  symbol_table.unused_tokens.each do |token_name|
    token_info = symbol_table.tokens[token_name]
    add_offense_for_declaration(ast, token_name, token_info[:location])
  end

  @offenses
end