class HamlLint::Linter::ClassesBeforeIds

Checks that classes are listed before IDs in tags.

Constants

MSG
TYPES_BY_PREFIX

Map of prefixes to the type of tag component

Public Instance Methods

visit_tag(node) click to toggle source
# File lib/haml_lint/linter/classes_before_ids.rb, line 15
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

Private Instance Methods

attribute_prefix_order() click to toggle source
# File lib/haml_lint/linter/classes_before_ids.rb, line 33
def attribute_prefix_order
  default = %w[. #]
  default.reverse! if ids_first?
  default
end
attribute_type_order() click to toggle source
# File lib/haml_lint/linter/classes_before_ids.rb, line 39
def attribute_type_order
  default = %w[Classes IDs]
  default.reverse! if ids_first?
  default
end
enforced_style() click to toggle source
# File lib/haml_lint/linter/classes_before_ids.rb, line 45
def enforced_style
  config.fetch('EnforcedStyle', 'class')
end
ids_first?() click to toggle source
# File lib/haml_lint/linter/classes_before_ids.rb, line 49
def ids_first?
  enforced_style == 'id'
end