Skip to content

Commit

Permalink
fix(ssr): track var as function scope (#10388)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 9, 2022
1 parent 8c4df1f commit 87b48f9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
24 changes: 24 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Expand Up @@ -787,3 +787,27 @@ export class Test {
Object.defineProperty(__vite_ssr_exports__, \\"Test\\", { enumerable: true, configurable: true, get(){ return Test }});;"
`)
})

// #10386
test('track var scope by function', async () => {
expect(
await ssrTransformSimpleCode(`
import { foo, bar } from 'foobar'
function test() {
if (true) {
var foo = () => { var why = 'would' }, bar = 'someone'
}
return [foo, bar]
}`)
).toMatchInlineSnapshot(`
"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foobar\\");
function test() {
if (true) {
var foo = () => { var why = 'would' }, bar = 'someone'
}
return [foo, bar]
}"
`)
})
24 changes: 21 additions & 3 deletions packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -5,6 +5,7 @@ import type {
Identifier,
Pattern,
Property,
VariableDeclaration,
Node as _Node
} from 'estree'
import { extract_names as extractNames } from 'periscopic'
Expand Down Expand Up @@ -316,6 +317,7 @@ function walk(
{ onIdentifier, onImportMeta, onDynamicImport }: Visitors
) {
const parentStack: Node[] = []
const varKindStack: VariableDeclaration['kind'][] = []
const scopeMap = new WeakMap<_Node, Set<string>>()
const identifiers: [id: any, stack: Node[]][] = []

Expand Down Expand Up @@ -375,6 +377,11 @@ function walk(
parentStack.unshift(parent)
}

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

if (node.type === 'MetaProperty' && node.meta.name === 'import') {
onImportMeta(node)
} else if (node.type === 'ImportExpression') {
Expand Down Expand Up @@ -434,7 +441,10 @@ function walk(
// mark property in destructuring pattern
setIsNodeInPattern(node)
} else if (node.type === 'VariableDeclarator') {
const parentFunction = findParentScope(parentStack)
const parentFunction = findParentScope(
parentStack,
varKindStack[0] === 'var'
)
if (parentFunction) {
handlePattern(node.id, parentFunction)
}
Expand All @@ -449,6 +459,10 @@ function walk(
) {
parentStack.shift()
}

if (node.type === 'VariableDeclaration') {
varKindStack.shift()
}
}
})

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

const scopeNodeTypeRE =
/(?:Function|Class)(?:Expression|Declaration)$|Method$|^IfStatement$/
function findParentScope(parentStack: _Node[]): _Node | undefined {
return parentStack.find((i) => scopeNodeTypeRE.test(i.type))
function findParentScope(
parentStack: _Node[],
isVar = false
): _Node | undefined {
const regex = isVar ? functionNodeTypeRE : scopeNodeTypeRE
return parentStack.find((i) => regex.test(i.type))
}

function isInDestructuringAssignment(
Expand Down

0 comments on commit 87b48f9

Please sign in to comment.