Class: HamlLint::Linter::UnnecessaryStringOutput

Inherits:
Linter
  • Object
show all
Includes:
HamlLint::LinterRegistry
Defined in:
lib/haml_lint/linter/unnecessary_string_output.rb

Overview

Checks for unnecessary outputting of strings in Ruby script tags.

For example, the following two code snippets are equivalent, but the latter is more concise (and thus preferred):

%tag= "Some #{expression}"
%tag Some #{expression}

Constant Summary collapse

MESSAGE =
'`= "..."` should be rewritten as `...`'

Instance Method Summary collapse

Methods included from HamlLint::LinterRegistry

extract_linters_from, included

Instance Method Details

#visit_script(node) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/haml_lint/linter/unnecessary_string_output.rb', line 27

def visit_script(node)
  # Some script nodes created by the Haml parser aren't actually script
  # nodes declared via the `=` marker. Check for it.
  return unless /\A\s*=/.match?(node.source_code)
  return unless plain = safe_plain_text(node.script)

  record_lint(node, MESSAGE, corrected: correct_script(node, plain))
end

#visit_tag(node) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/haml_lint/linter/unnecessary_string_output.rb', line 19

def visit_tag(node)
  return unless tag_has_inline_script?(node) && inline_content_is_string?(node)

  plain = safe_plain_text(node.script)
  corrected = plain ? correct_tag(node, plain) : false
  record_lint(node, MESSAGE, corrected: corrected)
end