Class: ERBLint::Linters::RightTrim

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

Overview

In ERB, right trim can be either =%> or -%> this linter will force one or the other.

Defined Under Namespace

Classes: ConfigSchema

Constant Summary

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



27
28
29
30
31
# File 'lib/erb_lint/linters/right_trim.rb', line 27

def autocorrect(_processed_source, offense)
  lambda do |corrector|
    corrector.replace(offense.source_range, @config.enforced_style)
  end
end

#run(processed_source) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/erb_lint/linters/right_trim.rb', line 15

def run(processed_source)
  processed_source.ast.descendants(:erb).each do |erb_node|
    _, _, _, trim_node = *erb_node
    next if trim_node.nil? || trim_node.loc.source == @config.enforced_style

    add_offense(
      trim_node.loc,
      "Prefer #{@config.enforced_style}%> instead of #{trim_node.loc.source}%> for trimming on the right."
    )
  end
end