Class: ERBLint::Linters::TrailingWhitespace

Inherits:
ERBLint::Linter show all
Includes:
ERBLint::LinterRegistry
Defined in:
lib/erb_lint/linters/trailing_whitespace.rb

Overview

Detects trailing whitespace at the end of a line

Constant Summary collapse

TRAILING_WHITESPACE =
/([[:space:]]*)\Z/

Constants included from ERBLint::LinterRegistry

ERBLint::LinterRegistry::CUSTOM_LINTERS_DIR

Instance Attribute Summary

Attributes inherited from ERBLint::Linter

#offenses

Instance Method Summary collapse

Methods included from ERBLint::LinterRegistry

clear, find_by_name, included, linters, load_custom_linters

Methods inherited from ERBLint::Linter

#add_offense, #clear_offenses, #enabled?, #excludes_file?, inherited, #initialize, support_autocorrect?

Constructor Details

This class inherits a constructor from ERBLint::Linter

Instance Method Details

#autocorrect(_processed_source, offense) ⇒ Object



26
27
28
29
30
# File 'lib/erb_lint/linters/trailing_whitespace.rb', line 26

def autocorrect(_processed_source, offense)
  lambda do |corrector|
    corrector.replace(offense.source_range, '')
  end
end

#run(processed_source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/erb_lint/linters/trailing_whitespace.rb', line 11

def run(processed_source)
  lines = processed_source.file_content.split("\n", -1)
  document_pos = 0
  lines.each do |line|
    document_pos += line.length + 1
    whitespace = line.match(TRAILING_WHITESPACE)&.captures&.first
    next unless whitespace && !whitespace.empty?

    add_offense(
      processed_source.to_source_range((document_pos - whitespace.length - 1)...(document_pos - 1)),
      "Extra whitespace detected at end of line."
    )
  end
end