]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): handle method shorthand syntax in withDefaults (#6972)
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Wed, 9 Nov 2022 03:12:54 +0000 (11:12 +0800)
committerGitHub <noreply@github.com>
Wed, 9 Nov 2022 03:12:54 +0000 (22:12 -0500)
fix #6971

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 55659cb93d3bb270a7952d970e0714cb5c9861a4..d553fb04b3b0fdbda149995b29b611de70d5c4f0 100644 (file)
@@ -1744,12 +1744,14 @@ export default /*#__PURE__*/_defineComponent({
     bar: { type: Number, required: false },
     baz: { type: Boolean, required: true },
     qux: { type: Function, required: false, default() { return 1 } },
-    quux: { type: Function, required: false, default() { } }
+    quux: { type: Function, required: false, default() { } },
+    quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } },
+    fred: { type: String, required: false, get default() { return 'fred' } }
   },
   setup(__props: any, { expose }) {
   expose();
 
-const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number, quux(): void };
+const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number, quux(): void, quuxx: Promise<string>, fred: string };
 
       
       
index c3ba6067648fac86100b2e6103289318ac808a1a..bf562defd241c536d53704073fef6c054852df3d 100644 (file)
@@ -1041,10 +1041,14 @@ const emit = defineEmits(['a', 'b'])
         baz: boolean;
         qux?(): number;
         quux?(): void
+        quuxx?: Promise<string>;
+        fred?: string
       }>(), {
         foo: 'hi',
         qux() { return 1 },
-        ['quux']() { }
+        ['quux']() { },
+        async quuxx() { return await Promise.resolve('hi') },
+        get fred() { return 'fred' }
       })
       </script>
       `)
@@ -1061,7 +1065,13 @@ const emit = defineEmits(['a', 'b'])
         `quux: { type: Function, required: false, default() { } }`
       )
       expect(content).toMatch(
-        `{ foo: string, bar?: number, baz: boolean, qux(): number, quux(): void }`
+        `quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`
+      )
+      expect(content).toMatch(
+        `fred: { type: String, required: false, get default() { return 'fred' } }`
+      )
+      expect(content).toMatch(
+        `{ foo: string, bar?: number, baz: boolean, qux(): number, quux(): void, quuxx: Promise<string>, fred: string }`
       )
       expect(content).toMatch(`const props = __props`)
       expect(bindings).toStrictEqual({
@@ -1070,6 +1080,8 @@ const emit = defineEmits(['a', 'b'])
         baz: BindingTypes.PROPS,
         qux: BindingTypes.PROPS,
         quux: BindingTypes.PROPS,
+        quuxx: BindingTypes.PROPS,
+        fred: BindingTypes.PROPS,
         props: BindingTypes.SETUP_CONST
       })
     })
index d55cb795c771140ebc448b61497e48f1acd2ef8c..e944b7647256da8462ab0d35659075992e726ac7 100644 (file)
@@ -805,7 +805,9 @@ export function compileScript(
                 prop.value.end!
               )}`
             } else {
-              defaultString = `default() ${scriptSetupSource.slice(
+              defaultString = `${prop.async ? 'async ' : ''}${
+                prop.kind !== 'method' ? `${prop.kind} ` : ''
+              }default() ${scriptSetupSource.slice(
                 prop.body.start!,
                 prop.body.end!
               )}`