]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): use dynamic defaults merging for methods with computed keys
authorEvan You <yyx990803@gmail.com>
Fri, 31 Mar 2023 01:08:23 +0000 (09:08 +0800)
committerEvan You <yyx990803@gmail.com>
Fri, 31 Mar 2023 01:08:23 +0000 (09:08 +0800)
ref #7113

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

index b1f33f5ff04d58d1cd5ab210055071b93d8b3a56..f59a7407c25a462e1906c2a31325ce53d26d4278 100644 (file)
@@ -2001,6 +2001,30 @@ const props = __props as { foo: () => void, bar: boolean, baz: boolean | (() =>
 
       
       
+return { props }
+}
+
+})"
+`;
+
+exports[`SFC compile <script setup> > with TypeScript > withDefaults w/ dynamic object method 1`] = `
+"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
+
+export default /*#__PURE__*/_defineComponent({
+  props: _mergeDefaults({
+    foo: { type: Function, required: false }
+  }, {
+        ['fo' + 'o']() { return 'foo' }
+      }),
+  setup(__props: any, { expose: __expose }) {
+  __expose();
+
+const props = __props as {
+        foo?: () => 'string'
+      };
+
+      
+      
 return { props }
 }
 
index c95b4f26172f6af09b08f2f711ccfae0a280e181..b3907fa721111377763250554d0b4f2dc21b36b9 100644 (file)
@@ -1430,6 +1430,28 @@ const emit = defineEmits(['a', 'b'])
       )
     })
 
+    test('withDefaults w/ dynamic object method', () => {
+      const { content } = compile(`
+      <script setup lang="ts">
+      const props = withDefaults(defineProps<{
+        foo?: () => 'string'
+      }>(), {
+        ['fo' + 'o']() { return 'foo' }
+      })
+      </script>
+      `)
+      assertCode(content)
+      expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
+      expect(content).toMatch(
+        `
+  _mergeDefaults({
+    foo: { type: Function, required: false }
+  }, {
+        ['fo' + 'o']() { return 'foo' }
+      })`.trim()
+      )
+    })
+
     test('defineEmits w/ type', () => {
       const { content } = compile(`
       <script setup lang="ts">
index 8f78ca90392f3d48424cf1cdda8e513d861dddf6..de9e11d071fdcdcc1138de7af7ea9b772570ff04 100644 (file)
@@ -842,9 +842,8 @@ export function compileScript(
       propsRuntimeDefaults.type === 'ObjectExpression' &&
       propsRuntimeDefaults.properties.every(
         node =>
-          (node.type === 'ObjectProperty' &&
-            (!node.computed || node.key.type.endsWith('Literal'))) ||
-          node.type === 'ObjectMethod'
+          node.type !== 'SpreadElement' &&
+          (!node.computed || node.key.type.endsWith('Literal'))
       )
     )
   }