Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): rename objectPattern dynamic key (fixes #9585) #9609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
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
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