Home / Function/ walk() — vite Function Reference

walk() — vite Function Reference

Architecture documentation for the walk() function in ssrTransform.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  1f4d9016_899e_0522_098a_8c1386655467["walk()"]
  20c20aaf_ad5b_4014_72b5_c8262a2b5be1["ssrTransform.ts"]
  1f4d9016_899e_0522_098a_8c1386655467 -->|defined in| 20c20aaf_ad5b_4014_72b5_c8262a2b5be1
  027164d9_0d90_6487_4866_e21cb9d3e6c1["ssrTransformScript()"]
  027164d9_0d90_6487_4866_e21cb9d3e6c1 -->|calls| 1f4d9016_899e_0522_098a_8c1386655467
  17df6204_81d2_952b_8111_6794dd397a83["isRefIdentifier()"]
  1f4d9016_899e_0522_098a_8c1386655467 -->|calls| 17df6204_81d2_952b_8111_6794dd397a83
  31cff562_ac44_b48d_c084_7531a8d9bad5["isFunction()"]
  1f4d9016_899e_0522_098a_8c1386655467 -->|calls| 31cff562_ac44_b48d_c084_7531a8d9bad5
  2b93a6ae_6e37_7ef0_8a33_c7464a1d9301["findParentScope()"]
  1f4d9016_899e_0522_098a_8c1386655467 -->|calls| 2b93a6ae_6e37_7ef0_8a33_c7464a1d9301
  122f5b8d_d979_5ce0_1eb2_0fc3202bf625["isStaticPropertyKey()"]
  1f4d9016_899e_0522_098a_8c1386655467 -->|calls| 122f5b8d_d979_5ce0_1eb2_0fc3202bf625
  187a5d06_bb60_8444_0c20_315795220552["setIsNodeInPattern()"]
  1f4d9016_899e_0522_098a_8c1386655467 -->|calls| 187a5d06_bb60_8444_0c20_315795220552
  style 1f4d9016_899e_0522_098a_8c1386655467 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/ssr/ssrTransform.ts lines 482–668

function walk(
  root: Node,
  { onIdentifier, onImportMeta, onDynamicImport, onStatements }: Visitors,
) {
  const parentStack: Node[] = []
  const varKindStack: VariableDeclaration['kind'][] = []
  const scopeMap = new WeakMap<_Node, Set<string>>()
  const identifiers: [id: any, stack: Node[]][] = []

  const setScope = (node: _Node, name: string) => {
    let scopeIds = scopeMap.get(node)
    if (scopeIds && scopeIds.has(name)) {
      return
    }
    if (!scopeIds) {
      scopeIds = new Set()
      scopeMap.set(node, scopeIds)
    }
    scopeIds.add(name)
  }

  function isInScope(name: string, parents: Node[]) {
    return parents.some((node) => scopeMap.get(node)?.has(name))
  }
  function handlePattern(p: Pattern, parentScope: _Node) {
    if (p.type === 'Identifier') {
      setScope(parentScope, p.name)
    } else if (p.type === 'RestElement') {
      handlePattern(p.argument, parentScope)
    } else if (p.type === 'ObjectPattern') {
      p.properties.forEach((property) => {
        if (property.type === 'RestElement') {
          setScope(parentScope, (property.argument as Identifier).name)
        } else {
          handlePattern(property.value, parentScope)
        }
      })
    } else if (p.type === 'ArrayPattern') {
      p.elements.forEach((element) => {
        if (element) {
          handlePattern(element, parentScope)
        }
      })
    } else if (p.type === 'AssignmentPattern') {
      handlePattern(p.left, parentScope)
    } else {
      setScope(parentScope, (p as any).name)
    }
  }

  ;(eswalk as any)(root, {
    enter(node: Node, parent: Node | null) {
      if (node.type === 'ImportDeclaration') {
        return this.skip()
      }

      // for nodes that can contain multiple statements
      if (
        node.type === 'Program' ||
        node.type === 'BlockStatement' ||
        node.type === 'StaticBlock'
      ) {
        onStatements(node.body as Node[])
      } else if (node.type === 'SwitchCase') {
        onStatements(node.consequent as Node[])
      }

      // track parent stack, skip for "else-if"/"else" branches as acorn nests
      // the ast within "if" nodes instead of flattening them
      if (
        parent &&
        !(parent.type === 'IfStatement' && node === parent.alternate)
      ) {
        parentStack.unshift(parent)
      }

      // track variable declaration kind stack used by VariableDeclarator
      if (node.type === 'VariableDeclaration') {
        varKindStack.unshift(node.kind)
      }

Domain

Subdomains

Frequently Asked Questions

What does walk() do?
walk() is a function in the vite codebase, defined in packages/vite/src/node/ssr/ssrTransform.ts.
Where is walk() defined?
walk() is defined in packages/vite/src/node/ssr/ssrTransform.ts at line 482.
What does walk() call?
walk() calls 5 function(s): findParentScope, isFunction, isRefIdentifier, isStaticPropertyKey, setIsNodeInPattern.
What calls walk()?
walk() is called by 1 function(s): ssrTransformScript.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free