MCPcopy
hub / github.com/vitejs/vite / walk

Function walk

packages/vite/src/node/ssr/ssrTransform.ts:452–641  ·  view source on GitHub ↗

* Same logic from \@vue/compiler-core & \@vue/compiler-sfc * Except this is using acorn AST

(
  root: ESTree.Node,
  { onIdentifier, onImportMeta, onDynamicImport, onStatements }: Visitors,
)

Source from the content-addressed store, hash-verified

450 * Except this is using acorn AST
451 */
452function walk(
453 root: ESTree.Node,
454 { onIdentifier, onImportMeta, onDynamicImport, onStatements }: Visitors,
455) {
456 const parentStack: ESTree.Node[] = []
457 const varKindStack: ESTree.VariableDeclaration['kind'][] = []
458 const scopeMap = new WeakMap<ESTree.Node, Set<string>>()
459 const identifiers: [id: any, stack: ESTree.Node[]][] = []
460
461 const setScope = (node: ESTree.Node, name: string) => {
462 let scopeIds = scopeMap.get(node)
463 if (scopeIds && scopeIds.has(name)) {
464 return
465 }
466 if (!scopeIds) {
467 scopeIds = new Set()
468 scopeMap.set(node, scopeIds)
469 }
470 scopeIds.add(name)
471 }
472
473 function isInScope(name: string, parents: ESTree.Node[]) {
474 return parents.some((node) => scopeMap.get(node)?.has(name))
475 }
476 function handlePattern(p: ESTree.ParamPattern, parentScope: ESTree.Node) {
477 if (p.type === 'Identifier') {
478 setScope(parentScope, p.name)
479 } else if (p.type === 'RestElement') {
480 handlePattern(p.argument, parentScope)
481 } else if (p.type === 'ObjectPattern') {
482 p.properties.forEach((property) => {
483 if (property.type === 'RestElement') {
484 setScope(
485 parentScope,
486 (property.argument as ESTree.IdentifierName).name,
487 )
488 } else {
489 handlePattern(property.value, parentScope)
490 }
491 })
492 } else if (p.type === 'ArrayPattern') {
493 p.elements.forEach((element) => {
494 if (element) {
495 handlePattern(element, parentScope)
496 }
497 })
498 } else if (p.type === 'AssignmentPattern') {
499 handlePattern(p.left, parentScope)
500 } else {
501 setScope(parentScope, (p as any).name)
502 }
503 }
504
505 ;(eswalk as any)(root, {
506 enter(node: ESTree.Node, parent: ESTree.Node | null) {
507 if (node.type === 'ImportDeclaration') {
508 return this.skip()
509 }

Callers 2

stripInternalTypesFunction · 0.85
ssrTransformScriptFunction · 0.85

Calls 2

isInScopeFunction · 0.85
onIdentifierFunction · 0.85

Tested by

no test coverage detected