Class: ERBLint::Linters::SpaceAroundErbTag
- Inherits:
-
ERBLint::Linter
- Object
- ERBLint::Linter
- ERBLint::Linters::SpaceAroundErbTag
- Includes:
- ERBLint::LinterRegistry
- Defined in:
- lib/erb_lint/linters/space_around_erb_tag.rb
Overview
Enforce a single space after ‘<%` and before `%>` in the erb source. This linter ignores opening erb tags (`<%`) that are followed by a newline, and closing erb tags (`%>`) that are preceeded by a newline.
Constant Summary collapse
- START_SPACES =
/\A([[:space:]]*)/m
- END_SPACES =
/([[:space:]]*)\z/m
Constants included from ERBLint::LinterRegistry
ERBLint::LinterRegistry::CUSTOM_LINTERS_DIR, ERBLint::LinterRegistry::DEPRECATED_CUSTOM_LINTERS_DIR
Instance Attribute Summary
Attributes inherited from ERBLint::Linter
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
60 61 62 63 64 |
# File 'lib/erb_lint/linters/space_around_erb_tag.rb', line 60 def autocorrect(_processed_source, offense) lambda do |corrector| corrector.replace(offense.source_range, offense.context) end end |
#run(processed_source) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/erb_lint/linters/space_around_erb_tag.rb', line 14 def run(processed_source) processed_source.ast.descendants(:erb).each do |erb_node| indicator_node, ltrim, code_node, rtrim = *erb_node indicator = indicator_node&.loc&.source next if indicator == "#" || indicator == "%" code = code_node.children.first start_spaces = code.match(START_SPACES)&.captures&.first || "" if start_spaces.size != 1 && !start_spaces.include?("\n") add_offense( code_node.loc.resize(start_spaces.size), "Use 1 space after `<%#{indicator}#{ltrim&.loc&.source}` " \ "instead of #{start_spaces.size} space#{"s" if start_spaces.size > 1}.", " ", ) elsif start_spaces.count("\n") > 1 lines = start_spaces.split("\n", -1) add_offense( code_node.loc.resize(start_spaces.size), "Use 1 newline after `<%#{indicator&.loc&.source}#{ltrim&.loc&.source}` " \ "instead of #{start_spaces.count("\n")}.", "#{lines.first}\n#{lines.last}", ) end end_spaces = code.match(END_SPACES)&.captures&.first || "" if end_spaces.size != 1 && !end_spaces.include?("\n") add_offense( code_node.loc.end.adjust(begin_pos: -end_spaces.size), "Use 1 space before `#{rtrim&.loc&.source}%>` " \ "instead of #{end_spaces.size} space#{"s" if start_spaces.size > 1}.", " ", ) elsif end_spaces.count("\n") > 1 lines = end_spaces.split("\n", -1) add_offense( code_node.loc.end.adjust(begin_pos: -end_spaces.size), "Use 1 newline before `#{rtrim&.loc&.source}%>` " \ "instead of #{end_spaces.count("\n")}.", "#{lines.first}\n#{lines.last}", ) end end end |