Class: Collie::Linter::Rules::TokenNaming

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

Overview

Checks token naming conventions

Constant Summary collapse

DEFAULT_PATTERN =
/^[A-Z][A-Z0-9_]*$/

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



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

def check(ast, _context = {})
  pattern = @config[:pattern] ? Regexp.new(@config[:pattern]) : DEFAULT_PATTERN

  ast.declarations.each do |decl|
    next unless decl.is_a?(AST::TokenDeclaration)

    decl.names.each do |name|
      next if name.match?(pattern)
      next if name.start_with?('"', "'") # Skip literals

      add_offense(decl,
                  message: "Token '#{name}' should match pattern #{pattern.inspect}")
    end
  end

  @offenses
end