Class: RemLint::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/remlint/rule.rb

Overview

Base class for rules. Subclassing registers.

A rule implements check and calls offend for each thing it finds. It gets one Document and its own slice of configuration, and it is instantiated fresh per document, so a rule accumulating state across a file (block stacks, FSET definitions) does not have to clean up after itself.

Registry-by-inheritance rather than an explicit list, following haml-lint: a new file under rules/ that is required is a rule that runs, with nothing else to remember to edit.

Direct Known Subclasses

RemLint::Rules::AddomitWithoutScanfrom, RemLint::Rules::AdvanceWarningBody, RemLint::Rules::BannerPlacement, RemLint::Rules::CalendarTextLimited, RemLint::Rules::CallbackSignature, RemLint::Rules::ClauseNeedsFullDate, RemLint::Rules::ClauseRequiresAt, RemLint::Rules::ClauseValueRange, RemLint::Rules::ColorComponentRange, RemLint::Rules::CoordinateNotString, RemLint::Rules::DanglingContinuation, RemLint::Rules::DateOutOfRange, RemLint::Rules::DebugCommand, RemLint::Rules::EasterdateFromToday, RemLint::Rules::FunctionArity, RemLint::Rules::FunctionRedefinition, RemLint::Rules::GeneratedFileEdited, RemLint::Rules::HebrewDate, RemLint::Rules::IftrigWithSatisfy, RemLint::Rules::IncludePath, RemLint::Rules::InfoClause, RemLint::Rules::InfoSubstitutionWithoutHeader, RemLint::Rules::InvocationMismatch, RemLint::Rules::KeywordCase, RemLint::Rules::LicenseHeader, RemLint::Rules::LineLength, RemLint::Rules::LiteralTypeMismatch, RemLint::Rules::LocalizationPack, RemLint::Rules::MoonPhaseArgument, RemLint::Rules::OmitAwareDelta, RemLint::Rules::PushVarsMissingName, RemLint::Rules::RepeatTrigger, RemLint::Rules::SatisfyConstraint, RemLint::Rules::ShellMaxlen, RemLint::Rules::ShellUseWhileRunDisabled, RemLint::Rules::StringEscape, RemLint::Rules::Syntax, RemLint::Rules::SystemVariableAssignment, RemLint::Rules::TagSyntax, RemLint::Rules::TextAfterEofMarker, RemLint::Rules::TimeZoneName, RemLint::Rules::TkTagNamespace, RemLint::Rules::TodoCompleteThrough, RemLint::Rules::TrailingWhitespace, RemLint::Rules::TranslateCommand, RemLint::Rules::UnbalancedBlocks, RemLint::Rules::UnbalancedDelimiters, RemLint::Rules::UnknownSpecialType, RemLint::Rules::UnknownSubstitutionSequence, RemLint::Rules::UnknownSystemVariable, RemLint::Rules::UnquotedShellSubstitution, RemLint::Rules::UntilBeforeFrom, RemLint::Rules::WorldWritableScript

Constant Summary collapse

REGISTRY =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Rule

Returns a new instance of Rule.



77
78
79
80
# File 'lib/remlint/rule.rb', line 77

def initialize(config = {})
  @config = config
  @offenses = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



75
76
77
# File 'lib/remlint/rule.rb', line 75

def config
  @config
end

#documentObject (readonly)

Returns the value of attribute document.



75
76
77
# File 'lib/remlint/rule.rb', line 75

def document
  @document
end

#offensesObject (readonly)

Returns the value of attribute offenses.



75
76
77
# File 'lib/remlint/rule.rb', line 75

def offenses
  @offenses
end

Class Method Details

.allObject

Only named subclasses. A rule with no name cannot be configured, named in a remlint:disable comment, or reported usefully, so it is not one the runner can run -- which is also what keeps the anonymous classes a spec makes from turning up in someone else's lint run.



66
67
68
# File 'lib/remlint/rule.rb', line 66

def all
  REGISTRY.reject { |rule| rule.name.nil? }
end

.default_severityObject



52
53
54
# File 'lib/remlint/rule.rb', line 52

def default_severity
  "warning"
end

.descriptionObject

One line, shown by remlint --show-rules and used as the doc comment in the generated default config.



58
59
60
# File 'lib/remlint/rule.rb', line 58

def description
  ""
end

.enabled_by_default?Boolean

Overridden by a rule that should be off unless asked for. Rules that enforce a house preference rather than report a defect default to off, because a linter whose first run is a wall of style noise gets turned off entirely.

Returns:

  • (Boolean)


48
49
50
# File 'lib/remlint/rule.rb', line 48

def enabled_by_default?
  true
end

.find(rule_name) ⇒ Object



70
71
72
# File 'lib/remlint/rule.rb', line 70

def find(rule_name)
  all.find { |rule| rule.rule_name == rule_name }
end

.inherited(subclass) ⇒ Object



27
28
29
30
# File 'lib/remlint/rule.rb', line 27

def inherited(subclass)
  super
  REGISTRY << subclass
end

.rule_nameObject

RemLint::Rules::TrailingWhitespace configures and reports as TrailingWhitespace. Anonymous subclasses -- which specs make, and which land in the registry like any other -- get a placeholder rather than blowing up every caller that walks the registry.



36
37
38
39
40
41
42
# File 'lib/remlint/rule.rb', line 36

def rule_name
  if name
    name.split("::").last
  else
    "(anonymous)"
  end
end

Instance Method Details

#checkObject

Subclasses implement this.

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/remlint/rule.rb', line 94

def check
  raise NotImplementedError, "#{self.class} must implement #check"
end

#rule_nameObject



89
90
91
# File 'lib/remlint/rule.rb', line 89

def rule_name
  self.class.rule_name
end

#run(document) ⇒ Object



82
83
84
85
86
87
# File 'lib/remlint/rule.rb', line 82

def run(document)
  @document = document
  @offenses = []
  check
  offenses
end