Skip to content

Commit

Permalink
Merge pull request #298 from lxsymington/enforce-nesting
Browse files Browse the repository at this point in the history
[#197] - New rule to enforce nesting
  • Loading branch information
kristerkari committed Jan 13, 2019
2 parents 3d2a23f + 62952e8 commit 8496e03
Show file tree
Hide file tree
Showing 4 changed files with 625 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rules/index.js
Expand Up @@ -36,6 +36,7 @@ import operatorNoNewlineBefore from "./operator-no-newline-before";
import operatorNoUnspaced from "./operator-no-unspaced";
import partialNoImport from "./partial-no-import";
import percentPlaceholderPattern from "./percent-placeholder-pattern";
import selectorNestCombinators from "./selector-nest-combinators";
import selectorNoRedundantNestingSelector from "./selector-no-redundant-nesting-selector";

export default {
Expand Down Expand Up @@ -77,5 +78,6 @@ export default {
"operator-no-unspaced": operatorNoUnspaced,
"percent-placeholder-pattern": percentPlaceholderPattern,
"partial-no-import": partialNoImport,
"selector-nest-combinators": selectorNestCombinators,
"selector-no-redundant-nesting-selector": selectorNoRedundantNestingSelector
};
221 changes: 221 additions & 0 deletions src/rules/selector-nest-combinators/README.md
@@ -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 {}
```

0 comments on commit 8496e03

Please sign in to comment.