Skip to content

Commit

Permalink
fix(ssr): handle object destructure alias, close #6222 (#6224)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 21, 2021
1 parent dd40438 commit 1d97ec3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,30 @@ function c({ _ = bar() + foo() }) {}
"
`)
})

test('object destructure alias', async () => {
expect(
(
await ssrTransform(
`
import { n } from 'foo'
const a = () => {
const { type: n = 'bar' } = {}
console.log(n)
}
`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
const a = () => {
const { type: n = 'bar' } = {}
console.log(n)
}
"
`)
})
5 changes: 5 additions & 0 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ function walk(
node.id.properties.forEach((property) => {
if (property.type === 'RestElement') {
setScope(parentFunction, (property.argument as Identifier).name)
} else if (property.value.type === 'AssignmentPattern') {
setScope(
parentFunction,
(property.value.left as Identifier).name
)
} else {
setScope(parentFunction, (property.value as Identifier).name)
}
Expand Down

0 comments on commit 1d97ec3

Please sign in to comment.