Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add WalkAction to indicate how the walk function should behave
You can `Continue` its execution (the default behaviour), `Skip` walking
the current node, or `Stop` walking entirely.
  • Loading branch information
RobinMalfait committed Mar 7, 2024
commit 2aac02cf9835706b11ffbb6d69a4b06e4ae82c8a
40 changes: 28 additions & 12 deletions packages/tailwindcss/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,44 @@ export function comment(value: string): Comment {
}
}

export enum WalkAction {
/** Continue walking, which is the default */
Continue,

/** Skip visiting the children of this node */
Skip,

/** Stop the walk entirely */
Stop,
}

export function walk(
ast: AstNode[],
visit: (
node: AstNode,
utils: {
replaceWith(newNode: AstNode | AstNode[]): void
},
) => void | false,
) => void | WalkAction,
) {
for (let i = 0; i < ast.length; i++) {
let node = ast[i]
let shouldContinue = visit(node, {
replaceWith(newNode) {
ast.splice(i, 1, ...(Array.isArray(newNode) ? newNode : [newNode]))
// We want to visit the newly replaced node(s), which start at the current
// index (i). By decrementing the index here, the next loop will process
// this position (containing the replaced node) again.
i--
},
})

if (shouldContinue === false) return
let status =
visit(node, {
replaceWith(newNode) {
ast.splice(i, 1, ...(Array.isArray(newNode) ? newNode : [newNode]))
// We want to visit the newly replaced node(s), which start at the
// current index (i). By decrementing the index here, the next loop
// will process this position (containing the replaced node) again.
i--
},
}) ?? WalkAction.Continue

// Stop the walk entirely
if (status === WalkAction.Stop) return

// Skip visiting the children of this node
if (status === WalkAction.Skip) continue

if (node.kind === 'rule') {
walk(node.nodes, visit)
Expand Down
6 changes: 3 additions & 3 deletions packages/tailwindcss/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Features, transform } from 'lightningcss'
import { version } from '../package.json'
import { comment, decl, rule, toCss, walk, type AstNode, type Rule } from './ast'
import { WalkAction, comment, decl, rule, toCss, walk, type AstNode, type Rule } from './ast'
import { compileCandidates } from './compile'
import * as CSS from './css-parser'
import { buildDesignSystem } from './design-system'
Expand Down Expand Up @@ -29,7 +29,7 @@ export function compile(css: string, rawCandidates: string[]) {
if (node.kind === 'rule' && node.selector.startsWith('@keyframes ')) {
keyframesRules.push(node)
replaceWith([])
return
return WalkAction.Skip
}

if (node.kind !== 'declaration') return
Expand Down Expand Up @@ -94,7 +94,7 @@ export function compile(css: string, rawCandidates: string[]) {
// Stop walking after finding `@tailwind utilities` to avoid walking all
// of the generated CSS. This means `@tailwind utilities` can only appear
// once per file but that's the intended usage at this point in time.
return false
return WalkAction.Stop
}
})

Expand Down