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): handle class declaration and expression name scoping #16569

Merged
merged 1 commit into from
May 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,37 @@ test('do not rewrite when function expression is in global scope', async () => {
`)
})

test('do not rewrite when class declaration is in scope', async () => {
const result = await ssrTransformSimple(
`import { cls } from 'vue';function A(){ class cls {} return { cls }; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["cls"]});
function A(){ class cls {} return { cls }; }"
`)
expect(result?.deps).toEqual(['vue'])
})

test('do not rewrite when class expression is in scope', async () => {
const result = await ssrTransformSimple(
`import { cls } from './vue';var a = function() { return class cls { constructor() { console.log(cls) } } }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
var a = function() { return class cls { constructor() { console.log(cls) } } }"
`)
})

test('do not rewrite when class expression is in global scope', async () => {
const result = await ssrTransformSimple(
`import { cls } from './vue';foo(class cls { constructor() { console.log(cls) } })`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
foo(class cls { constructor() { console.log(cls) } })"
`)
})

test('do not rewrite catch clause', async () => {
const result = await ssrTransformSimple(
`import {error} from './dependency';try {} catch(error) {}`,
Expand Down
11 changes: 10 additions & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ function walk(
if (node.type === 'FunctionDeclaration') {
const parentScope = findParentScope(parentStack)
if (parentScope) {
setScope(parentScope, node.id!.name)
setScope(parentScope, node.id.name)
}
}
// If it is a function expression, its name (if exist) could also be
Expand Down Expand Up @@ -479,6 +479,15 @@ function walk(
},
})
})
} else if (node.type === 'ClassDeclaration') {
// A class declaration name could shadow an import, so add its name to the parent scope
const parentScope = findParentScope(parentStack)
if (parentScope) {
setScope(parentScope, node.id.name)
}
} else if (node.type === 'ClassExpression' && node.id) {
// A class expression name could shadow an import, so add its name to the scope
setScope(node, node.id.name)
} else if (node.type === 'Property' && parent!.type === 'ObjectPattern') {
// mark property in destructuring pattern
setIsNodeInPattern(node)
Expand Down