Class: HamlLint::Linter::UnnecessaryInterpolation
- Inherits:
-
Linter
- Object
- Linter
- HamlLint::Linter::UnnecessaryInterpolation
- Includes:
- HamlLint::LinterRegistry
- Defined in:
- lib/haml_lint/linter/unnecessary_interpolation.rb
Overview
Checks for unnecessary uses of string interpolation.
For example, the following two code snippets are equivalent, but the latter is more concise (and thus preferred):
%tag #{expression}
%tag= expression
Instance Method Summary collapse
Methods included from HamlLint::LinterRegistry
extract_linters_from, included
Instance Method Details
#visit_tag(node) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/haml_lint/linter/unnecessary_interpolation.rb', line 16 def visit_tag(node) return if node.script.length <= 2 count = 0 chars = 2 # Include surrounding quote chars interpolated_code = nil HamlLint::Utils.extract_interpolated_values(node.script) do |code, _line| count += 1 return if count > 1 # rubocop:disable Lint/NonLocalExitFromIterator chars += code.length + 3 interpolated_code = code end if chars == node.script.length corrected = correct_interpolation(node, interpolated_code) record_lint(node, '`%... \#{expression}` can be written without ' \ 'interpolation as `%...= expression`', corrected: corrected) end end |