Skip to content

Commit 6b9c5a5

Browse files
author
Merkle Bonsai
committed
fix: prevent crash on body-less @scope at-rule
1 parent e4f1ae6 commit 6b9c5a5

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,15 +771,20 @@ module.exports = (options = {}) => {
771771
}
772772
}
773773

774-
atRule.nodes.forEach((declaration) => {
775-
if (declaration.type === "decl") {
776-
localizeDeclaration(declaration, {
777-
localAliasMap,
778-
options: options,
779-
global: globalMode,
780-
});
781-
}
782-
});
774+
// Guard matches the non-scope branch below — body-less @scope
775+
// at-rules (or postcss-misparsed inputs) have undefined .nodes;
776+
// unconditional forEach crashes.
777+
if (atRule.nodes) {
778+
atRule.nodes.forEach((declaration) => {
779+
if (declaration.type === "decl") {
780+
localizeDeclaration(declaration, {
781+
localAliasMap,
782+
options: options,
783+
global: globalMode,
784+
});
785+
}
786+
});
787+
}
783788
} else if (atRule.nodes) {
784789
atRule.nodes.forEach((declaration) => {
785790
if (declaration.type === "decl") {

test/index.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,15 @@ html {
21682168
}
21692169
`,
21702170
},
2171+
// Body-less @scope at-rules (e.g. `@scope (.foo);`) have `atRule.nodes`
2172+
// === undefined; the unconditional `atRule.nodes.forEach(...)` in the
2173+
// @scope branch threw `Cannot read properties of undefined`. The
2174+
// non-scope at-rule branch has the same guard.
2175+
{
2176+
name: "@scope at-rule — body-less @scope no longer crashes",
2177+
input: `@scope (.foo);`,
2178+
expected: `@scope (:local(.foo));`,
2179+
},
21712180
// The `to` keyword is case-insensitive per CSS keyword rules.
21722181
{
21732182
name: "@scope at-rule — uppercase TO keyword (#90)",

0 commit comments

Comments
 (0)