Class: Collie::Linter::Rules::LongRule

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

Overview

Detects rules with too many alternatives

Constant Summary collapse

DEFAULT_MAX_ALTERNATIVES =
10

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
# File 'lib/collie/linter/rules/long_rule.rb', line 15

def check(ast, _context = {})
  max_alternatives = @config.dig("rules", "LongRule", "max_alternatives") || DEFAULT_MAX_ALTERNATIVES

  ast.rules.each do |rule|
    alternatives_count = rule.alternatives.size

    next unless alternatives_count > max_alternatives

    add_offense(
      rule,
      message: "Rule '#{rule.name}' has #{alternatives_count} alternatives " \
               "(max: #{max_alternatives}). Consider refactoring."
    )
  end

  @offenses
end