Skip to content

Commit c5e3bdd

Browse files
committed
Apply review suggestion
1 parent e20db21 commit c5e3bdd

3 files changed

Lines changed: 81 additions & 19 deletions

File tree

src/language-css/parser-postcss.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -303,26 +303,46 @@ function parseNestedCSS(node, options) {
303303
// `postcss@8` parse custom properties as `css-decl`
304304
if (options.parser === "css" && node.type === "css-decl") {
305305
const { prop, value } = node;
306-
if (
307-
prop.startsWith("--") &&
308-
value.startsWith("{") &&
309-
value.endsWith("}")
310-
) {
311-
const textBefore = options.originalText.slice(
312-
0,
313-
node.source.start.offset
314-
);
315-
const nodeText =
316-
"a".repeat(prop.length) +
317-
options.originalText.slice(
318-
node.source.start.offset + prop.length,
319-
node.source.end.offset + 1
306+
if (prop.startsWith("--") && value.startsWith("{")) {
307+
let rules;
308+
if (value.endsWith("}")) {
309+
const textBefore = options.originalText.slice(
310+
0,
311+
node.source.start.offset
320312
);
321-
const fakeContent = textBefore.replace(/[^\n]/g, " ") + nodeText;
322-
node.value = {
323-
type: "css-rule",
324-
nodes: parseCss(fakeContent, [], { ...options }).nodes[0].nodes,
325-
};
313+
const nodeText =
314+
"a".repeat(prop.length) +
315+
options.originalText.slice(
316+
node.source.start.offset + prop.length,
317+
node.source.end.offset + 1
318+
);
319+
const fakeContent = textBefore.replace(/[^\n]/g, " ") + nodeText;
320+
let ast;
321+
try {
322+
ast = parseCss(fakeContent, [], { ...options });
323+
} catch (_) {
324+
// noop
325+
}
326+
if (
327+
ast &&
328+
ast.nodes &&
329+
ast.nodes.length === 1 &&
330+
ast.nodes[0].type === "css-rule"
331+
) {
332+
rules = ast.nodes[0].nodes;
333+
}
334+
}
335+
if (rules) {
336+
node.value = {
337+
type: "css-rule",
338+
nodes: rules,
339+
};
340+
} else {
341+
node.value = {
342+
type: "value-unknown",
343+
value: node.raws.value.raw
344+
}
345+
}
326346
return node;
327347
}
328348
}

tests/css/variables/__snapshots__/jsfmt.spec.js.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ printWidth: 80
2323
--without-semi: {color:red;}
2424
}
2525
26+
:root {
27+
--like-a-apply-rule: {
28+
color:red;} /* no semi here*/
29+
--another-prop: blue;
30+
}
31+
32+
:root {
33+
--like-a-apply-rule: {
34+
color:red;} /* no semi here*/
35+
--another-one-like-a-apply-rule: {
36+
color:red;
37+
};
38+
}
39+
2640
=====================================output=====================================
2741
/* http://tabatkins.github.io/specs/css-apply-rule/#defining */
2842
@@ -43,6 +57,20 @@ printWidth: 80
4357
};
4458
}
4559
60+
:root {
61+
--like-a-apply-rule: {
62+
color:red;} /* no semi here*/
63+
--another-prop: blue;
64+
}
65+
66+
:root {
67+
--like-a-apply-rule: {
68+
color:red;} /* no semi here*/
69+
--another-one-like-a-apply-rule: {
70+
color:red;
71+
};
72+
}
73+
4674
================================================================================
4775
`;
4876

tests/css/variables/apply-rule.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,17 @@
1414
:root {
1515
--without-semi: {color:red;}
1616
}
17+
18+
:root {
19+
--like-a-apply-rule: {
20+
color:red;} /* no semi here*/
21+
--another-prop: blue;
22+
}
23+
24+
:root {
25+
--like-a-apply-rule: {
26+
color:red;} /* no semi here*/
27+
--another-one-like-a-apply-rule: {
28+
color:red;
29+
};
30+
}

0 commit comments

Comments
 (0)