]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): allow type annotation for defineEmits variable (#5394)
authorygj6 <7699524+ygj6@users.noreply.github.com>
Wed, 26 Oct 2022 09:12:55 +0000 (17:12 +0800)
committerGitHub <noreply@github.com>
Wed, 26 Oct 2022 09:12:55 +0000 (05:12 -0400)
fix #5393

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

index aa5e2d049265f0b7107391951b92b3e17c0cf1e8..737cc90174e74547964ab586d54b2703645fa909 100644 (file)
@@ -1309,6 +1309,23 @@ export default /*#__PURE__*/_defineComponent({
 
       
       
+return { emit }
+}
+
+})"
+`;
+
+exports[`SFC compile <script setup> with TypeScript defineEmits w/ type (interface ts type) 1`] = `
+"import { defineComponent as _defineComponent } from 'vue'
+interface Emits { (e: 'foo'): void }
+      
+export default /*#__PURE__*/_defineComponent({
+  emits: ['foo'],
+  setup(__props, { expose, emit }) {
+  expose();
+
+      
+      
 return { emit }
 }
 
index 5b394d6c5d43f2e6a88408ceddce735e72159313..09ccb327afef68ff1c09646afb5aa4a295c2882d 100644 (file)
@@ -1133,6 +1133,19 @@ const emit = defineEmits(['a', 'b'])
       expect(content).toMatch(`emits: ["foo", "bar"]`)
     })
 
+    // #5393
+    test('defineEmits w/ type (interface ts type)', () => {
+      const { content } = compile(`
+      <script setup lang="ts">
+      interface Emits { (e: 'foo'): void }
+      const emit: Emits = defineEmits(['foo'])
+      </script>
+      `)
+      assertCode(content)
+      expect(content).toMatch(`setup(__props, { expose, emit }) {`)
+      expect(content).toMatch(`emits: ['foo']`)
+    })
+
     test('runtime Enum', () => {
       const { content, bindings } = compile(
         `<script setup lang="ts">
index 5f90623b8bb7ce4f1ddcb48bac50ccc81fa822e9..d6f1278498608b9ec17f373f9b495511a1d5697e 100644 (file)
@@ -553,7 +553,7 @@ export function compileScript(
     }
 
     if (declId) {
-      emitIdentifier = scriptSetup!.content.slice(declId.start!, declId.end!)
+      emitIdentifier = (declId.type === 'Identifier') ? declId.name : scriptSetup!.content.slice(declId.start!, declId.end!)
     }
 
     return true