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, ERBLint::LinterRegistry::DEPRECATED_CUSTOM_LINTERS_DIR

Instance Attribute Summary

Attributes inherited from ERBLint::Linter

#config, #offenses

Instance Method Summary collapse

Methods included from ERBLint::LinterRegistry

clear, find_by_name, included, linters, load_custom_linters

Methods inherited from ERBLint::Linter

#add_offense, #clear_offenses, #enabled?, #excludes_file?, inherited, #initialize, #run_and_update_offense_status, support_autocorrect?

Constructor Details

This class inherits a constructor from ERBLint::Linter

Instance Method Details

#autocorrect(processed_source, offense) ⇒ Object



48
49
50
51
52
# File 'lib/erb_lint/linters/no_javascript_tag_helper.rb', line 48

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

#run(processed_source) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/erb_lint/linters/no_javascript_tag_helper.rb', line 19

def run(processed_source)
  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

    ruby_node =
      begin
        BetterHtml::TestHelper::RubyNode.parse(source)
      rescue ::Parser::SyntaxError
        nil
      end
    next unless ruby_node

    send_node = ruby_node.descendants(:send).first
    next unless send_node&.method_name?(:javascript_tag)

    add_offense(
      erb_node.loc,
      "Avoid using 'javascript_tag do' as it confuses tests " \
        "that validate html, use inline <script> instead",
      [erb_node, send_node],
    )
  end
end