class HamlLint::Linter::RepeatedId

Detects repeated instances of an element ID in a file

Constants

MESSAGE_FORMAT

Attributes

id_map[R]

Public Instance Methods

visit_root(_node) click to toggle source
# File lib/haml_lint/linter/repeated_id.rb, line 10
def visit_root(_node)
  @id_map = Hash.new { |hash, key| hash[key] = [] }
end
visit_tag(node) click to toggle source
# File lib/haml_lint/linter/repeated_id.rb, line 14
def visit_tag(node)
  id = node.tag_id
  return unless id && !id.empty?

  nodes = (id_map[id] << node)
  case nodes.size
  when 1 then nil
  when 2 then add_lints_for_first_duplications(nodes)
  else add_lint(node, id)
  end
end

Private Instance Methods

add_lint(node, id) click to toggle source
# File lib/haml_lint/linter/repeated_id.rb, line 30
def add_lint(node, id)
  record_lint(node, MESSAGE_FORMAT % id)
end
add_lints_for_first_duplications(nodes) click to toggle source
# File lib/haml_lint/linter/repeated_id.rb, line 34
def add_lints_for_first_duplications(nodes)
  nodes.each { |node| add_lint(node, node.tag_id) }
end