Skip to content
This repository has been archived by the owner on Jan 17, 2018. It is now read-only.

Commit

Permalink
Fix UnnecessaryParentReference with multiple parent refs
Browse files Browse the repository at this point in the history
The `UnnecessaryParentReference` linter would incorrectly report lints
for cases like the following:

    p {
      & + & { ... }
    }

In these situations, the first ampersand is required, since if you wrote
something like this:

    p {
      + & { ... }
    }

...the Sass compiler would produce the following invalid CSS:

    + p { ... }

Fix the problem by detecting if there are multiple ampersand references
in the sequence and ignoring it if that is the case.

Change-Id: Ica6042785883e3ed1c76d24dd8a25da5899bdfda
Reviewed-on: http://gerrit.causes.com/41625
Tested-by: jenkins <[email protected]>
Reviewed-by: Shane da Silva <[email protected]>
  • Loading branch information
sds committed Aug 15, 2014
1 parent 2df7f13 commit a06fce5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Add linting of `@include` and `@if`/`@else` blocks in `PropertySortOrder`
* Fix `HexValidation` bug incorrectly reporting lints for 8 character hex
codes in Microsoft `filter` property
* Fix `UnnecessaryParentReference` incorrectly reporting lints for nested
selectors with more than one parent reference

## 0.26.2

Expand Down
6 changes: 6 additions & 0 deletions lib/scss_lint/linter/unnecessary_parent_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def visit_comma_sequence(comma_sequence)
def visit_sequence(sequence)
return unless sequence_starts_with_parent?(sequence.members.first)

# 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.
Expand Down
11 changes: 11 additions & 0 deletions spec/scss_lint/linter/unnecessary_parent_reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@
it { should_not report_lint }
end

context 'when an ampersand precedes a sibling operator' do
let(:css) { <<-CSS }
p {
& + & {}
& ~ & {}
}
CSS

it { should_not report_lint }
end

context 'when an amperand is used in a comma sequence to DRY up code' do
let(:css) { <<-CSS }
p {
Expand Down

0 comments on commit a06fce5

Please sign in to comment.