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

    MESSAGE = 'Unnecessary parent selector'

    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)

      # 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
      simple_sequence.members.size == 1 &&
        first.is_a?(Sass::Selector::Parent) &&
        first.suffix.empty? # Ignore concatenated selectors, like `&-something`
    end
  end
end

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

    MESSAGE = 'Unnecessary parent selector (&)'

    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)

      # 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
      simple_sequence.members.size == 1 &&
        first.is_a?(Sass::Selector::Parent) &&
        first.suffix.empty? # Ignore concatenated selectors, like `&-something`
    end
  end
end

Include ampersand in UnnecessaryParentReference description
Include ampersand in UnnecessaryParentReference description

This will make it more obvious what the linter is complaining about.

Change-Id: I1f73c86d9d970bdc73854dbf150d0f4c1fcf23b0
Reviewed-on: http://gerrit.causes.com/40991
Reviewed-by: Shane da Silva <67fb7bfe39f841b16a1f86d3928acc568d12c716@brigade.com>
Tested-by: jenkins <d95b56ce41a2e1ac4cecdd398defd7414407cc08@causes.com>
Reviewed-by: Henric Trotzig <f5a3082dfc399666895f58d2777281f9ac20aad5@brigade.com>
