class SCSSLint::Linter::SelectorFormat

Checks that selector names use a specified convention

Constants

CONVENTIONS

Public Instance Methods

visit_attribute(attribute) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 12
def visit_attribute(attribute)
  check(attribute, 'attribute') unless @ignored_types.include?('attribute')
end
visit_class(klass) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 16
def visit_class(klass)
  check(klass, 'class') unless @ignored_types.include?('class')
end
visit_element(element) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 20
def visit_element(element)
  check(element, 'element') unless @ignored_types.include?('element')
end
visit_id(id) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 24
def visit_id(id)
  check(id, 'id') unless @ignored_types.include?('id')
end
visit_placeholder(placeholder) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 28
def visit_placeholder(placeholder)
  check(placeholder, 'placeholder') unless @ignored_types.include?('placeholder')
end
visit_root(_node) { || ... } click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 6
def visit_root(_node)
  @ignored_names = Array(config['ignored_names']).to_set
  @ignored_types = Array(config['ignored_types']).to_set
  yield
end

Private Instance Methods

check(node, type) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 34
def check(node, type)
  name = node.name

  return if @ignored_names.include?(name)
  return unless violation = violated_convention(name, type)

  add_lint(node, "Selector `#{name}` #{violation[:explanation]}")
end
convention_explanation(type) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 93
def convention_explanation(type)
  existing_convention = CONVENTIONS[convention_name(type)]

  config["#{type}_convention_explanation"] ||
    config['convention_explanation'] ||
    (existing_convention && existing_convention[:explanation]) ||
    "should match regex /#{convention_name(type)}/"
end
convention_name(type) click to toggle source
# File lib/scss_lint/linter/selector_format.rb, line 87
def convention_name(type)
  config["#{type}_convention"] ||
    config['convention'] ||
    'hyphenated_lowercase'
end
violated_convention(name_string, type) click to toggle source

Checks the given name and returns the violated convention if it failed.

# File lib/scss_lint/linter/selector_format.rb, line 73
def violated_convention(name_string, type)
  convention_name = convention_name(type)

  existing_convention = CONVENTIONS[convention_name]

  convention = (existing_convention || {
    validator: ->(name) { name =~ /#{convention_name}/ }
  }).merge(
    explanation: convention_explanation(type), # Allow explanation to be customized
  )

  convention unless convention[:validator].call(name_string)
end