abstract class Ameba::Rule::Base

Overview

Represents a base of all rules. In other words, all rules inherit from this class:

class Ameba::Rule::MyRule < Ameba::Rule::Base
  def test(source)
    if invalid?(source)
      issue_for line, column, "Something wrong"
    end
  end

  private def invalid?(source)
    # ...
  end
end

Enforces rules to implement an abstract #test method which is designed to test the source passed in. If source has issues that are tested by this rule, it should add an issue.

Included Modules

Direct Known Subclasses

Defined in:

ameba/rule/base.cr

Constant Summary

GROUP_SEVERITY = { Lint: Ameba::Severity::Warning, Metrics: Ameba::Severity::Warning, Performance: Ameba::Severity::Warning, }

Class Method Summary

Macro Summary

Instance Method Summary

Macros inherited from module Ameba::Config::RuleConfig

properties(&block) properties

Class Method Detail

def self.default_severity : Ameba::Severity #

def self.group_name #

Returns the group name for this rule.

Ameba::Rule::Lint::Syntax.group_name # => "Lint"

def self.rule_name #

Returns the name for this rule.

Ameba::Rule::Lint::Syntax.rule_name # => "Lint/Syntax"

Macro Detail

macro issue_for(*args, **kwargs, &block) #

Adds an issue to the source


Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def catch(source : Source) #

A convenient addition to #test method that does the same but returns a passed in source as an addition.

source = MyRule.new.catch(source)
source.valid?

def excluded?(source, root = Dir.current) #

Checks whether the source is excluded from this rule. It searches for a path in excluded property which matches the one of the given source.

my_rule.excluded?(source) # => true or false

def group #

Returns a group this rule belongs to.

module Ameba
  class Rule::MyGroup::MyRule < Rule::Base
    # ...
  end
end

Ameba::Rule::MyGroup::MyRule.new.group # => "MyGroup"

def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def name #

Returns a name of this rule, which is basically a class name.

module Ameba
  class Rule::MyRule < Rule::Base
    def test(source)
    end
  end
end

Ameba::Rule::MyRule.new.name # => "MyRule"

def special? #

Returns true if this rule is special and behaves differently than usual rules.

my_rule.special? # => true or false

def test(source : Source, node : Crystal::ASTNode, *opts) #

NOTE Can't be abstract


def test(source : Source) #

This method is designed to test the source passed in. If source has issues that are tested by this rule, it should add an issue.

By default it uses a node visitor to traverse all the nodes in the source.

NOTE Must be overridden for other type of rules.