Skip to content

Commit 43168ab

Browse files
tomasz-sodzawicznyevilebottnawi
authored andcommitted
fix: don't crash when raws.before is undefined (#60)
1 parent 2bfed2e commit 43168ab

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/extractICSS.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const importPattern = /^:import\(("[^"]*"|'[^']*'|[^"']+)\)$/;
33
const getDeclsObject = rule => {
44
const object = {};
55
rule.walkDecls(decl => {
6-
object[decl.raws.before.trim() + decl.prop] = decl.value;
6+
const before = decl.raws.before ? decl.raws.before.trim() : "";
7+
object[before + decl.prop] = decl.value;
78
});
89
return object;
910
};

test/extractICSS.test.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test("extract :import statements with single quoted path", () => {
2727
});
2828
});
2929

30-
test("extract :import statements with double quoted path", () => {
30+
test("extract manually added :import", () => {
3131
expect(runExtract(':import("./colors.css") {}')).toEqual({
3232
icssImports: {
3333
"./colors.css": {}
@@ -50,6 +50,25 @@ test("not extract :import with values", () => {
5050
});
5151
});
5252

53+
test("extract :import statements manually created in postcss", () => {
54+
const root = postcss.parse("");
55+
root.append(
56+
postcss
57+
.rule({ selector: ":import(./colors.css)" })
58+
.append(postcss.decl({ prop: "i__blue", value: "blue" }))
59+
.append(postcss.decl({ prop: "i__red", value: "red" }))
60+
);
61+
expect(extractICSS(root)).toEqual({
62+
icssImports: {
63+
"./colors.css": {
64+
i__blue: "blue",
65+
i__red: "red"
66+
}
67+
},
68+
icssExports: {}
69+
});
70+
});
71+
5372
test("not extract invalid :import", () => {
5473
expect(runExtract(":import(\\'./colors.css) {}")).toEqual({
5574
icssImports: {},

0 commit comments

Comments
 (0)