Class: HamlLint::Linter::StrictLocals

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

Overview

Checks for the presence of a ‘locals` magic comment at the beginning of a partial.

Defined Under Namespace

Classes: DummyNode

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, #run_or_raise, supports_autocorrect?, #supports_autocorrect?

Methods included from HamlVisitor

#visit, #visit_children

Constructor Details

This class inherits a constructor from HamlLint::Linter

Instance Method Details

#visit_root(root) ⇒ true, false

Enables the linter if the tree is for the right file type.

Parameters:

Returns:

  • (true, false)

    whether the linter is enabled for the tree



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

def visit_root(root)
  return unless enabled?(root)

  # Rails technically allows the comment to be anywhere in the file,
  # but as a best practice they should be at the top of the file.
  # https://guides.rubyonrails.org/action_view_overview.html#strict-locals
  # https://github.com/rails/rails/blob/v8.0.2/actionview/lib/action_view/template.rb#L368
  found =
    root.children
        .take_while { |child| child.is_a?(HamlLint::Tree::HamlCommentNode) }
        .any?(&:is_strict_locals?)

  return if found

  record_lint(DummyNode.new(1), failure_message)
end