Skip to content

Commit 87b48f9

Browse files
authoredOct 9, 2022
fix(ssr): track var as function scope (#10388)
1 parent 8c4df1f commit 87b48f9

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
 

‎packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,27 @@ export class Test {
787787
Object.defineProperty(__vite_ssr_exports__, \\"Test\\", { enumerable: true, configurable: true, get(){ return Test }});;"
788788
`)
789789
})
790+
791+
// #10386
792+
test('track var scope by function', async () => {
793+
expect(
794+
await ssrTransformSimpleCode(`
795+
import { foo, bar } from 'foobar'
796+
function test() {
797+
if (true) {
798+
var foo = () => { var why = 'would' }, bar = 'someone'
799+
}
800+
return [foo, bar]
801+
}`)
802+
).toMatchInlineSnapshot(`
803+
"
804+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");
805+
806+
function test() {
807+
if (true) {
808+
var foo = () => { var why = 'would' }, bar = 'someone'
809+
}
810+
return [foo, bar]
811+
}"
812+
`)
813+
})

‎packages/vite/src/node/ssr/ssrTransform.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
Identifier,
66
Pattern,
77
Property,
8+
VariableDeclaration,
89
Node as _Node
910
} from 'estree'
1011
import { extract_names as extractNames } from 'periscopic'
@@ -316,6 +317,7 @@ function walk(
316317
{ onIdentifier, onImportMeta, onDynamicImport }: Visitors
317318
) {
318319
const parentStack: Node[] = []
320+
const varKindStack: VariableDeclaration['kind'][] = []
319321
const scopeMap = new WeakMap<_Node, Set<string>>()
320322
const identifiers: [id: any, stack: Node[]][] = []
321323

@@ -375,6 +377,11 @@ function walk(
375377
parentStack.unshift(parent)
376378
}
377379

380+
// track variable declaration kind stack used by VariableDeclarator
381+
if (node.type === 'VariableDeclaration') {
382+
varKindStack.unshift(node.kind)
383+
}
384+
378385
if (node.type === 'MetaProperty' && node.meta.name === 'import') {
379386
onImportMeta(node)
380387
} else if (node.type === 'ImportExpression') {
@@ -434,7 +441,10 @@ function walk(
434441
// mark property in destructuring pattern
435442
setIsNodeInPattern(node)
436443
} else if (node.type === 'VariableDeclarator') {
437-
const parentFunction = findParentScope(parentStack)
444+
const parentFunction = findParentScope(
445+
parentStack,
446+
varKindStack[0] === 'var'
447+
)
438448
if (parentFunction) {
439449
handlePattern(node.id, parentFunction)
440450
}
@@ -449,6 +459,10 @@ function walk(
449459
) {
450460
parentStack.shift()
451461
}
462+
463+
if (node.type === 'VariableDeclaration') {
464+
varKindStack.shift()
465+
}
452466
}
453467
})
454468

@@ -538,8 +552,12 @@ function isFunction(node: _Node): node is FunctionNode {
538552

539553
const scopeNodeTypeRE =
540554
/(?:Function|Class)(?:Expression|Declaration)$|Method$|^IfStatement$/
541-
function findParentScope(parentStack: _Node[]): _Node | undefined {
542-
return parentStack.find((i) => scopeNodeTypeRE.test(i.type))
555+
function findParentScope(
556+
parentStack: _Node[],
557+
isVar = false
558+
): _Node | undefined {
559+
const regex = isVar ? functionNodeTypeRE : scopeNodeTypeRE
560+
return parentStack.find((i) => regex.test(i.type))
543561
}
544562

545563
function isInDestructuringAssignment(

0 commit comments

Comments
 (0)