class SCSSLint::Linter::NameFormat

Checks the format of declared names of functions, mixins, variables, and placeholders.

Constants

CONVENTIONS
FUNCTION_WHITELIST

Public Instance Methods

visit_extend(node) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 7
def visit_extend(node)
  check_placeholder(node)
end
visit_function(node) { || ... } click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 11
def visit_function(node)
  check_name(node, 'function')
  yield # Continue into content block of this function definition
end
visit_mixin(node) { || ... } click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 16
def visit_mixin(node)
  check_name(node, 'mixin') unless FUNCTION_WHITELIST.include?(node.name)
  yield # Continue into content block of this mixin's block
end
visit_mixindef(node) { || ... } click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 21
def visit_mixindef(node)
  check_name(node, 'mixin')
  yield # Continue into content block of this mixin definition
end
visit_script_funcall(node) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 26
def visit_script_funcall(node)
  check_name(node, 'function') unless FUNCTION_WHITELIST.include?(node.name)
end
visit_script_variable(node) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 30
def visit_script_variable(node)
  check_name(node, 'variable')
end
visit_variable(node) { || ... } click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 34
def visit_variable(node)
  check_name(node, 'variable')
  yield # Continue into expression tree for this variable definition
end

Private Instance Methods

check_name(node, node_type, node_text = node.name) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 48
def check_name(node, node_type, node_text = node.name)
  node_text = trim_underscore_prefix(node_text)
  return unless violation = violated_convention(node_text, node_type)

  add_lint(node,
           "Name of #{node_type} `#{node_text}` #{violation[:explanation]}")
end
check_placeholder(node) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 66
def check_placeholder(node)
  extract_string_selectors(node.selector).any? do |selector_str|
    check_name(node, 'placeholder', selector_str.gsub('%', ''))
  end
end
convention_explanation(type) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 108
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/name_format.rb, line 102
def convention_name(type)
  config["#{type}_convention"] ||
    config['convention'] ||
    'hyphenated_lowercase'
end
trim_underscore_prefix(name) click to toggle source

Removes underscore prefix from name if leading underscores are allowed.

# File lib/scss_lint/linter/name_format.rb, line 57
def trim_underscore_prefix(name)
  if config['allow_leading_underscore']
    # Remove if there is a single leading underscore
    name = name.gsub(/^_(?!_)/, '')
  end

  name
end
violated_convention(name_string, type) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 88
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