Ameba::Rule::Base - ameba 1.7.0

# abstract class Ameba::Rule::Base

- Ameba::Rule::Base
- Reference
- Object

##
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

- Ameba::Config::RuleConfig

##
Direct Known Subclasses

- Ameba::Rule::Documentation::Admonition
- Ameba::Rule::Documentation::Documentation
- Ameba::Rule::Layout::LineLength
- Ameba::Rule::Layout::TrailingBlankLines
- Ameba::Rule::Layout::TrailingWhitespace
- Ameba::Rule::Lint::AmbiguousAssignment
- Ameba::Rule::Lint::AssignmentInCallArgument
- Ameba::Rule::Lint::BadDirective
- Ameba::Rule::Lint::ComparisonToBoolean
- Ameba::Rule::Lint::DebugCalls
- Ameba::Rule::Lint::DebuggerStatement
- Ameba::Rule::Lint::DeprecatedRule
- Ameba::Rule::Lint::DuplicateBranch
- Ameba::Rule::Lint::DuplicateEnumValue
- Ameba::Rule::Lint::DuplicateMethodSignature
- Ameba::Rule::Lint::DuplicateRequire
- Ameba::Rule::Lint::DuplicateWhenCondition
- Ameba::Rule::Lint::ElseNil
- Ameba::Rule::Lint::EmptyEnsure
- Ameba::Rule::Lint::EmptyExpression
- Ameba::Rule::Lint::EmptyLoop
- Ameba::Rule::Lint::EnumMemberNameConflict
- Ameba::Rule::Lint::Formatting
- Ameba::Rule::Lint::HashDuplicatedKey
- Ameba::Rule::Lint::LiteralAssignmentsInExpressions
- Ameba::Rule::Lint::LiteralInCondition
- Ameba::Rule::Lint::LiteralInInterpolation
- Ameba::Rule::Lint::LiteralsComparison
- Ameba::Rule::Lint::MissingBlockArgument
- Ameba::Rule::Lint::NonExistentRule
- Ameba::Rule::Lint::NotNil
- Ameba::Rule::Lint::NotNilAfterNoBang
- Ameba::Rule::Lint::PercentArrays
- Ameba::Rule::Lint::RandZero
- Ameba::Rule::Lint::RedundantStringCoercion
- Ameba::Rule::Lint::RedundantWithIndex
- Ameba::Rule::Lint::RedundantWithObject
- Ameba::Rule::Lint::RequireParentheses
- Ameba::Rule::Lint::SelfInitializeDefinition
- Ameba::Rule::Lint::ShadowedArgument
- Ameba::Rule::Lint::ShadowedException
- Ameba::Rule::Lint::ShadowingOuterLocalVar
- Ameba::Rule::Lint::SharedVarInFiber
- Ameba::Rule::Lint::SignalTrap
- Ameba::Rule::Lint::SpecEqWithBoolOrNilLiteral
- Ameba::Rule::Lint::SpecFilename
- Ameba::Rule::Lint::SpecFocus
- Ameba::Rule::Lint::Syntax
- Ameba::Rule::Lint::TopLevelOperatorDefinition
- Ameba::Rule::Lint::TrailingRescueException
- Ameba::Rule::Lint::Typos
- Ameba::Rule::Lint::UnneededDisableDirective
- Ameba::Rule::Lint::UnreachableCode
- Ameba::Rule::Lint::UnusedArgument
- Ameba::Rule::Lint::UnusedBlockArgument
- Ameba::Rule::Lint::UnusedExpression
- Ameba::Rule::Lint::UnusedRescueVariable
- Ameba::Rule::Lint::UselessAssign
- Ameba::Rule::Lint::UselessConditionInWhen
- Ameba::Rule::Lint::UselessVisibilityModifier
- Ameba::Rule::Lint::VoidOutsideLib
- Ameba::Rule::Lint::WhitespaceAroundMacroExpression
- Ameba::Rule::Metrics::CyclomaticComplexity
- Ameba::Rule::Naming::AccessorMethodName
- Ameba::Rule::Naming::AsciiIdentifiers
- Ameba::Rule::Naming::BinaryOperatorParameterName
- Ameba::Rule::Naming::BlockParameterName
- Ameba::Rule::Naming::ConstantNames
- Ameba::Rule::Naming::Filename
- Ameba::Rule::Naming::MethodNames
- Ameba::Rule::Naming::PredicateName
- Ameba::Rule::Naming::QueryBoolMethods
- Ameba::Rule::Naming::RescuedExceptionsVariableName
- Ameba::Rule::Naming::TypeNames
- Ameba::Rule::Naming::VariableNames
- Ameba::Rule::Performance::Base
- Ameba::Rule::Style::ArrayLiteralSyntax
- Ameba::Rule::Style::CallParentheses
- Ameba::Rule::Style::Elsif
- Ameba::Rule::Style::GuardClause
- Ameba::Rule::Style::HashLiteralSyntax
- Ameba::Rule::Style::HeredocEscape
- Ameba::Rule::Style::HeredocIndent
- Ameba::Rule::Style::IsAFilter
- Ameba::Rule::Style::IsANil
- Ameba::Rule::Style::LargeNumbers
- Ameba::Rule::Style::MultilineCurlyBlock
- Ameba::Rule::Style::MultilineStringLiteral
- Ameba::Rule::Style::NegatedConditionsInUnless
- Ameba::Rule::Style::ParenthesesAroundCondition
- Ameba::Rule::Style::PercentLiteralDelimiters
- Ameba::Rule::Style::RedundantBegin
- Ameba::Rule::Style::RedundantNext
- Ameba::Rule::Style::RedundantNilInControlExpression
- Ameba::Rule::Style::RedundantReturn
- Ameba::Rule::Style::RedundantSelf
- Ameba::Rule::Style::UnlessElse
- Ameba::Rule::Style::VerboseBlock
- Ameba::Rule::Style::VerboseNilType
- Ameba::Rule::Style::WhileTrue
- Ameba::Rule::Typing::MacroCallArgumentTypeRestriction
- Ameba::Rule::Typing::MethodParameterTypeRestriction
- Ameba::Rule::Typing::MethodReturnTypeRestriction
- Ameba::Rule::Typing::ProcLiteralReturnTypeRestriction

##
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

- .default_severity : Ameba::Severity
- .group_name

  Returns the group name for this rule.

- .rule_name

  Returns the name for this rule.

##
Macro Summary

- issue_for(*args, **kwargs, &block)

  Adds an issue to the source

##
Instance Method Summary

- #==(other : self)

  Returns `true` if this reference is the same as other.

- #catch(source : Source)

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

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

  Checks whether the source is excluded from this rule.

- #group

  Returns a group this rule belongs to.

- #hash(hasher)

  See `Object#hash(hasher)`

- #name

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

- #special?

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

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

  NOTE Can't be abstract

- #test(source : Source)

  This method is designed to test the source passed in.

### 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.