Class: RemLint::Rule
- Inherits:
-
Object
- Object
- RemLint::Rule
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#offenses ⇒ Object
readonly
Returns the value of attribute offenses.
Class Method Summary collapse
-
.all ⇒ Object
Only named subclasses.
- .default_severity ⇒ Object
-
.description ⇒ Object
One line, shown by
remlint --show-rulesand used as the doc comment in the generated default config. -
.enabled_by_default? ⇒ Boolean
Overridden by a rule that should be off unless asked for.
- .find(rule_name) ⇒ Object
- .inherited(subclass) ⇒ Object
-
.rule_name ⇒ Object
RemLint::Rules::TrailingWhitespaceconfigures and reports asTrailingWhitespace.
Instance Method Summary collapse
-
#check ⇒ Object
Subclasses implement this.
-
#initialize(config = {}) ⇒ Rule
constructor
A new instance of Rule.
- #rule_name ⇒ Object
- #run(document) ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
75 76 77 |
# File 'lib/remlint/rule.rb', line 75 def config @config end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
75 76 77 |
# File 'lib/remlint/rule.rb', line 75 def document @document end |
#offenses ⇒ Object (readonly)
Returns the value of attribute offenses.
75 76 77 |
# File 'lib/remlint/rule.rb', line 75 def offenses @offenses end |
Class Method Details
.all ⇒ Object
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_severity ⇒ Object
52 53 54 |
# File 'lib/remlint/rule.rb', line 52 def default_severity "warning" end |
.description ⇒ Object
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.
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_name ⇒ Object
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
#check ⇒ Object
Subclasses implement this.
94 95 96 |
# File 'lib/remlint/rule.rb', line 94 def check raise NotImplementedError, "#{self.class} must implement #check" end |
#rule_name ⇒ Object
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 |