diff --git a/packages/tailwindcss/src/walk.ts b/packages/tailwindcss/src/walk.ts index ccb384256..97c30db90 100644 --- a/packages/tailwindcss/src/walk.ts +++ b/packages/tailwindcss/src/walk.ts @@ -52,42 +52,53 @@ export function walk( else walkImplementation(ast, hooks.enter, hooks.exit) } +interface Stack { + value: T + prev: Stack | null +} + function walkImplementation( ast: T[], enter: (node: T, ctx: VisitContext) => EnterResult | void = () => WalkAction.Continue, exit: (node: T, ctx: VisitContext) => ExitResult | void = () => WalkAction.Continue, ) { - let stack: [nodes: T[], offset: number, parent: Parent | null][] = [[ast, 0, null]] + type StackFrame = [nodes: T[], offset: number, parent: Parent | null] + let stack: Stack | null = { value: [ast, 0, null], prev: null } + let ctx: VisitContext = { parent: null, depth: 0, path() { let path: T[] = [] - for (let i = 1; i < stack.length; i++) { - let parent = stack[i][2] + let frames: Stack | null = stack + + while (frames) { + let parent = frames.value[2] if (parent) path.push(parent) + frames = frames.prev } + path.reverse() + return path }, } - while (stack.length > 0) { - let depth = stack.length - 1 - let frame = stack[depth] + while (stack !== null) { + let frame = stack.value let nodes = frame[0] let offset = frame[1] let parent = frame[2] // Done with this level if (offset >= nodes.length) { - stack.pop() + stack = stack.prev + ctx.depth -= 1 continue } ctx.parent = parent - ctx.depth = depth // Enter phase (offsets are positive) if (offset >= 0) { @@ -97,7 +108,11 @@ function walkImplementation( switch (result.kind) { case WalkKind.Continue: { if (node.nodes && node.nodes.length > 0) { - stack.push([node.nodes, 0, node as Parent]) + ctx.depth += 1 + stack = { + value: [node.nodes, 0, node as Parent], + prev: stack, + } } frame[1] = ~offset // Prepare for exit phase, same offset