Parcourir la source

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

ref https://github.com/vuejs/core/issues/5298#issuecomment-1017970061
Evan You il y a 4 ans
Parent
commit
a05b000948

+ 3 - 1
packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap

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

+ 7 - 3
packages/reactivity-transform/__tests__/reactivityTransform.spec.ts

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

+ 5 - 0
packages/reactivity-transform/src/reactivityTransform.ts

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