Class: Collie::Linter::Rules::AmbiguousPrecedence

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

Overview

Detects operators without explicit precedence declarations

Constant Summary collapse

OPERATOR_PATTERNS =

Common operator patterns (may be quoted or unquoted)

[
  %r{^'[+\-*/%^&|<>=!]+'$}, # Quoted single-character operators
  %r{^"[+\-*/%^&|<>=!]+"$}, # Double-quoted single-character operators
  %r{^[+\-*/%^&|<>=!]+$}, # Unquoted symbolic operators
  /^'(==|!=|<=|>=|<<|>>|\|\||&&)'$/, # Quoted multi-character operators
  /^"(==|!=|<=|>=|<<|>>|\|\||&&)"$/, # Double-quoted multi-character operators
  /^(==|!=|<=|>=|<<|>>|\|\||&&)$/ # Unquoted multi-character operators
].freeze

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/collie/linter/rules/ambiguous_precedence.rb', line 23

def check(ast, _context = {})
  precedence_tokens = collect_precedence_tokens(ast)
  operators = collect_operators(ast)

  operators.each do |operator, locations|
    next if precedence_tokens.include?(operator)

    locations.each do |location|
      add_offense_at(
        location,
        message: "Operator '#{operator}' does not have an explicit precedence declaration"
      )
    end
  end

  @offenses
end