Skip to content

Commit 4213fdf

Browse files
committed
fix: #110 node.nodes can be empty for some rules
1 parent 74ee93b commit 4213fdf

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

examples/css-imports/local.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
@import url("https://unpkg.com/[email protected]/orange.min.css");
22
@import url(./nested.css);
33

4+
@layer x01, x02;
5+
@layer x01 {
6+
.test {
7+
--test-x01: green;
8+
}
9+
}
10+
@layer x02 {
11+
.test {
12+
--test-x01: red;
13+
}
14+
}
15+
416
:root {
517
--gap-sm: 0.25rem;
618
--gap-md: 0.5rem;

src/parser.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export function getVariableDeclarations(
192192
if (isNodeType<Rule>(node, SUPPORTED_CSS_RULE_TYPES[0])) {
193193
// For proper theming, following filter condition should be improved
194194
const [theme] = config.themes.filter(theme => node.selector.match(theme));
195-
if (!config.excludeThemedVariables || !theme) {
195+
if (node.nodes && (!config.excludeThemedVariables || !theme)) {
196196
for (const _node of node.nodes) {
197197
const decls = getVariableDeclarations(config, _node, {
198198
...options,
@@ -208,9 +208,11 @@ export function getVariableDeclarations(
208208
isNodeType<AtRule>(node, SUPPORTED_CSS_RULE_TYPES[2]) &&
209209
SUPPORTED_EVALUATING_ATRULES.has(node.name)
210210
) {
211-
for (const _node of node.nodes) {
212-
const decls = getVariableDeclarations(config, _node, options);
213-
declarations = declarations.concat(decls);
211+
if (node.nodes) {
212+
for (const _node of node.nodes) {
213+
const decls = getVariableDeclarations(config, _node, options);
214+
declarations = declarations.concat(decls);
215+
}
214216
}
215217
}
216218

src/test/at-rules.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@layer x01, x02;
2+
@layer x02 {
3+
.test {
4+
color: firebrick;
5+
}
6+
}
7+
@layer x01 {
8+
.test {
9+
color: limegreen;
10+
}
11+
}
12+
13+
:root {
14+
--red100: #f00;
15+
--red500: #f24455;
16+
}

src/test/parser.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { fetchAndCacheAsset } from "../remote-paths";
1111
const MODIFIED_DATE = new Date("2021-04-12T08:58:58.676Z");
1212
const DUMMY_FILE = path.resolve("src", "test", "touch.css");
1313
const RENAMED_FILE = path.resolve("src", "test", "renamed.css");
14+
const AT_RULES_CSS = path.resolve("src", "test", "at-rules.css");
1415
const BROKEN_FILE = path.resolve("src", "test", "broken.css");
1516
const IMPORT_BASE = path.resolve("src", "test", "css-imports");
1617
const IMPORT_CSS_FILE = path.resolve(IMPORT_BASE, "import.css");
@@ -153,6 +154,19 @@ describe("Test Parser", () => {
153154
await parseFiles(updatedConfig);
154155
expect(fetchAndCacheAsset).toHaveBeenCalledTimes(0);
155156
});
157+
158+
it("Should parse at-rules", async () => {
159+
// Updated config should contain the latest renamed file name.
160+
const updatedConfig: ConfigRecord = {
161+
[CACHE.activeRootPath]: {
162+
...EXTENSION_CONFIG[CACHE.activeRootPath],
163+
files: [getLocalCSSVarLocation(AT_RULES_CSS)],
164+
},
165+
};
166+
CACHE.config = updatedConfig;
167+
const [_, errorPaths] = await parseFiles(updatedConfig);
168+
expect(errorPaths.length).toBe(0);
169+
});
156170
});
157171

158172
describe("parseFiles handle improper CSS Files", () => {

0 commit comments

Comments
 (0)