Sha256: 4f8c2ecd0ae11902198c042f5b54f7880aae3b4f60396b7958033105876a912a

Contents?: true

Size: 1.23 KB

Versions: 22

Compression:

Stored size: 1.23 KB

Contents

module SCSSLint
  # Checks for rule sets nested deeper than a specified maximum depth.
  class Linter::NestingDepth < Linter
    include LinterRegistry

    IGNORED_SELECTORS = [Sass::Selector::Parent, Sass::Selector::Pseudo].freeze

    def visit_root(_node)
      @max_depth = config['max_depth']
      @depth = 1
      yield # Continue linting children
    end

    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

    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

    def simple_selectors(node)
      node.members.flat_map(&:members).reject do |simple_sequence|
        simple_sequence.is_a?(String)
      end.flat_map(&:members)
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
scss_lint-0.60.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.59.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.58.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.57.1 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.57.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.56.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.55.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.54.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.53.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.52.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.51.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.50.3 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.50.2 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.50.1 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.50.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.49.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.48.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.47.1 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.47.0 lib/scss_lint/linter/nesting_depth.rb
scss_lint-0.46.0 lib/scss_lint/linter/nesting_depth.rb