class SCSSLint::Linter::NameFormat

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

Constants

CONVENTIONS
CSS_FUNCTION_WHITELIST
SCSS_FUNCTION_WHITELIST

Public Instance Methods

visit_function(node) { || ... } click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 29
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 34
def visit_mixin(node)
  check_name(node, 'mixin') unless whitelist?(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 39
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 44
def visit_script_funcall(node)
  check_name(node, 'function') unless whitelist?(node.name)
  yield # Continue linting any arguments of this function call
end
visit_script_variable(node) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 49
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 53
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 65
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
convention_explanation(type) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 119
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 113
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 74
def trim_underscore_prefix(name)
  if config['allow_leading_underscore']
    # Remove if there is a single leading underscore
    name = name.sub(/^_(?!_)/, '')
  end

  name
end
violated_convention(name_string, type) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 99
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
whitelist?(name) click to toggle source
# File lib/scss_lint/linter/name_format.rb, line 60
def whitelist?(name)
  CSS_FUNCTION_WHITELIST.include?(name) ||
    SCSS_FUNCTION_WHITELIST.include?(name)
end