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

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/haml_lint/linter/classes_before_ids.rb', line 12

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

  first, second = attribute_prefix_order

  (1...components.count).each do |index|
    next unless components[index].start_with?(first) &&
                components[index - 1].start_with?(second)

    record_lint(node, 'Classes should be listed before IDs '\
                      "(#{components[index]} should precede #{components[index - 1]})")
    break
  end
end