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(reactivity-transform): correct semicolon insertion with more complex destructuring #7464

Closed
Closed
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
Expand Up @@ -163,7 +163,20 @@ exports[`nested destructure 1`] = `
let __$temp_2 = (useBar()),
d = _toRef(__$temp_2.c, 0),
e = _toRef(__$temp_2.c, 1);
console.log(b.value, d.value, e.value)
let __$temp_3 = (useBaz()),
f = _toRef(__$temp_3, 0),
g = _toRef(__$temp_3[1], 'g'),
h = _toRef(__$temp_3[2], 1);
console.log(b.value, d.value, e.value, f.value, g.value, h.value)
"
`;

exports[`nested destructure w/ mid-path default values 1`] = `
"import { toRef as _toRef } from 'vue'

let __$temp_1 = (useFoo()),
b = _toRef((__$temp_1[0].a || { b: 123 }), 'b');
console.log(b.value)
"
`;

Expand Down
Expand Up @@ -283,12 +283,26 @@ test('nested destructure', () => {
const { code, rootRefs } = transform(`
let [{ a: { b }}] = $(useFoo())
let { c: [d, e] } = $(useBar())
console.log(b, d, e)
let [f, { g }, [ , h ]] = $(useBaz())
console.log(b, d, e, f, g, h)
`)
expect(code).toMatch(`b = _toRef(__$temp_1[0].a, 'b')`)
expect(code).toMatch(`d = _toRef(__$temp_2.c, 0)`)
expect(code).toMatch(`e = _toRef(__$temp_2.c, 1)`)
expect(rootRefs).toStrictEqual(['b', 'd', 'e'])
expect(code).toMatch(`f = _toRef(__$temp_3, 0)`)
expect(code).toMatch(`g = _toRef(__$temp_3[1], 'g')`)
expect(code).toMatch(`h = _toRef(__$temp_3[2], 1)`)
expect(rootRefs).toStrictEqual(['b', 'd', 'e', 'f', 'g', 'h'])
assertCode(code)
})

test('nested destructure w/ mid-path default values', () => {
const { code, rootRefs } = transform(`
let [{ a: { b } = { b: 123 }}] = $(useFoo())
console.log(b)
`)
expect(code).toMatch(`b = _toRef((__$temp_1[0].a || { b: 123 }), 'b')`)
expect(rootRefs).toStrictEqual(['b'])
assertCode(code)
})

Expand Down
65 changes: 41 additions & 24 deletions packages/reactivity-transform/src/reactivityTransform.ts
Expand Up @@ -368,15 +368,16 @@ export function transformAST(
isConst: boolean,
tempVar?: string,
path: PathSegment[] = []
) {
): boolean {
if (!tempVar) {
tempVar = genTempVar()
// const { x } = $(useFoo()) --> const __$temp_1 = useFoo()
s.overwrite(pattern.start! + offset, pattern.end! + offset, tempVar)
}

let nameId: Identifier | undefined
let didBind = false
for (const p of pattern.properties) {
let nameId: Identifier | undefined
let key: Expression | string | undefined
let defaultValue: Expression | undefined
if (p.type === 'ObjectProperty') {
Expand All @@ -400,30 +401,34 @@ export function transformAST(
// { foo: bar }
nameId = p.value
} else if (p.value.type === 'ObjectPattern') {
processRefObjectPattern(p.value, call, isConst, tempVar, [
...path,
key
])
didBind =
processRefObjectPattern(p.value, call, isConst, tempVar, [
...path,
key
]) || didBind
} else if (p.value.type === 'ArrayPattern') {
processRefArrayPattern(p.value, call, isConst, tempVar, [
...path,
key
])
didBind =
processRefArrayPattern(p.value, call, isConst, tempVar, [
...path,
key
]) || didBind
} else if (p.value.type === 'AssignmentPattern') {
if (p.value.left.type === 'Identifier') {
// { foo: bar = 1 }
nameId = p.value.left
defaultValue = p.value.right
} else if (p.value.left.type === 'ObjectPattern') {
processRefObjectPattern(p.value.left, call, isConst, tempVar, [
...path,
[key, p.value.right]
])
didBind =
processRefObjectPattern(p.value.left, call, isConst, tempVar, [
...path,
[key, p.value.right]
]) || didBind
} else if (p.value.left.type === 'ArrayPattern') {
processRefArrayPattern(p.value.left, call, isConst, tempVar, [
...path,
[key, p.value.right]
])
didBind =
processRefArrayPattern(p.value.left, call, isConst, tempVar, [
...path,
[key, p.value.right]
]) || didBind
} else {
// MemberExpression case is not possible here, ignore
}
Expand All @@ -449,11 +454,14 @@ export function transformAST(
'toRef'
)}(${source}, ${keyStr}${defaultStr})`
)

didBind = true
}
}
if (nameId) {
if (didBind && path.length === 0) {
s.appendLeft(call.end! + offset, ';')
}
return didBind
}

function processRefArrayPattern(
Expand All @@ -462,17 +470,19 @@ export function transformAST(
isConst: boolean,
tempVar?: string,
path: PathSegment[] = []
) {
): boolean {
if (!tempVar) {
// const [x] = $(useFoo()) --> const __$temp_1 = useFoo()
tempVar = genTempVar()
s.overwrite(pattern.start! + offset, pattern.end! + offset, tempVar)
}

let nameId: Identifier | undefined
let didBind = false
for (let i = 0; i < pattern.elements.length; i++) {
const e = pattern.elements[i]
if (!e) continue

let nameId: Identifier | undefined
let defaultValue: Expression | undefined
if (e.type === 'Identifier') {
// [a] --> [__a]
Expand All @@ -485,9 +495,13 @@ export function transformAST(
// [...a]
error(`reactivity destructure does not support rest elements.`, e)
} else if (e.type === 'ObjectPattern') {
processRefObjectPattern(e, call, isConst, tempVar, [...path, i])
didBind =
processRefObjectPattern(e, call, isConst, tempVar, [...path, i]) ||
didBind
} else if (e.type === 'ArrayPattern') {
processRefArrayPattern(e, call, isConst, tempVar, [...path, i])
didBind =
processRefArrayPattern(e, call, isConst, tempVar, [...path, i]) ||
didBind
}
if (nameId) {
registerRefBinding(nameId, isConst)
Expand All @@ -500,11 +514,14 @@ export function transformAST(
'toRef'
)}(${source}, ${i}${defaultStr})`
)

didBind = true
}
}
if (nameId) {
if (didBind && path.length === 0) {
s.appendLeft(call.end! + offset, ';')
}
return didBind
}

type PathSegmentAtom = Expression | string | number
Expand Down