Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): track var as function scope #10397

Merged
merged 1 commit into from Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -319,6 +320,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 @@ -378,6 +380,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 @@ -437,7 +444,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 @@ -452,6 +462,10 @@ function walk(
) {
parentStack.shift()
}

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

Expand Down Expand Up @@ -541,8 +555,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