Skip to content
Open
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
12 changes: 12 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ test('rewrite variables in default value of destructuring params', async () => {
expect(result?.deps).toEqual(['vue'])
})

// #23232
test('rewrite imported binding used as a computed key of a defaulted destructuring param', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A({ [fn]: foo } = {}){ return { [fn]: foo }; }`,
)
expect(result?.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
function A({ [__vite_ssr_import_0__.fn]: foo } = {}){ return { [__vite_ssr_import_0__.fn]: foo }; }"
`)
expect(result?.deps).toEqual(['vue'])
})

test('do not rewrite when function declaration is in scope', async () => {
const result = await ssrTransformSimple(
`import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }`,
Expand Down
13 changes: 11 additions & 2 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,17 @@ function walk(
return this.skip()
}
if (child.type !== 'Identifier') return
// do not record as scope variable if is a destructuring keyword
if (isStaticPropertyKey(child, parent)) return
// do not record as scope variable if it is a destructuring key
// (static or computed) rather than the bound value; a computed
// key like `{ [IMPORTED]: v } = {}` references an outer binding
// and must stay rewritable
if (
parent?.type === 'Property' &&
parent.key === child &&
parent.value !== child
) {
return
}
// do not record if this is a default value
// assignment of a destructuring variable
if (
Expand Down
Loading