Skip to content

Commit 485e298

Browse files
authored
fix(ssr): nested destucture (vitejs#6249)
1 parent a96bdd9 commit 485e298

File tree

2 files changed

+91
-20
lines changed

2 files changed

+91
-20
lines changed

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

+65
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,68 @@ const a = () => {
433433
"
434434
`)
435435
})
436+
437+
test('nested object destructure alias', async () => {
438+
expect(
439+
(
440+
await ssrTransform(
441+
`
442+
import { remove, add, get, set, rest, objRest } from 'vue'
443+
444+
function a() {
445+
const {
446+
o: { remove },
447+
a: { b: { c: [ add ] }},
448+
d: [{ get }, set, ...rest],
449+
...objRest
450+
} = foo
451+
452+
remove()
453+
add()
454+
get()
455+
set()
456+
rest()
457+
objRest()
458+
}
459+
460+
remove()
461+
add()
462+
get()
463+
set()
464+
rest()
465+
objRest()
466+
`,
467+
null,
468+
null
469+
)
470+
).code
471+
).toMatchInlineSnapshot(`
472+
"
473+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
474+
475+
476+
function a() {
477+
const {
478+
o: { remove },
479+
a: { b: { c: [ add ] }},
480+
d: [{ get }, set, ...rest],
481+
...objRest
482+
} = foo
483+
484+
remove()
485+
add()
486+
get()
487+
set()
488+
rest()
489+
objRest()
490+
}
491+
492+
__vite_ssr_import_0__.remove()
493+
__vite_ssr_import_0__.add()
494+
__vite_ssr_import_0__.get()
495+
__vite_ssr_import_0__.set()
496+
__vite_ssr_import_0__.rest()
497+
__vite_ssr_import_0__.objRest()
498+
"
499+
`)
500+
})

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

+26-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import type {
66
Identifier,
77
Node as _Node,
88
Property,
9-
Function as FunctionNode
9+
Function as FunctionNode,
10+
Pattern
1011
} from 'estree'
1112
import { extract_names as extractNames } from 'periscopic'
1213
import { walk as eswalk } from 'estree-walker'
@@ -339,26 +340,31 @@ function walk(
339340
} else if (node.type === 'VariableDeclarator') {
340341
const parentFunction = findParentFunction(parentStack)
341342
if (parentFunction) {
342-
if (node.id.type === 'ObjectPattern') {
343-
node.id.properties.forEach((property) => {
344-
if (property.type === 'RestElement') {
345-
setScope(parentFunction, (property.argument as Identifier).name)
346-
} else if (property.value.type === 'AssignmentPattern') {
347-
setScope(
348-
parentFunction,
349-
(property.value.left as Identifier).name
350-
)
351-
} else {
352-
setScope(parentFunction, (property.value as Identifier).name)
353-
}
354-
})
355-
} else if (node.id.type === 'ArrayPattern') {
356-
node.id.elements.filter(Boolean).forEach((element) => {
357-
setScope(parentFunction, (element as Identifier).name)
358-
})
359-
} else {
360-
setScope(parentFunction, (node.id as Identifier).name)
343+
const handlePattern = (p: Pattern) => {
344+
if (p.type === 'Identifier') {
345+
setScope(parentFunction, p.name)
346+
} else if (p.type === 'RestElement') {
347+
handlePattern(p.argument)
348+
} else if (p.type === 'ObjectPattern') {
349+
p.properties.forEach((property) => {
350+
if (property.type === 'RestElement') {
351+
setScope(
352+
parentFunction,
353+
(property.argument as Identifier).name
354+
)
355+
} else handlePattern(property.value)
356+
})
357+
} else if (p.type === 'ArrayPattern') {
358+
p.elements.forEach((element) => {
359+
if (element) handlePattern(element)
360+
})
361+
} else if (p.type === 'AssignmentPattern') {
362+
handlePattern(p.left)
363+
} else {
364+
setScope(parentFunction, (p as any).name)
365+
}
361366
}
367+
handlePattern(node.id)
362368
}
363369
}
364370
},

0 commit comments

Comments
 (0)