scss-lint icon indicating copy to clipboard operation
scss-lint copied to clipboard

Feature Request: NecessaryParentReference

Open ifyoumakeit opened this issue 9 years ago • 4 comments

Regarding the use of combinators, ampersand, and UnnecessaryParentReference.

SCSS shouldn't really compile ~ & because it creates invalid CSS. UnnecessaryParentReference leads developers to drop the ampersands proceeding a combinator, despite SCSS not doing what it's supposed to do in the ~ & case. The parent selector is nice, but maybe there should be an additional linter to use & ~ & when the second value starts with an ampersand.

SCSS

.foo {  
  & ~ & {
    background: green; // Valid
  }
  ~ & {
    background: black; // Invalid
  }
}

CSS

.foo ~ .foo {
  background: green;
}
~ .foo {
  background: black;
}

Issue brought up with SASS: https://github.com/sass/sass/issues/1836

Maybe NecessaryParentReference?

ifyoumakeit avatar Sep 17 '15 15:09 ifyoumakeit

Edited and changed title for clarity.

ifyoumakeit avatar Sep 17 '15 15:09 ifyoumakeit

Hey @ifyoumakeit, this request makes sense to me. It's a quirky behavior of Sass that ultimately makes sense but is initially confusing. Thanks for the suggestion—happy to merge a pull request with the implementation.

sds avatar Sep 24 '15 18:09 sds

+1

I have a similar case where i do the following:

  li {
    .....

    & + li::before {
      .....
    }
  }

SCSS-lint is complaining about the use of the 2nd li as it thinks it is an unnecessary parent selector, but if i replace it with & my CSS doesn't work anymore.

wouter-muller avatar May 03 '16 14:05 wouter-muller

btw, thanks for this linter, it's really helpful!

I've also have similar issue with ember-component-css

the rule of usage is:

& {  // ampersand refers to the component itself (parent selector)
  padding: 2px;
}

https://github.com/ebryn/ember-component-css#usage

but linter is raised the offense:

Unnecessary parent selector (&) 

well it's broke the build because CC is using this linter: https://docs.codeclimate.com/docs/scss-lint

Mifrill avatar Nov 26 '19 21:11 Mifrill