Class: HamlLint::Linter::UnnecessaryStringOutput

Inherits:
HamlLint::Linter 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 `...`'.freeze

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_script(node) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/haml_lint/linter/unnecessary_string_output.rb', line 22

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 if node.source_code !~ /\A\s*=/

  if outputs_string_literal?(node)
    record_lint(node, MESSAGE)
  end
end

#visit_tag(node) ⇒ Object



16
17
18
19
20
# File 'lib/haml_lint/linter/unnecessary_string_output.rb', line 16

def visit_tag(node)
  if tag_has_inline_script?(node) && inline_content_is_string?(node)
    record_lint(node, MESSAGE)
  end
end