Skip to content

Commit 62a5b85

Browse files
authored
Improve @at-root behavior (#147)
* tests: Normalize expected and actual strings to reduce noise * style: Add @ts-check and JSDoc type definitions to surface errors * tests: Add more passing tests * tests: Add failing tests for valid uses of `@at-root` * style: Rename functions and variables for clarity * rafactor: Resolve non-critical/false-positive type-checking issues * fix: Logic/typing error when handling @at-root rules * refactor: Simplify existing logic * fix: Move all preceeding comments with rule * fix: `@layer` blocks should also bubble * fix: Correctly handle `with`/`without` parameters on `@at-root` * feat: Add option `rootRuleName` to rename the custom `@at-root` rule * style: Auto formatting/linting * fix: Remove hasRootRule optimization * fix: Add back hasRootRule optimization, scoped to root node As per examples given in the postcss docs: https://github.com/postcss/postcss/blob/main/docs/writing-a-plugin.md#step-4-change-nodes * docs: Update README * style: Reformat test input * tests: Add more `@at-rule` test, some failing * fix: Failing `@at-root` edge cases * style: Fix typo in function name, remove console.log * refactor: Remove @ts-check hack code path, use type assertion instead * test: Remove comments * refactor: Use root#walkAtRules * chore: Tweak eslint rule value * refactor: Fix linting errors … Not sure how/why this didn't show up yesterday or the day before. VSCode's ESLint plugin must have silently crashed or something.
1 parent 2e52ada commit 62a5b85

File tree

6 files changed

+882
-102
lines changed

6 files changed

+882
-102
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Change Log
22
This project adheres to [Semantic Versioning](http://semver.org/).
33

4+
## Upcoming...
5+
* ... <!-- Add new lines here. -->
6+
* fix: Correctly handle `with`/`without` parameters on `@at-root`
7+
* feat: Add option `rootRuleName` to rename the custom `@at-root` rule
8+
* fix: Errors when handling sibling `@at-root` rule blocks
9+
* fix: Move all preceeding comments with rule
10+
* fix: `@layer` blocks should also bubble
11+
412
## 5.0.6
513
* Fixed custom at-rules nesting (by @bsak-shell).
614

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ module.exports = {
119119

120120
### `bubble`
121121

122-
By default, plugin will bubble only `@media` and `@supports` at-rules.
123-
You can add your custom at-rules to this list by `bubble` option:
122+
By default, plugin will bubble only `@media`, `@supports` and `@layer`
123+
at-rules. Use this option to add your custom at-rules to this list.
124124

125125
```js
126126
postcss([ require('postcss-nested')({ bubble: ['phone'] }) ])
@@ -196,3 +196,27 @@ Will be compiled to:
196196
```
197197

198198
This is especially useful if you want to export the empty classes with `postcss-modules`.
199+
200+
201+
### `rootRuleName`
202+
203+
The plugin supports the SCSS custom at-rule `@at-root` which breaks rule
204+
blocks out of their nested position. If you want, you can choose a new
205+
custom name for this rule in your code.
206+
207+
```js
208+
postcss([ require('postcss-nested')({ rootRuleName: '_escape-nesting' }) ])
209+
```
210+
211+
```css
212+
/* input */
213+
.a {
214+
color: white;
215+
@_escape-nesting {
216+
.b { color: black; }
217+
}
218+
}
219+
/* output */
220+
.a { color: white; }
221+
.b { color: black; }
222+
```

index.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { PluginCreator } from 'postcss'
66
declare namespace nested {
77
interface Options {
88
/**
9-
* By default, plugin will bubble only `@media` and `@supports` at-rules.
10-
* You can add your custom at-rules to this list by this option.
9+
* By default, plugin will bubble only `@media`, `@supports` and `@layer`
10+
* at-rules. Use this option to add your custom at-rules to this list.
1111
*/
1212
bubble?: string[]
1313

@@ -24,6 +24,13 @@ declare namespace nested {
2424
* to preserve them.
2525
*/
2626
preserveEmpty?: boolean
27+
28+
/**
29+
* The plugin supports the SCSS custom at-rule `@at-root` which breaks
30+
* rule blocks out of their nested position. If you want, you can choose
31+
* a new custom name for this rule in your code.
32+
*/
33+
rootRuleName?: string
2734
}
2835

2936
type Nested = PluginCreator<Options>

0 commit comments

Comments
 (0)