Skip to content

Commit 80bf0fd

Browse files
committed
preserve symmetry when walking css ast
1 parent bff7dac commit 80bf0fd

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/compiler/compile/css/Stylesheet.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -269,24 +269,19 @@ export default class Stylesheet {
269269

270270
this.has_styles = true;
271271

272-
const stack: Array<Rule | Atrule> = [];
272+
const stack: Array<Atrule> = [];
273+
let depth = 0;
273274
let current_atrule: Atrule = null;
274275

275276
walk(ast.css, {
276277
enter: (node: Node) => {
277278
if (node.type === 'Atrule') {
278-
const last = stack[stack.length - 1];
279-
280279
const atrule = new Atrule(node);
281280
stack.push(atrule);
282281

283-
// this is an awkward special case — @apply (and
284-
// possibly other future constructs)
285-
if (last && !(last instanceof Atrule)) return;
286-
287282
if (current_atrule) {
288283
current_atrule.children.push(atrule);
289-
} else {
284+
} else if (depth <= 1) {
290285
this.children.push(atrule);
291286
}
292287

@@ -303,26 +298,24 @@ export default class Stylesheet {
303298

304299
if (node.type === 'Rule') {
305300
const rule = new Rule(node, this, current_atrule);
306-
stack.push(rule);
307301

308302
if (current_atrule) {
309303
current_atrule.children.push(rule);
310-
} else {
304+
} else if (depth <= 1) {
311305
this.children.push(rule);
312306
}
313307
}
308+
309+
depth += 1;
314310
},
315311

316312
leave: (node: Node) => {
317-
if (node.type === 'Rule' || node.type === 'Atrule') stack.pop();
318313
if (node.type === 'Atrule') {
319-
current_atrule = null;
320-
for (let i = stack.length - 1; i >= 0; i--) {
321-
if (stack[i] instanceof Atrule) {
322-
current_atrule = stack[i] as Atrule;
323-
}
324-
}
314+
stack.pop();
315+
current_atrule = stack[stack.length - 1];
325316
}
317+
318+
depth -= 1;
326319
}
327320
});
328321
} else {

0 commit comments

Comments
 (0)