Class: HamlLint::Linter Abstract

Inherits:
Object
  • Object
show all
Includes:
HamlVisitor
Defined in:
lib/haml_lint/linter.rb

Overview

This class is abstract.

Base implementation for all lint checks.

Defined Under Namespace

Classes: AlignmentTabs, AltText, ClassAttributeWithStaticValue, ClassesBeforeIds, ConsecutiveComments, ConsecutiveSilentScripts, EmptyObjectReference, EmptyScript, FinalNewline, HtmlAttributes, IdNames, ImplicitDiv, Indentation, InlineStyles, InstanceVariables, LeadingCommentSpace, LineLength, MultilinePipe, MultilineScript, NoPlaceholders, ObjectReferenceAttributes, RepeatedId, RuboCop, RubyComments, SpaceBeforeScript, SpaceInsideHashAttributes, StrictLocals, Syntax, TagName, TrailingEmptyLines, TrailingWhitespace, UnescapedHtml, UnnecessaryInterpolation, UnnecessaryStringOutput, ViewLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HamlVisitor

#visit, #visit_children

Constructor Details

#initialize(config) ⇒ Linter

Initializes a linter with the specified configuration.

Parameters:

  • config (Hash)

    configuration for this linter



19
20
21
22
# File 'lib/haml_lint/linter.rb', line 19

def initialize(config)
  @config = config
  @lints = []
end

Instance Attribute Details

#lintsObject (readonly)

TODO:

Remove once spec/support/shared_linter_context returns an array of lints for the subject instead of the linter itself.

List of lints reported by this linter.



14
15
16
# File 'lib/haml_lint/linter.rb', line 14

def lints
  @lints
end

Class Method Details

.autocorrect_priority(value = nil) ⇒ Integer

The autocorrect ordering priority for this linter. During autocorrect, linters with a lower priority run first and higher priority run later, against the same (already mutated) document. Linters with the same priority keep their default (alphabetical) order.

Called with an argument (e.g. ‘autocorrect_priority(1)`) in a linter’s top-level scope, it sets the priority; called with no argument it reads it. The default, when never set, is 0.

Parameters:

  • value (Integer, nil) (defaults to: nil)

    the new priority, or nil to read the current one

Returns:

  • (Integer)


108
109
110
111
# File 'lib/haml_lint/linter.rb', line 108

def self.autocorrect_priority(value = nil)
  @autocorrect_priority = value unless value.nil?
  @autocorrect_priority || 0
end

.autocorrect_safe?Boolean

Returns whether this linter’s autocorrect is safe. Defaults to true (safe) unless the linter declares otherwise via ‘autocorrect_safe(false)`.

Returns:

  • (Boolean)


93
94
95
# File 'lib/haml_lint/linter.rb', line 93

def self.autocorrect_safe?
  @autocorrect_safe != false
end

.ruby_parserObject

rubocop:disable Lint/IneffectiveAccessModifier



229
230
231
# File 'lib/haml_lint/linter.rb', line 229

def self.ruby_parser # rubocop:disable Lint/IneffectiveAccessModifier
  @ruby_parser ||= HamlLint::RubyParser.new
end

.supports_autocorrect?Boolean

Returns true if this linter supports autocorrect, false otherwise

Returns:

  • (Boolean)


81
82
83
# File 'lib/haml_lint/linter.rb', line 81

def self.supports_autocorrect?
  @supports_autocorrect || false
end

Instance Method Details

#nameString

Returns the simple name for this linter.

Returns:

  • (String)


74
75
76
# File 'lib/haml_lint/linter.rb', line 74

def name
  self.class.name.to_s.split('::').last
end

#run(document, autocorrect: nil) ⇒ Object

Runs the linter against the given Haml document.

Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/haml_lint/linter.rb', line 27

def run(document, autocorrect: nil) # rubocop:disable Metrics
  run_or_raise(document, autocorrect: autocorrect)
rescue Parser::SyntaxError => e
  location = e.diagnostic.location
  @lints <<
    HamlLint::Lint.new(
      HamlLint::Linter::Syntax.new(config),
      document.file,
      location.line,
      e.to_s,
      :error
    )
rescue StandardError => e
  msg = +"Couldn't process the file"
  if @autocorrect
    # Those lints related to auto-correction were not saved, so don't display them
    @lints = []
    msg << " for autocorrect '#{@autocorrect}'. "
  else
    msg << ' for linting. '
  end

  msg << "#{e.class.name}: #{e.message}"
  if ENV['HAML_LINT_DEBUG'] == 'true'
    msg << "(DEBUG: Backtrace follows)\n#{e.backtrace.join("\n")}\n------"
  end

  @lints << HamlLint::Lint.new(self, document.file, nil, msg, :error)
  @lints
end

#run_or_raise(document, autocorrect: nil) ⇒ Object

Runs the linter against the given Haml document, raises if the file cannot be processed due to Syntax or HAML-Lint internal errors. (For testing purposes)

Parameters:



62
63
64
65
66
67
68
69
# File 'lib/haml_lint/linter.rb', line 62

def run_or_raise(document, autocorrect: nil)
  @document = document
  @lints = []
  @autocorrect = autocorrect
  reset_autocorrect_state
  visit(document.tree)
  @lints
end

#supports_autocorrect?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/haml_lint/linter.rb', line 85

def supports_autocorrect?
  self.class.supports_autocorrect?
end