Skip to content

Commit

Permalink
fix(ssr): rename objectPattern dynamic key (fixes #9585) (#9609)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Aug 10, 2022
1 parent 9d0b18b commit ee7f78f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
63 changes: 63 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,30 @@ const a = () => {
}
"
`)

// #9585
expect(
await ssrTransformSimpleCode(
`
import { n, m } from 'foo'
const foo = {}
{
const { [n]: m } = foo
}
`
)
).toMatchInlineSnapshot(`
"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
const foo = {}
{
const { [__vite_ssr_import_0__.n]: m } = foo
}
"
`)
})

test('nested object destructure alias', async () => {
Expand Down Expand Up @@ -463,6 +487,45 @@ objRest()
`)
})

test('object props and methods', async () => {
expect(
await ssrTransformSimpleCode(
`
import foo from 'foo'
const bar = 'bar'
const obj = {
foo() {},
[foo]() {},
[bar]() {},
foo: () => {},
[foo]: () => {},
[bar]: () => {},
bar(foo) {}
}
`
)
).toMatchInlineSnapshot(`
"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
const bar = 'bar'
const obj = {
foo() {},
[__vite_ssr_import_0__.default]() {},
[bar]() {},
foo: () => {},
[__vite_ssr_import_0__.default]: () => {},
[bar]: () => {},
bar(foo) {}
}
"
`)
})

test('class props', async () => {
expect(
await ssrTransformSimpleCode(
Expand Down
17 changes: 12 additions & 5 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ async function ssrTransformScript(
// { foo } -> { foo: __import_x__.foo }
// skip for destructuring patterns
if (
!isNodeInPatternWeakMap.get(parent) ||
!isNodeInPattern(parent) ||
isInDestructuringAssignment(parent, parentStack)
) {
s.appendLeft(id.end, `: ${binding}`)
Expand Down Expand Up @@ -305,7 +305,10 @@ interface Visitors {
onDynamicImport: (node: Node) => void
}

const isNodeInPatternWeakMap = new WeakMap<_Node, boolean>()
const isNodeInPatternWeakSet = new WeakSet<_Node>()
const setIsNodeInPattern = (node: Property) => isNodeInPatternWeakSet.add(node)
const isNodeInPattern = (node: _Node): node is Property =>
isNodeInPatternWeakSet.has(node)

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

// property key
// this also covers object destructuring pattern
if (isStaticPropertyKey(id, parent) || isNodeInPatternWeakMap.get(parent)) {
if (isStaticPropertyKey(id, parent)) {
return false
}

// object destructuring pattern
if (isNodeInPattern(parent) && parent.value === id) {
return false
}

Expand Down

0 comments on commit ee7f78f

Please sign in to comment.