Class: ERBLint::Linters::SpaceInHtmlTag

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

Overview

Detects extra or missing whitespace in html tags.

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



38
39
40
41
42
# File 'lib/erb_lint/linters/space_in_html_tag.rb', line 38

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

#run(processed_source) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/erb_lint/linters/space_in_html_tag.rb', line 9

def run(processed_source)
  processed_source.ast.descendants(:tag).each do |tag_node|
    start_solidus, name, attributes, end_solidus = *tag_node

    next_loc = name&.loc&.begin_pos || attributes&.loc&.begin_pos ||
      end_solidus&.loc&.begin_pos || (tag_node.loc.end_pos - 1)
    if start_solidus
      no_space(processed_source, (tag_node.loc.begin_pos + 1)...start_solidus.loc.begin_pos)
      no_space(processed_source, start_solidus.loc.end_pos...next_loc)
    else
      no_space(processed_source, (tag_node.loc.begin_pos + 1)...next_loc)
    end

    if attributes
      single_space_or_newline(processed_source, name.loc.end_pos...attributes.loc.begin_pos) if name
      process_attributes(processed_source, attributes)
    end

    previous_loc = attributes&.loc&.end_pos || name&.loc&.end_pos ||
      start_solidus&.loc&.end_pos || (tag_node.loc.begin_pos + 1)
    if end_solidus
      single_space(processed_source, previous_loc...end_solidus.loc.begin_pos)
      no_space(processed_source, end_solidus.loc.end_pos...(tag_node.loc.end_pos - 1))
    else
      no_space(processed_source, previous_loc...(tag_node.loc.end_pos - 1))
    end
  end
end