Skip to content

Commit b75f5ae

Browse files
Merge pull request #11 from csstools/backport-postcss-nesting-changes
Backport postcss nesting changes
2 parents 3fccd36 + a8c8c11 commit b75f5ae

20 files changed

+144
-221
lines changed

plugins/postcss-nesting/.bin/test-tape.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ export default async function tape() {
2121
failures += await test('ignores invalid entries', { basename: 'ignore' })
2222
failures += await test('supports complex entries', { basename: 'complex' })
2323
failures += await test('supports all spec examples', { basename: 'spec-examples' })
24-
failures += await test('supports spec compliant mixing of nested rules and declarations', { basename: 'mixing-nested-rules-and-declarations-spec' })
25-
failures += await test('supports legacy mixing of nested rules and declarations', { basename: 'mixing-nested-rules-and-declarations-legacy', pluginOptions: { allowDeclarationsAfterNestedRules: true } })
2624

27-
let mixinPlugin = () => {
25+
let mixinPluginRule = () => {
2826
return {
2927
postcssPlugin: 'mixin',
3028
AtRule: {
@@ -34,20 +32,35 @@ export default async function tape() {
3432
},
3533
}
3634
}
37-
mixinPlugin.postcss = true
38-
failures += await test('supports other visitors', { basename: 'mixin' }, mixinPlugin)
35+
36+
mixinPluginRule.postcss = true
37+
failures += await test('supports other visitors (mixin rule)', { basename: 'mixin-rule' }, mixinPluginRule)
38+
39+
let mixinPluginDeclaration = () => {
40+
return {
41+
postcssPlugin: 'mixin',
42+
AtRule: {
43+
mixin(node) {
44+
node.replaceWith('color: blue;')
45+
},
46+
},
47+
}
48+
}
49+
50+
mixinPluginDeclaration.postcss = true
51+
failures += await test('supports other visitors (mixin declaration)', { basename: 'mixin-declaration' }, mixinPluginDeclaration)
3952

4053
return failures === 0
4154
}
4255

4356
async function test(name, init, ...plugins) {
44-
const { basename, pluginOptions } = Object(init)
57+
const { basename } = Object(init)
4558

4659
let sourceUrl = new URL(`test/${basename}.css`, workingUrl)
4760
let expectUrl = new URL(`test/${basename}.expect.css`, workingUrl)
4861
let resultUrl = new URL(`test/${basename}.result.css`, workingUrl)
4962

50-
plugins.unshift(plugin(pluginOptions))
63+
plugins.unshift(plugin())
5164

5265
let sourceCss = await fs.readFile(sourceUrl, 'utf8')
5366
let expectCss = await fs.readFile(expectUrl, 'utf8')

plugins/postcss-nesting/CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# Changes to PostCSS Nesting
22

3+
### 10.0.1 (November 17, 2021)
4+
5+
- Removed: Support for `allowDeclarationsAfterNestedRules`.
6+
7+
We've realised that enforcing this rule from the spec was going to be problematic
8+
in the long run given how plugins work and the whole ecosystem around mixins and
9+
other features. Treating this as a patch given that this was introduced in the
10+
previous version and was starting to break projects that were using other features.
11+
12+
### 10.0.0 (November 16, 2021)
13+
14+
- Added: Support for `allowDeclarationsAfterNestedRules` to deviate from spec.
15+
- Added: `.npmrc` file.
16+
17+
- Updated: Aligning completely with the [spec](https://www.w3.org/TR/css-nesting-1/) updates.
18+
- Updated: `esbuild` to 0.13.12 (minor)
19+
20+
- Removed: Support for PostCSS 7
21+
22+
### 9.0.0 (October 27, 2021)
23+
24+
- Added: Support for Deno
25+
- Fixed: Issue with PostCSS 8 compatibility using the RuleExit listener
26+
327
### 8.0.1 (May 1, 2021)
428

529
- Fixed: Compatibility issue with CommonJS.

plugins/postcss-nesting/README.md

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
[![Support Chat][git-img]][git-url]
77

88
[PostCSS Nesting] lets you nest style rules inside each other, following the
9-
[CSS Nesting] specification.
9+
[CSS Nesting] specification. If you want nested rules the same way [Sass] works
10+
you might want to use [PostCSS Nested] instead.
1011

1112
```pcss
1213
a, b {
@@ -71,64 +72,11 @@ import postcssNesting from "https://deno.land/x/postcss_nesting/mod.js";
7172
await postcss([postcssNesting]).process(YOUR_CSS /*, processOptions */);
7273
```
7374

74-
## Options
75+
### ⚠️ Spec disclaimer
7576

76-
### allowDeclarationsAfterNestedRules
77-
78-
The [specification](https://www.w3.org/TR/css-nesting-1/#mixing) does not allow declarations after nested rules.
79-
This was previously supported by this plugin and can be re-enabled with `allowDeclarationsAfterNestedRules`.
80-
81-
Before :
82-
83-
```css
84-
a {
85-
color: red;
86-
87-
& b {
88-
color: white;
89-
}
90-
91-
padding: 20px;
92-
}
93-
```
94-
95-
After **without** the option :
96-
97-
```js
98-
postcssNesting()
99-
```
100-
101-
```css
102-
a {
103-
color: red;
104-
}
105-
106-
a b {
107-
color: white;
108-
}
109-
```
110-
111-
After **with** the option :
112-
113-
```js
114-
postcssNesting({
115-
allowDeclarationsAfterNestedRules: true
116-
})
117-
```
118-
119-
```css
120-
a {
121-
color: red;
122-
}
123-
124-
a b {
125-
color: white;
126-
}
127-
128-
a {
129-
padding: 20px;
130-
}
131-
```
77+
The [CSS Nesting Module] spec states on nesting that "Declarations occuring after a nested rule are invalid and ignored.".
78+
While we think it makes sense on browsers, enforcing this at the plugin level introduces several constrains that would
79+
interfere with PostCSS' plugin nature such as with `@mixin`
13280

13381
[cli-img]: https://img.shields.io/travis/csstools/postcss-nesting.svg
13482
[cli-url]: https://travis-ci.org/csstools/postcss-nesting
@@ -143,3 +91,6 @@ a {
14391
[PostCSS]: https://github.com/postcss/postcss
14492
[PostCSS Nesting]: https://github.com/jonathantneal/postcss-nesting
14593
[Deno]: https://deno.land/x/postcss_nesting
94+
[PostCSS Nested]: https://github.com/postcss/postcss-nested
95+
[Sass]: https://sass-lang.com/
96+
[CSS Nesting Module]: https://www.w3.org/TR/css-nesting-1/

plugins/postcss-nesting/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "postcss-nesting",
33
"description": "Nest rules inside each other in CSS",
44
"license": "CC0-1.0",
5-
"version": "10.0.0",
5+
"version": "10.0.1",
66
"type": "module",
77
"main": "dist/index.cjs",
88
"module": "dist/index.mjs",

plugins/postcss-nesting/src/index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
import ensureCorrectMixingOfNestingRulesAndDeclarations from './lib/mixing-nesting-rules-and-declarations.js'
21
import walk from './lib/walk.js'
32

43
/**
5-
* @param {{allowDeclarationsAfterNestedRules?: boolean}} opts
64
* @returns {import('postcss').Plugin}
75
*/
8-
export default function postcssNesting(opts) {
9-
const allowDeclarationsAfterNestedRules = Object(opts).allowDeclarationsAfterNestedRules || false
10-
6+
export default function postcssNesting() {
117
return {
128
postcssPlugin: 'postcss-nesting',
139
Rule(rule) {
14-
if (!allowDeclarationsAfterNestedRules) {
15-
ensureCorrectMixingOfNestingRulesAndDeclarations(rule)
16-
}
1710
walk(rule)
1811
},
1912
}

plugins/postcss-nesting/src/lib/mixing-nesting-rules-and-declarations.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

plugins/postcss-nesting/test/basic.expect.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ a {
2424
}
2525
}
2626

27+
a {
28+
29+
order: 5;
30+
order: 6
31+
}
32+
2733
a b {
2834
order: 7;
2935
}
@@ -37,6 +43,11 @@ a b {
3743
order: 9;
3844
}
3945

46+
a {
47+
48+
order: 10
49+
}
50+
4051
body a {
4152
order: 11
4253
}
@@ -50,6 +61,11 @@ body a {
5061
order: 13
5162
}
5263

64+
a {
65+
66+
order: 14
67+
}
68+
5369
@media screen {
5470

5571
a {

plugins/postcss-nesting/test/direct.expect.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ a, b {
1010
:is(a,b) c, :is(a,b) d {
1111
order: 4;
1212
}
13+
a, b {
14+
order: 5;
15+
}
1316
a, b {
1417
order: 1;
1518
}
@@ -22,3 +25,6 @@ a, b {
2225
:is(a,b) c, :is(a,b) d {
2326
order: 4;
2427
}
28+
a, b {
29+
order: 5;
30+
}

plugins/postcss-nesting/test/empty.expect.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ d e {
1111
f g {
1212
order: 4;
1313
}
14+
f {
15+
order: 5;
16+
}
1417
a b c {
1518
order: 1
1619
}
@@ -23,3 +26,6 @@ d e {
2326
f g {
2427
order: 4;
2528
}
29+
f {
30+
order: 5;
31+
}

plugins/postcss-nesting/test/ignore.expect.css

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
a, b {
2-
order: 1
3-
}
4-
a, b {
2+
order: 1;
53
c, d {
64
order: 2;
75
}
@@ -13,9 +11,7 @@ f:is(g) {
1311
order: 5;
1412
}
1513
a, b {
16-
order: 1
17-
}
18-
a, b {
14+
order: 1;
1915
@nest c, d {
2016
order: 2;
2117
}

0 commit comments

Comments
 (0)