Skip to content

Commit

Permalink
fix: scoped variable with array destructuring & nested arguments (#5730)
Browse files Browse the repository at this point in the history
Co-authored-by: Svetlana Smorodinova <svetlanasmorodinova@Svetlanas-MacBook-Air.local>
  • Loading branch information
jbmolle and Svetlana Smorodinova committed Nov 19, 2021
1 parent d93a9fa commit b86a2f3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
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
26 changes: 15 additions & 11 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,20 @@ function walk(
node.params.forEach((p) =>
(eswalk as any)(p.type === 'AssignmentPattern' ? p.left : p, {
enter(child: Node, parent: Node) {
if (child.type !== 'Identifier') return
// do not record as scope variable if is a destructuring keyword
if (isStaticPropertyKey(child, parent)) return
// do not record if this is a default value
// assignment of a destructuring variable
if (
child.type === 'Identifier' &&
// do not record as scope variable if is a destructuring key
!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?.type === 'AssignmentPattern' &&
parent?.right === child) ||
(parent?.type === 'TemplateLiteral' &&
parent?.expressions.includes(child))
) {
setScope(node, child.name)
return
}
setScope(node, child.name)
}
})
)
Expand All @@ -346,6 +346,10 @@ function walk(
setScope(parentFunction, (property.value as Identifier).name)
}
})
} 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

0 comments on commit b86a2f3

Please sign in to comment.