Class: Mdlint::Linter::Rules::NoTrailingSpaces

Inherits:
Mdlint::Linter::Rule show all
Defined in:
lib/mdlint/linter/rules/no_trailing_spaces.rb,
sig/internal.rbs

Instance Attribute Summary

Attributes inherited from Mdlint::Linter::Rule

#violations

Instance Method Summary collapse

Methods inherited from Mdlint::Linter::Rule

#add_violation, inherited, #initialize

Constructor Details

This class inherits a constructor from Mdlint::Linter::Rule

Instance Method Details

#check(_tokens, source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mdlint/linter/rules/no_trailing_spaces.rb', line 11

def check(_tokens, source)
  source.each_line.with_index(1) do |line, line_num|
    trimmed = line.chomp
    next unless trimmed.end_with?(" ") || trimmed.end_with?("\t")
    next if trimmed.end_with?("  ") && line_num < source.lines.count

    add_violation(
      message: "Trailing spaces",
      line: line_num,
      column: trimmed.rstrip.length + 1,
      fixable: true
    )
  end
  @violations
end

#fix(_tokens, source) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/mdlint/linter/rules/no_trailing_spaces.rb', line 27

def fix(_tokens, source)
  source.each_line.map do |line|
    if line.end_with?("  \n")
      line
    else
      line.rstrip + "\n"
    end
  end.join
end