Class: ERBLint::Linters::SpaceIndentation

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

Overview

Detects indentation with tabs and autocorrect them to spaces

Defined Under Namespace

Classes: ConfigSchema

Constant Summary collapse

START_SPACES =
/\A([[:blank:]]*)/

Constants included from ERBLint::LinterRegistry

ERBLint::LinterRegistry::CUSTOM_LINTERS_DIR

Instance Method Summary collapse

Methods included from ERBLint::LinterRegistry

find_by_name, included, load_custom_linters

Methods inherited from ERBLint::Linter

#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



35
36
37
38
39
# File 'lib/erb_lint/linters/space_indentation.rb', line 35

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

#offenses(processed_source) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/erb_lint/linters/space_indentation.rb', line 16

def offenses(processed_source)
  lines = processed_source.file_content.split("\n", -1)
  document_pos = 0
  lines.each_with_object([]) do |line, offenses|
    spaces = line.match(START_SPACES)&.captures&.first

    if spaces.include?("\t")
      offenses << Offense.new(
        self,
        processed_source.to_source_range(document_pos, document_pos + spaces.length - 1),
        "Indent with spaces instead of tabs.",
        spaces.gsub("\t", ' ' * @config.tab_width)
      )
    end

    document_pos += line.length + 1
  end
end