]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix: handle case of ref declaration without initial value
authorEvan You <yyx990803@gmail.com>
Fri, 30 Oct 2020 19:29:38 +0000 (15:29 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 30 Oct 2020 19:29:38 +0000 (15:29 -0400)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 69937bc9e7998822799c5af3fa4589f49cee47e6..7865e20d7e1fff0df37db3573bd5e53fbd4006fe 100644 (file)
@@ -292,6 +292,7 @@ exports[`SFC compile <script setup> ref: syntax sugar convert ref declarations 1
 
 export function setup() {
 
+      const foo = ref()
       const a = ref(1)
       const b = ref({
         count: 0
@@ -299,7 +300,7 @@ export function setup() {
       let c = () => {}
       let d
       
-return { a, b, c, d }
+return { foo, a, b, c, d }
 }
 
 export default { setup }"
index 7c17d42d1d2d17a6aa92560406124d09115c4e7d..e1f2c8ae8124660123a7c8e04b1c34ab357cbabe 100644 (file)
@@ -259,6 +259,7 @@ describe('SFC compile <script setup>', () => {
   describe('ref: syntax sugar', () => {
     test('convert ref declarations', () => {
       const { content, bindings } = compile(`<script setup>
+      ref: foo
       ref: a = 1
       ref: b = {
         count: 0
@@ -267,7 +268,10 @@ describe('SFC compile <script setup>', () => {
       let d
       </script>`)
       expect(content).toMatch(`import { ref } from 'vue'`)
+      expect(content).not.toMatch(`ref: foo`)
       expect(content).not.toMatch(`ref: a`)
+      expect(content).not.toMatch(`ref: b`)
+      expect(content).toMatch(`const foo = ref()`)
       expect(content).toMatch(`const a = ref(1)`)
       expect(content).toMatch(`
       const b = ref({
@@ -279,6 +283,7 @@ describe('SFC compile <script setup>', () => {
       expect(content).toMatch(`let d`)
       assertCode(content)
       expect(bindings).toStrictEqual({
+        foo: 'setup',
         a: 'setup',
         b: 'setup',
         c: 'setup',
index 3572cedce4175c0064df345ff4adb34ce1029f23..9b93717cc1e2d634494d0ca3b99ea1fc7c8ccea8 100644 (file)
@@ -155,11 +155,7 @@ export function compileScript(
       helperImports.add('ref')
       const { left, right } = exp
       if (left.type === 'Identifier') {
-        if (left.name[0] === '$') {
-          error(`ref variable identifiers cannot start with $.`, left)
-        }
-        refBindings[left.name] = setupBindings[left.name] = true
-        refIdentifiers.add(left)
+        registerRefBinding(left)
         s.prependRight(right.start! + startOffset, `ref(`)
         s.appendLeft(right.end! + startOffset, ')')
       } else if (left.type === 'ObjectPattern') {
@@ -186,11 +182,22 @@ export function compileScript(
       // possible multiple declarations
       // ref: x = 1, y = 2
       exp.expressions.forEach(e => processRefExpression(e, statement))
+    } else if (exp.type === 'Identifier') {
+      registerRefBinding(exp)
+      s.appendLeft(exp.end! + startOffset, ` = ref()`)
     } else {
       error(`ref: statements can only contain assignment expressions.`, exp)
     }
   }
 
+  function registerRefBinding(id: Identifier) {
+    if (id.name[0] === '$') {
+      error(`ref variable identifiers cannot start with $.`, id)
+    }
+    refBindings[id.name] = setupBindings[id.name] = true
+    refIdentifiers.add(id)
+  }
+
   function processRefObjectPattern(
     pattern: ObjectPattern,
     statement: LabeledStatement
@@ -227,9 +234,7 @@ export function compileScript(
         s.prependRight(nameId.start! + startOffset, `__`)
       }
       if (nameId) {
-        // register binding
-        refBindings[nameId.name] = setupBindings[nameId.name] = true
-        refIdentifiers.add(nameId)
+        registerRefBinding(nameId)
         // append binding declarations after the parent statement
         s.appendLeft(
           statement.end! + startOffset,
@@ -261,10 +266,9 @@ export function compileScript(
         processRefArrayPattern(e, statement)
       }
       if (nameId) {
+        registerRefBinding(nameId)
+        // prefix original
         s.prependRight(nameId.start! + startOffset, `__`)
-        // register binding
-        refBindings[nameId.name] = setupBindings[nameId.name] = true
-        refIdentifiers.add(nameId)
         // append binding declarations after the parent statement
         s.appendLeft(
           statement.end! + startOffset,