Class: HamlLint::Linter::UnnecessaryInterpolation

Inherits:
HamlLint::Linter show all
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 Attribute Summary

Attributes inherited from HamlLint::Linter

#lints

Instance Method Summary collapse

Methods included from HamlLint::LinterRegistry

extract_linters_from, included

Methods inherited from HamlLint::Linter

#initialize, #name, #run

Methods included from HamlVisitor

#visit, #visit_children

Constructor Details

This class inherits a constructor from HamlLint::Linter

Instance Method Details

#visit_tag(node) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/haml_lint/linter/unnecessary_interpolation.rb', line 14

def visit_tag(node)
  return if node.script.length <= 2

  count = 0
  chars = 2 # Include surrounding quote chars
  HamlLint::Utils.extract_interpolated_values(node.script) do |interpolated_code, _line|
    count += 1
    return if count > 1 # rubocop:disable Lint/NonLocalExitFromIterator
    chars += interpolated_code.length + 3
  end

  if chars == node.script.length
    record_lint(node, '`%... \#{expression}` can be written without ' \
                      'interpolation as `%...= expression`')
  end
end