preserve symmetry when walking css ast

pull/3150/head
Rich Harris 5 years ago
parent bff7dace5b
commit 80bf0fd152

@ -269,24 +269,19 @@ export default class Stylesheet {
this.has_styles = true;
const stack: Array<Rule | Atrule> = [];
const stack: Array<Atrule> = [];
let depth = 0;
let current_atrule: Atrule = null;
walk(ast.css, {
enter: (node: Node) => {
if (node.type === 'Atrule') {
const last = stack[stack.length - 1];
const atrule = new Atrule(node);
stack.push(atrule);
// this is an awkward special case — @apply (and
// possibly other future constructs)
if (last && !(last instanceof Atrule)) return;
if (current_atrule) {
current_atrule.children.push(atrule);
} else {
} else if (depth <= 1) {
this.children.push(atrule);
}
@ -303,26 +298,24 @@ export default class Stylesheet {
if (node.type === 'Rule') {
const rule = new Rule(node, this, current_atrule);
stack.push(rule);
if (current_atrule) {
current_atrule.children.push(rule);
} else {
} else if (depth <= 1) {
this.children.push(rule);
}
}
depth += 1;
},
leave: (node: Node) => {
if (node.type === 'Rule' || node.type === 'Atrule') stack.pop();
if (node.type === 'Atrule') {
current_atrule = null;
for (let i = stack.length - 1; i >= 0; i--) {
if (stack[i] instanceof Atrule) {
current_atrule = stack[i] as Atrule;
}
}
stack.pop();
current_atrule = stack[stack.length - 1];
}
depth -= 1;
}
});
} else {

Loading…
Cancel
Save