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, ERBLint::LinterRegistry::DEPRECATED_CUSTOM_LINTERS_DIR

Instance Attribute Summary

Attributes inherited from ERBLint::Linter

#config, #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, #run_and_update_offense_status, support_autocorrect?

Constructor Details

This class inherits a constructor from ERBLint::Linter

Instance Method Details

#autocorrect(_processed_source, offense) ⇒ Object



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

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

#run(processed_source) ⇒ Object



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

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

    if spaces.include?("\t")
      add_offense(
        processed_source.to_source_range(document_pos...(document_pos + spaces.length)),
        "Indent with spaces instead of tabs.",
        spaces.gsub("\t", " " * @config.tab_width),
      )
    end

    document_pos += line.length + 1
  end
end