diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index e78522ee3af..da223439c57 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1478,6 +1478,22 @@ export default /*#__PURE__*/_defineComponent({ +return { emit } +} + +})" +`; + +exports[`SFC compile + `) + expect(content).toMatch(`emits: ["foo", "bar"]`) + assertCode(content) + }) + test('runtime Enum', () => { const { content, bindings } = compile( ``).content ) }) + + test('mixed usage of tuple / call signature in defineEmits', () => { + expect(() => + compile(``) + ).toThrow( + `defineEmits() type cannot mixed call signature and property syntax.` + ) + }) }) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 9a5e19c84d4..2c2505740a7 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1486,7 +1486,7 @@ export function compileScript( extractRuntimeProps(propsTypeDecl, typeDeclaredProps, declaredTypes) } if (emitsTypeDecl) { - extractRuntimeEmits(emitsTypeDecl, typeDeclaredEmits) + extractRuntimeEmits(emitsTypeDecl, typeDeclaredEmits, error) } // 5. check macro args to make sure it doesn't reference setup scope @@ -2289,15 +2289,32 @@ function inferValueType(node: Node): string | undefined { function extractRuntimeEmits( node: TSFunctionType | TSTypeLiteral | TSInterfaceBody, - emits: Set + emits: Set, + error: (msg: string, node: Node) => never ) { if (node.type === 'TSTypeLiteral' || node.type === 'TSInterfaceBody') { const members = node.type === 'TSTypeLiteral' ? node.members : node.body + let hasCallSignature = false + let hasProperty = false for (let t of members) { if (t.type === 'TSCallSignatureDeclaration') { extractEventNames(t.parameters[0], emits) + hasCallSignature = true + } + if (t.type === 'TSPropertySignature') { + if (t.key.type !== 'Identifier' || t.computed) { + error(`defineEmits() type cannot use computed keys.`, t.key) + } + emits.add(t.key.name) + hasProperty = true } } + if (hasCallSignature && hasProperty) { + error( + `defineEmits() type cannot mixed call signature and property syntax.`, + node + ) + } return } else { extractEventNames(node.parameters[0], emits)