Class: ERBLint::Linters::NoJavascriptTagHelper

Inherits:
ERBLint::Linter show all
Includes:
ERBLint::LinterRegistry
Defined in:
lib/erb_lint/linters/no_javascript_tag_helper.rb

Defined Under Namespace

Classes: ConfigSchema

Constant Summary

Constants included from ERBLint::LinterRegistry

ERBLint::LinterRegistry::CUSTOM_LINTERS_DIR

Instance Method Summary collapse

Methods included from ERBLint::LinterRegistry

find_by_name, included, load_custom_linters

Methods inherited from ERBLint::Linter

#enabled?, #excludes_file?, inherited, #initialize, support_autocorrect?

Constructor Details

This class inherits a constructor from ERBLint::Linter

Instance Method Details

#autocorrect(processed_source, offense) ⇒ Object



44
45
46
47
48
# File 'lib/erb_lint/linters/no_javascript_tag_helper.rb', line 44

def autocorrect(processed_source, offense)
  lambda do |corrector|
    correct_offense(processed_source, offense, corrector)
  end
end

#offenses(processed_source) ⇒ Object



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
# File 'lib/erb_lint/linters/no_javascript_tag_helper.rb', line 18

def offenses(processed_source)
  offenses = []

  parser = processed_source.parser
  parser.ast.descendants(:erb).each do |erb_node|
    indicator_node, _, code_node, _ = *erb_node
    indicator = indicator_node&.loc&.source
    next if indicator == '#'
    source = code_node.loc.source

    next unless (ruby_node = BetterHtml::TestHelper::RubyNode.parse(source))
    send_node = ruby_node.descendants(:send).first
    next unless send_node&.method_name?(:javascript_tag)

    offenses << Offense.new(
      self,
      processed_source.to_source_range(erb_node.loc.start, erb_node.loc.stop),
      "Avoid using 'javascript_tag do' as it confuses tests "\
      "that validate html, use inline <script> instead",
      [erb_node, send_node]
    )
  end

  offenses
end