Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from lxsymington/enforce-nesting
[#197] - New rule to enforce nesting
- Loading branch information
Showing
4 changed files
with
625 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
# selector-nest-combinators | ||
|
||
Require or disallow nesting of combinators in selectors | ||
|
||
```scss | ||
/* Examples of selectors without nesting of combinators */ | ||
.foo .bar {} | ||
|
||
.foo.bar {} | ||
|
||
.foo > .bar {} | ||
|
||
.foo:hover {} | ||
|
||
/* Corresponding selectors with combinators nested */ | ||
.foo { | ||
.bar {} | ||
} | ||
|
||
.foo { | ||
&.bar {} | ||
} | ||
|
||
.foo { | ||
& > .bar {} | ||
} | ||
|
||
.foo { | ||
&:hover {} | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
`string`: `"always"|"never"` | ||
|
||
### `"always"` | ||
|
||
*Every combinator* in a selector *must be* nested where possible without altering the existing resolved selector. | ||
|
||
Sections of selectors preceding a parent selector are ignored with `always`. | ||
e.g. | ||
|
||
```scss | ||
.foo { | ||
.bar.baz & {} | ||
} | ||
``` | ||
|
||
Sections of selectors within pseudo selectors are also ignored with `always`. | ||
e.g. | ||
|
||
```scss | ||
.foo { | ||
&:not(.bar .baz) {} | ||
} | ||
``` | ||
|
||
while this could be refactored to: | ||
|
||
```scss | ||
.bar { | ||
.baz { | ||
.foo:not(&) {} | ||
} | ||
} | ||
``` | ||
|
||
There are variances in the way this is compiled between compilers, therefore for the purposes of this rule the selector sections within pseudo selectors are being ignored. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```scss | ||
.foo .bar {} | ||
``` | ||
|
||
```scss | ||
.foo.bar {} | ||
``` | ||
|
||
```scss | ||
.foo > .bar {} | ||
``` | ||
|
||
```scss | ||
.foo:hover {} | ||
``` | ||
|
||
```scss | ||
a[href] {} | ||
``` | ||
|
||
```scss | ||
* + li {} | ||
``` | ||
|
||
```scss | ||
:nth-child(2n - 1):last-child {} | ||
``` | ||
|
||
The following patterns are *not* considered warnings: | ||
|
||
```scss | ||
.foo { | ||
.bar {} | ||
} | ||
``` | ||
|
||
```scss | ||
.foo { | ||
&.bar {} | ||
} | ||
``` | ||
|
||
```scss | ||
.foo { | ||
& > .bar {} | ||
} | ||
``` | ||
|
||
```scss | ||
.foo { | ||
&:hover {} | ||
} | ||
``` | ||
|
||
```scss | ||
a { | ||
&[href] {} | ||
} | ||
``` | ||
|
||
```scss | ||
* { | ||
& + li {} | ||
} | ||
``` | ||
|
||
```scss | ||
:nth-child(2n - 1) { | ||
&:last-child {} | ||
} | ||
``` | ||
|
||
### `"never"` | ||
|
||
Nested of selectors are not allowed. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```scss | ||
.foo { | ||
.bar {} | ||
} | ||
``` | ||
|
||
```scss | ||
.foo { | ||
&.bar {} | ||
} | ||
``` | ||
|
||
```scss | ||
.foo { | ||
& > .bar {} | ||
} | ||
``` | ||
|
||
```scss | ||
.foo { | ||
&:hover {} | ||
} | ||
``` | ||
|
||
```scss | ||
a { | ||
&[href] {} | ||
} | ||
``` | ||
|
||
```scss | ||
* { | ||
& + li {} | ||
} | ||
``` | ||
|
||
```scss | ||
:nth-child(2n - 1) { | ||
&:last-child {} | ||
} | ||
``` | ||
|
||
The following patterns are *not* considered warnings: | ||
|
||
```scss | ||
.foo .bar {} | ||
``` | ||
|
||
```scss | ||
.foo.bar {} | ||
``` | ||
|
||
```scss | ||
.foo > .bar {} | ||
``` | ||
|
||
```scss | ||
.foo:hover {} | ||
``` | ||
|
||
```scss | ||
a[href] {} | ||
``` | ||
|
||
```scss | ||
* + li {} | ||
``` | ||
|
||
```scss | ||
:nth-child(2n - 1):last-child {} | ||
``` |
Oops, something went wrong.