Class: HamlLint::Linter::TrailingWhitespace

Inherits:
Linter
  • Object
show all
Includes:
HamlLint::LinterRegistry
Defined in:
lib/haml_lint/linter/trailing_whitespace.rb

Overview

Checks for trailing whitespace.

Defined Under Namespace

Classes: DummyNode

Instance Method Summary collapse

Methods included from HamlLint::LinterRegistry

extract_linters_from, included

Instance Method Details

#visit_root(root) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/haml_lint/linter/trailing_whitespace.rb', line 12

def visit_root(root)
  new_lines = document.source_lines.dup
  changed = false

  document.source_lines.each_with_index do |line, index|
    next unless /\s+$/.match?(line)

    node = root.node_for_line(index + 1)
    next if node.disabled?(self)

    if autocorrect?
      new_lines[index] = line.sub(/\s+$/, '')
      changed = true
    end

    record_lint(DummyNode.new(index + 1), 'Line contains trailing whitespace',
                corrected: autocorrect?)
  end

  apply_autocorrect(new_lines.join("\n")) if changed
end