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

Loading…
Cancel
Save