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: scoped variable with array destructuring & nested arguments #5730

Merged
merged 4 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 42 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,48 @@ test('do not rewrite when variable is in scope', async () => {
expect(result.deps).toEqual(['vue'])
})

// #5472
test('do not rewrite when variable is in scope with object destructuring', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }"
`)
expect(result.deps).toEqual(['vue'])
})

// #5472
test('do not rewrite when variable is in scope with array destructuring', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }"
`)
expect(result.deps).toEqual(['vue'])
})

// #5727
test('rewrite variable in string interpolation in function nested arguments', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A({foo = \`test\${fn}\`} = {}){ return {}; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A({foo = \`test\${__vite_ssr_import_0__.fn}\`} = {}){ return {}; }"
`)
expect(result.deps).toEqual(['vue'])
})

test('do not rewrite when function declaration is in scope', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }`,
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 @@ -321,10 +321,12 @@ function walk(
!isStaticPropertyKey(child, parent) &&
// do not record if this is a default value
// assignment of a destructuring variable
!(
parent &&
parent.type === 'AssignmentPattern' &&
parent.right === child
(
!parent ||
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
(
!(parent.type === 'AssignmentPattern' && parent.right === child) &&
!(parent.type === 'TemplateLiteral' && parent.expressions.includes(child))
)
)
) {
setScope(node, child.name)
Expand All @@ -346,7 +348,12 @@ function walk(
setScope(parentFunction, (property.value as Identifier).name)
}
})
} else {
} else if (node.id.type === 'ArrayPattern') {
node.id.elements.forEach((element) => {
setScope(parentFunction, (element as Identifier).name)
})
}
else {
setScope(parentFunction, (node.id as Identifier).name)
}
}
Expand Down