Skip to content

postcss-nesting : better handling of nested selectors that begin with a letter #861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/postcss-nesting/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes to PostCSS Nesting

### Unreleased (patch)

- Skip nested rules that have a selector that begins with a letter
- Better warning when nested rules have a selector that begins with a letter

### 11.2.0 (February 13, 2023)

- Added: support for `&` at the root
Expand Down
2 changes: 1 addition & 1 deletion plugins/postcss-nesting/dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugins/postcss-nesting/dist/index.mjs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default function mergeSelectors(node: Node, postcssResult: Result, fromSe

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

Expand Down
20 changes: 14 additions & 6 deletions plugins/postcss-nesting/src/lib/nest-rule-within-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import { walkFunc } from './walk-func.js';
import { options } from './options.js';

export default function transformNestRuleWithinRule(node: AtRule, parent: Rule, result: Result, walk: walkFunc, opts: options) {
let selectors = [];

try {
selectors = mergeSelectors(node, result, parent.selectors, comma(node.params), opts, true);
} catch (err) {
node.warn(result, `Failed to parse selectors : "${parent.selector}" / "${node.params}" with message: "${err.message}"`);
return;
}

if (!selectors.length) {
return;
}

// move previous siblings and the node to before the parent
shiftNodesBeforeParent(node, parent);

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

// update the selectors of the node to be merged with the parent
try {
rule.selectors = mergeSelectors(node, result, parent.selectors, comma(node.params), opts, true);
} catch (err) {
node.warn(result, `Failed to parse selectors : "${parent.selector}" / "${node.params}" with message: "${err.message}"`);
return;
}
rule.selectors = selectors;

// replace the node with the new rule
node.replaceWith(rule);
Expand Down
4 changes: 4 additions & 0 deletions plugins/postcss-nesting/src/lib/rule-within-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export default function transformRuleWithinRule(node: Rule, parent: Rule, result
return;
}

if (!selectors.length) {
return;
}

// Group all declarations after the first one.
groupDeclarations(parent);

Expand Down
4 changes: 2 additions & 2 deletions plugins/postcss-nesting/test/ignore.expect.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
a, b {
order: 1;
}
a c, b c, a d, b d {
c, d {
order: 2;
}
}
:scope e {
order: 3;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
a, b {
order: 1;
}
a c, b c, a d, b d {
c, d {
order: 2;
}
}
:scope e {
order: 3;
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/postcss-nesting/test/invalid-selector.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
}
}

@nest &.child {
}

.foo : bar {
@nest &.child {
order: 2;
}
}

.foo {
Expand All @@ -16,8 +16,8 @@
}
}

@nest &.child : bar {
}

.foo {
@nest &.child : bar {
order: 4;
}
}