Skip to content

Commit ee7f78f

Browse files
authored
fix(ssr): rename objectPattern dynamic key (fixes #9585) (#9609)
1 parent 9d0b18b commit ee7f78f

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

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

+63
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,30 @@ const a = () => {
400400
}
401401
"
402402
`)
403+
404+
// #9585
405+
expect(
406+
await ssrTransformSimpleCode(
407+
`
408+
import { n, m } from 'foo'
409+
const foo = {}
410+
411+
{
412+
const { [n]: m } = foo
413+
}
414+
`
415+
)
416+
).toMatchInlineSnapshot(`
417+
"
418+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
419+
420+
const foo = {}
421+
422+
{
423+
const { [__vite_ssr_import_0__.n]: m } = foo
424+
}
425+
"
426+
`)
403427
})
404428

405429
test('nested object destructure alias', async () => {
@@ -463,6 +487,45 @@ objRest()
463487
`)
464488
})
465489

490+
test('object props and methods', async () => {
491+
expect(
492+
await ssrTransformSimpleCode(
493+
`
494+
import foo from 'foo'
495+
496+
const bar = 'bar'
497+
498+
const obj = {
499+
foo() {},
500+
[foo]() {},
501+
[bar]() {},
502+
foo: () => {},
503+
[foo]: () => {},
504+
[bar]: () => {},
505+
bar(foo) {}
506+
}
507+
`
508+
)
509+
).toMatchInlineSnapshot(`
510+
"
511+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
512+
513+
514+
const bar = 'bar'
515+
516+
const obj = {
517+
foo() {},
518+
[__vite_ssr_import_0__.default]() {},
519+
[bar]() {},
520+
foo: () => {},
521+
[__vite_ssr_import_0__.default]: () => {},
522+
[bar]: () => {},
523+
bar(foo) {}
524+
}
525+
"
526+
`)
527+
})
528+
466529
test('class props', async () => {
467530
expect(
468531
await ssrTransformSimpleCode(

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ async function ssrTransformScript(
230230
// { foo } -> { foo: __import_x__.foo }
231231
// skip for destructuring patterns
232232
if (
233-
!isNodeInPatternWeakMap.get(parent) ||
233+
!isNodeInPattern(parent) ||
234234
isInDestructuringAssignment(parent, parentStack)
235235
) {
236236
s.appendLeft(id.end, `: ${binding}`)
@@ -305,7 +305,10 @@ interface Visitors {
305305
onDynamicImport: (node: Node) => void
306306
}
307307

308-
const isNodeInPatternWeakMap = new WeakMap<_Node, boolean>()
308+
const isNodeInPatternWeakSet = new WeakSet<_Node>()
309+
const setIsNodeInPattern = (node: Property) => isNodeInPatternWeakSet.add(node)
310+
const isNodeInPattern = (node: _Node): node is Property =>
311+
isNodeInPatternWeakSet.has(node)
309312

310313
/**
311314
* Same logic from \@vue/compiler-core & \@vue/compiler-sfc
@@ -425,7 +428,7 @@ function walk(
425428
})
426429
} else if (node.type === 'Property' && parent!.type === 'ObjectPattern') {
427430
// mark property in destructuring pattern
428-
isNodeInPatternWeakMap.set(node, true)
431+
setIsNodeInPattern(node)
429432
} else if (node.type === 'VariableDeclarator') {
430433
const parentFunction = findParentFunction(parentStack)
431434
if (parentFunction) {
@@ -474,8 +477,12 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) {
474477
}
475478

476479
// property key
477-
// this also covers object destructuring pattern
478-
if (isStaticPropertyKey(id, parent) || isNodeInPatternWeakMap.get(parent)) {
480+
if (isStaticPropertyKey(id, parent)) {
481+
return false
482+
}
483+
484+
// object destructuring pattern
485+
if (isNodeInPattern(parent) && parent.value === id) {
479486
return false
480487
}
481488

0 commit comments

Comments
 (0)