Class: SasLinter::Rules::TrailingWhitespace

Inherits:
SasLinter::Rule show all
Defined in:
lib/sas_linter/rules/trailing_whitespace.rb

Overview

Flag end-of-line trailing whitespace (spaces or tabs that appear before the line terminator). Trailing whitespace is invisible noise — it inflates diffs, fights with editor auto-trim, and hides intent. Supports ‘autofix` to strip the offending bytes in place.

Recognized config options:

autofix: true | false   (default: false)

Constant Summary collapse

TRAILING_WS =
/([ \t]+)(\r?\n|\z)/

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SasLinter::Rule

all, #autofix?, description, fetch, from_config, inherited, #initialize, register, registry, rule_id, severity

Constructor Details

This class inherits a constructor from SasLinter::Rule

Class Method Details

.supports_autofix?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/sas_linter/rules/trailing_whitespace.rb', line 22

def self.supports_autofix?
  true
end

Instance Method Details

#autofix(source) ⇒ Object

Strip end-of-line trailing whitespace while preserving the original line terminator (LF or CRLF) and the trailing newline (or its absence) on the final line.



48
49
50
# File 'lib/sas_linter/rules/trailing_whitespace.rb', line 48

def autofix(source)
  source.gsub(TRAILING_WS) { ::Regexp.last_match(2) }
end

#check(_tokens, path:, all_tokens: nil, source: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sas_linter/rules/trailing_whitespace.rb', line 26

def check(_tokens, path:, all_tokens: nil, source: nil) # rubocop:disable Lint/UnusedMethodArgument
  return [] unless source

  findings = []
  source.each_line.with_index do |line, idx|
    chomped = line.sub(/\r?\n\z/, "")
    next unless chomped =~ /([ \t]+)\z/

    ws_start = ::Regexp.last_match.begin(1)
    findings << finding(
      line: idx + 1,
      column: ws_start + 1,
      message: "trailing whitespace#{autofix? ? ' (autofixed)' : ''}",
      path: path
    )
  end
  findings
end