Skip to content

Commit f6ca274

Browse files
authored
postcss-nesting : better handling of nested selectors that begin with a letter (#861)
* postcss-nesting : better handling of nested selectors that begin with a letter * Update plugins/postcss-nesting/CHANGELOG.md * Update CHANGELOG.md * fix typo
1 parent 2a52eb9 commit f6ca274

File tree

9 files changed

+37
-19
lines changed

9 files changed

+37
-19
lines changed

plugins/postcss-nesting/CHANGELOG.md

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

3+
### Unreleased (patch)
4+
5+
- Skip nested rules that have a selector that begins with a letter
6+
- Better warning when nested rules have a selector that begins with a letter
7+
38
### 11.2.0 (February 13, 2023)
49

510
- Added: support for `&` at the root

plugins/postcss-nesting/dist/index.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugins/postcss-nesting/dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugins/postcss-nesting/src/lib/merge-selectors/merge-selectors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export default function mergeSelectors(node: Node, postcssResult: Result, fromSe
2929

3030
if (!fromAtNest) {
3131
if (x === 0 && toSelectorAST.nodes?.[0]?.nodes?.[0]?.type === 'tag') {
32-
node.warn(postcssResult, `Nested selectors must start with a symbol : "${toSelectors[x]}"`);
32+
node.warn(postcssResult, `Nested selectors must start with a symbol and "${toSelectors[x]}" begins with a letter.`);
33+
return [];
3334
}
3435
}
3536

plugins/postcss-nesting/src/lib/nest-rule-within-rule.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ import { walkFunc } from './walk-func.js';
77
import { options } from './options.js';
88

99
export default function transformNestRuleWithinRule(node: AtRule, parent: Rule, result: Result, walk: walkFunc, opts: options) {
10+
let selectors = [];
11+
12+
try {
13+
selectors = mergeSelectors(node, result, parent.selectors, comma(node.params), opts, true);
14+
} catch (err) {
15+
node.warn(result, `Failed to parse selectors : "${parent.selector}" / "${node.params}" with message: "${err.message}"`);
16+
return;
17+
}
18+
19+
if (!selectors.length) {
20+
return;
21+
}
22+
1023
// move previous siblings and the node to before the parent
1124
shiftNodesBeforeParent(node, parent);
1225

@@ -15,12 +28,7 @@ export default function transformNestRuleWithinRule(node: AtRule, parent: Rule,
1528
rule.raws.semicolon = true; /* nested rules end with "}" and do not have this flag set */
1629

1730
// update the selectors of the node to be merged with the parent
18-
try {
19-
rule.selectors = mergeSelectors(node, result, parent.selectors, comma(node.params), opts, true);
20-
} catch (err) {
21-
node.warn(result, `Failed to parse selectors : "${parent.selector}" / "${node.params}" with message: "${err.message}"`);
22-
return;
23-
}
31+
rule.selectors = selectors;
2432

2533
// replace the node with the new rule
2634
node.replaceWith(rule);

plugins/postcss-nesting/src/lib/rule-within-rule.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export default function transformRuleWithinRule(node: Rule, parent: Rule, result
1616
return;
1717
}
1818

19+
if (!selectors.length) {
20+
return;
21+
}
22+
1923
// Group all declarations after the first one.
2024
groupDeclarations(parent);
2125

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
a, b {
22
order: 1;
3-
}
4-
a c, b c, a d, b d {
3+
c, d {
54
order: 2;
65
}
6+
}
77
:scope e {
88
order: 3;
99
}

plugins/postcss-nesting/test/ignore.no-is-pseudo-selector.expect.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
a, b {
22
order: 1;
3-
}
4-
a c, b c, a d, b d {
3+
c, d {
54
order: 2;
65
}
6+
}
77
:scope e {
88
order: 3;
99
}

plugins/postcss-nesting/test/invalid-selector.expect.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
}
55
}
66

7-
@nest &.child {
8-
}
9-
107
.foo : bar {
8+
@nest &.child {
9+
order: 2;
10+
}
1111
}
1212

1313
.foo {
@@ -16,8 +16,8 @@
1616
}
1717
}
1818

19-
@nest &.child : bar {
20-
}
21-
2219
.foo {
20+
@nest &.child : bar {
21+
order: 4;
22+
}
2323
}

0 commit comments

Comments
 (0)