class SCSSLint::Linter::NestingDepth

Checks for rule sets nested deeper than a specified maximum depth.

Constants

IGNORED_SELECTORS

Public Instance Methods

visit_root(_node) { || ... } click to toggle source
# File lib/scss_lint/linter/nesting_depth.rb, line 8
def visit_root(_node)
  @max_depth = config['max_depth']
  @depth = 1
  yield # Continue linting children
end
visit_rule(node) { || ... } click to toggle source
# File lib/scss_lint/linter/nesting_depth.rb, line 14
def visit_rule(node)
  return yield if ignore_selectors?(node)

  if @depth > @max_depth
    add_lint node, "Nesting should be no greater than #{@max_depth}, " \
                   "but was #{@depth}"
  else
    # Only continue if we didn't exceed the max depth already (this makes
    # the lint less noisy)
    @depth += 1
    yield # Continue linting children
    @depth -= 1
  end
end

Private Instance Methods

ignore_selectors?(node) click to toggle source
# File lib/scss_lint/linter/nesting_depth.rb, line 31
def ignore_selectors?(node)
  return unless config['ignore_parent_selectors']
  return unless node.parsed_rules

  simple_selectors(node.parsed_rules).all? do |selector|
    IGNORED_SELECTORS.include?(selector.class)
  end
end
simple_selectors(node) click to toggle source
# File lib/scss_lint/linter/nesting_depth.rb, line 40
def simple_selectors(node)
  node.members.flat_map(&:members).reject do |simple_sequence|
    simple_sequence.is_a?(String)
  end.flat_map(&:members)
end