Sha256: 06ad532c24cef48e08a56b0e19ebd85dcfa83d24241c2fba8821f1834b8ddfc9

Contents?: true

Size: 1.78 KB

Versions: 22

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module SCSSLint
  # Checks for unnecessary uses of the parent reference (&) in nested selectors.
  class Linter::UnnecessaryParentReference < Linter
    include LinterRegistry

    MESSAGE = 'Unnecessary parent selector (&)'.freeze

    def visit_comma_sequence(comma_sequence)
      @multiple_sequences = comma_sequence.members.size > 1
    end

    def visit_sequence(sequence)
      return unless sequence_starts_with_parent?(sequence.members.first)

      # Allow concatentation, e.g.
      # element {
      #   &.foo {}
      # }
      return if sequence.members.first.members.size > 1

      # Allow sequences that contain multiple parent references, e.g.
      # element {
      #   & + & { ... }
      # }
      return if sequence.members[1..-1].any? { |ss| sequence_contains_parent_reference?(ss) }

      # Special case: allow an isolated parent to appear if it is part of a
      # comma sequence of more than one sequence, as this could be used to DRY
      # up code.
      return if @multiple_sequences && isolated_parent?(sequence)

      add_lint(sequence.members.first.line, MESSAGE)
    end

  private

    def isolated_parent?(sequence)
      sequence.members.size == 1 &&
        sequence_starts_with_parent?(sequence.members.first)
    end

    def sequence_starts_with_parent?(simple_sequence)
      return unless simple_sequence.is_a?(Sass::Selector::SimpleSequence)
      first = simple_sequence.members.first
      first.is_a?(Sass::Selector::Parent) &&
        first.suffix.nil? # Ignore concatenated selectors, like `&-something`
    end

    def sequence_contains_parent_reference?(simple_sequence)
      return unless simple_sequence.is_a?(Sass::Selector::SimpleSequence)
      simple_sequence.members.any? { |s| s.is_a?(Sass::Selector::Parent) }
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

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