Class: SCSSLint::Linter::UnnecessaryParentReference

Inherits:
SCSSLint::Linter
  • Object
show all
Includes:
SCSSLint::LinterRegistry
Defined in:
lib/scss_lint/linter/unnecessary_parent_reference.rb

Overview

Checks for unnecessary uses of the parent reference (&) in nested selectors.

Constant Summary collapse

MESSAGE =
'Unnecessary parent selector (&)'

Constants included from Utils

Utils::COLOR_REGEX

Instance Attribute Summary

Attributes inherited from SCSSLint::Linter

#config, #engine, #lints

Instance Method Summary collapse

Methods included from SCSSLint::LinterRegistry

extract_linters_from, included

Methods inherited from SCSSLint::Linter

#initialize, #name, #run

Methods included from Utils

#color?, #color_hex?, #color_keyword?, #color_keyword_to_code, #extract_string_selectors, #node_ancestor, #node_siblings, #pluralize, #previous_node, #remove_quoted_strings, #same_position?

Methods included from SelectorVisitor

#visit_selector

Constructor Details

This class inherits a constructor from SCSSLint::Linter

Instance Method Details

#visit_comma_sequence(comma_sequence) ⇒ Object



8
9
10
# File 'lib/scss_lint/linter/unnecessary_parent_reference.rb', line 8

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

#visit_sequence(sequence) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/scss_lint/linter/unnecessary_parent_reference.rb', line 12

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_starts_with_parent?(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