]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): accept `StringLiteral` node in `defineEmit` tuple syntax (#8041)
author-isum <17521736+equt@users.noreply.github.com>
Thu, 6 Apr 2023 09:13:34 +0000 (17:13 +0800)
committerGitHub <noreply@github.com>
Thu, 6 Apr 2023 09:13:34 +0000 (17:13 +0800)
close #8040

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

index 818ea02e30307474a981627fedaf95b454450f41..23a3741afb2810e5cc04e8f290fa3472c8721b6c 100644 (file)
@@ -1457,6 +1457,22 @@ export default /*#__PURE__*/_defineComponent({
 
       
       
+return { emit }
+}
+
+})"
+`;
+
+exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (property syntax string literal) 1`] = `
+"import { defineComponent as _defineComponent } from 'vue'
+
+export default /*#__PURE__*/_defineComponent({
+  emits: [\\"foo:bar\\"],
+  setup(__props, { expose: __expose, emit }) {
+  __expose();
+
+      
+      
 return { emit }
 }
 
index 7187c5d319115796a358c9ad35a1383f42d6a5cb..1d8d20ec1a08e7b512f2c4607abec59b9397aee7 100644 (file)
@@ -1629,6 +1629,17 @@ const emit = defineEmits(['a', 'b'])
       assertCode(content)
     })
 
+    // #8040
+    test('defineEmits w/ type (property syntax string literal)', () => {
+      const { content } = compile(`
+      <script setup lang="ts">
+      const emit = defineEmits<{ 'foo:bar': [] }>()
+      </script>
+      `)
+      expect(content).toMatch(`emits: ["foo:bar"]`)
+      assertCode(content)
+    })
+
     describe('defineSlots()', () => {
       test('basic usage', () => {
         const { content } = compile(`
index 8d22d7e1348e6727eb08300bd186f301ce3cef89..31114c52d8cc3ac3c793346018ebc7f7209e4fcc 100644 (file)
@@ -2316,11 +2316,15 @@ function extractRuntimeEmits(
         hasCallSignature = true
       }
       if (t.type === 'TSPropertySignature') {
-        if (t.key.type !== 'Identifier' || t.computed) {
+        if (t.key.type === 'Identifier' && !t.computed) {
+          emits.add(t.key.name)
+          hasProperty = true
+        } else if (t.key.type === 'StringLiteral' && !t.computed) {
+          emits.add(t.key.value)
+          hasProperty = true
+        } else {
           error(`defineEmits() type cannot use computed keys.`, t.key)
         }
-        emits.add(t.key.name)
-        hasProperty = true
       }
     }
     if (hasCallSignature && hasProperty) {