Skip to content

Commit c071eb3

Browse files
authoredMay 1, 2024··
fix(ssr): handle class declaration and expression name scoping (#16569)
1 parent 02db947 commit c071eb3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
 

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

+31
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,37 @@ test('do not rewrite when function expression is in global scope', async () => {
271271
`)
272272
})
273273

274+
test('do not rewrite when class declaration is in scope', async () => {
275+
const result = await ssrTransformSimple(
276+
`import { cls } from 'vue';function A(){ class cls {} return { cls }; }`,
277+
)
278+
expect(result?.code).toMatchInlineSnapshot(`
279+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["cls"]});
280+
function A(){ class cls {} return { cls }; }"
281+
`)
282+
expect(result?.deps).toEqual(['vue'])
283+
})
284+
285+
test('do not rewrite when class expression is in scope', async () => {
286+
const result = await ssrTransformSimple(
287+
`import { cls } from './vue';var a = function() { return class cls { constructor() { console.log(cls) } } }`,
288+
)
289+
expect(result?.code).toMatchInlineSnapshot(`
290+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
291+
var a = function() { return class cls { constructor() { console.log(cls) } } }"
292+
`)
293+
})
294+
295+
test('do not rewrite when class expression is in global scope', async () => {
296+
const result = await ssrTransformSimple(
297+
`import { cls } from './vue';foo(class cls { constructor() { console.log(cls) } })`,
298+
)
299+
expect(result?.code).toMatchInlineSnapshot(`
300+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
301+
foo(class cls { constructor() { console.log(cls) } })"
302+
`)
303+
})
304+
274305
test('do not rewrite catch clause', async () => {
275306
const result = await ssrTransformSimple(
276307
`import {error} from './dependency';try {} catch(error) {}`,

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ function walk(
439439
if (node.type === 'FunctionDeclaration') {
440440
const parentScope = findParentScope(parentStack)
441441
if (parentScope) {
442-
setScope(parentScope, node.id!.name)
442+
setScope(parentScope, node.id.name)
443443
}
444444
}
445445
// If it is a function expression, its name (if exist) could also be
@@ -479,6 +479,15 @@ function walk(
479479
},
480480
})
481481
})
482+
} else if (node.type === 'ClassDeclaration') {
483+
// A class declaration name could shadow an import, so add its name to the parent scope
484+
const parentScope = findParentScope(parentStack)
485+
if (parentScope) {
486+
setScope(parentScope, node.id.name)
487+
}
488+
} else if (node.type === 'ClassExpression' && node.id) {
489+
// A class expression name could shadow an import, so add its name to the scope
490+
setScope(node, node.id.name)
482491
} else if (node.type === 'Property' && parent!.type === 'ObjectPattern') {
483492
// mark property in destructuring pattern
484493
setIsNodeInPattern(node)

0 commit comments

Comments
 (0)
Please sign in to comment.