fix(reactivity-transform): apply transform for labelled variable declarations

ref https://github.com/vuejs/core/issues/5298#issuecomment-1017970061
This commit is contained in:
Evan You 2022-01-21 07:48:41 +08:00
parent a81a9922bb
commit a05b000948
3 changed files with 15 additions and 4 deletions

View File

@ -10,6 +10,7 @@ exports[`$ unwrapping 1`] = `
})) }))
let c = () => {} let c = () => {}
let d let d
label: var e = (ref())
" "
`; `;
@ -34,12 +35,13 @@ exports[`$ref & $shallowRef declarations 1`] = `
"import { ref as _ref, shallowRef as _shallowRef } from 'vue' "import { ref as _ref, shallowRef as _shallowRef } from 'vue'
let foo = _ref() let foo = _ref()
let a = _ref(1) export let a = _ref(1)
let b = _shallowRef({ let b = _shallowRef({
count: 0 count: 0
}) })
let c = () => {} let c = () => {}
let d let d
label: var e = _ref()
" "
`; `;

View File

@ -25,6 +25,7 @@ test('$ unwrapping', () => {
})) }))
let c = () => {} let c = () => {}
let d let d
label: var e = $(ref())
`) `)
expect(code).not.toMatch(`$(ref())`) expect(code).not.toMatch(`$(ref())`)
expect(code).not.toMatch(`$(ref(1))`) expect(code).not.toMatch(`$(ref(1))`)
@ -39,19 +40,21 @@ test('$ unwrapping', () => {
// normal declarations left untouched // normal declarations left untouched
expect(code).toMatch(`let c = () => {}`) expect(code).toMatch(`let c = () => {}`)
expect(code).toMatch(`let d`) expect(code).toMatch(`let d`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b']) expect(code).toMatch(`label: var e = (ref())`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b', 'e'])
assertCode(code) assertCode(code)
}) })
test('$ref & $shallowRef declarations', () => { test('$ref & $shallowRef declarations', () => {
const { code, rootRefs, importedHelpers } = transform(` const { code, rootRefs, importedHelpers } = transform(`
let foo = $ref() let foo = $ref()
let a = $ref(1) export let a = $ref(1)
let b = $shallowRef({ let b = $shallowRef({
count: 0 count: 0
}) })
let c = () => {} let c = () => {}
let d let d
label: var e = $ref()
`) `)
expect(code).toMatch( expect(code).toMatch(
`import { ref as _ref, shallowRef as _shallowRef } from 'vue'` `import { ref as _ref, shallowRef as _shallowRef } from 'vue'`
@ -69,7 +72,8 @@ test('$ref & $shallowRef declarations', () => {
// normal declarations left untouched // normal declarations left untouched
expect(code).toMatch(`let c = () => {}`) expect(code).toMatch(`let c = () => {}`)
expect(code).toMatch(`let d`) expect(code).toMatch(`let d`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b']) expect(code).toMatch(`label: var e = _ref()`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b', 'e'])
expect(importedHelpers).toStrictEqual(['ref', 'shallowRef']) expect(importedHelpers).toStrictEqual(['ref', 'shallowRef'])
assertCode(code) assertCode(code)
}) })

View File

@ -235,6 +235,11 @@ export function transformAST(
stmt.declaration.type === 'VariableDeclaration' stmt.declaration.type === 'VariableDeclaration'
) { ) {
walkVariableDeclaration(stmt.declaration, isRoot) walkVariableDeclaration(stmt.declaration, isRoot)
} else if (
stmt.type === 'LabeledStatement' &&
stmt.body.type === 'VariableDeclaration'
) {
walkVariableDeclaration(stmt.body, isRoot)
} }
} }
} }