Class: HamlLint::Linter::ClassesBeforeIds

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

Overview

Checks that classes are listed before IDs in tags.

Constant Summary collapse

TYPES_BY_PREFIX =

Map of prefixes to the type of tag component

{
  '.' => :class,
  '#' => :id,
}.freeze
MSG =
'%s should be listed before %s (%s should precede %s)'

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


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

def visit_tag(node)
  # Convert ".class#id" into [.class, #id] (preserving order)
  components = node.static_attributes_source.scan(/[.#][^.#]+/)

  first, second = attribute_prefix_order

  components.each_cons(2) do |current_val, next_val|
    next unless next_val.start_with?(first) &&
                current_val.start_with?(second)

    failure_message = format(MSG, *(attribute_type_order + [next_val, current_val]))
    record_lint(node, failure_message)
    break
  end
end