diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index cb8e5ed2805..67e5856f2a8 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -54,7 +54,7 @@ return { a } }" `; -exports[`SFC compile + `) + assertCode(content) + expect(bindings).toStrictEqual({ + myEmit: BindingTypes.SETUP_CONST + }) + // should remove defineOptions import and call + expect(content).not.toMatch('defineEmits') // should generate correct setup signature expect(content).toMatch(`setup(__props, { emit: myEmit }) {`) // should include context options in default export @@ -145,9 +166,9 @@ const myEmit = defineEmit(['foo', 'bar']) test('should allow defineProps/Emit at the start of imports', () => { assertCode( compile(``).content ) @@ -450,9 +471,9 @@ const myEmit = defineEmit(['foo', 'bar']) test('defineProps/Emit w/ runtime options', () => { const { content } = compile(` `) assertCode(content) @@ -549,11 +570,11 @@ const emit = defineEmit(['a', 'b']) }) }) - test('defineEmit w/ type', () => { + test('defineEmits w/ type', () => { const { content } = compile(` `) assertCode(content) @@ -561,24 +582,24 @@ const emit = defineEmit(['a', 'b']) expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`) }) - test('defineEmit w/ type (union)', () => { + test('defineEmits w/ type (union)', () => { const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)` expect(() => compile(` `) ).toThrow() }) - test('defineEmit w/ type (type literal w/ call signatures)', () => { + test('defineEmits w/ type (type literal w/ call signatures)', () => { const type = `{(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}` const { content } = compile(` `) assertCode(content) @@ -906,8 +927,8 @@ const emit = defineEmit(['a', 'b']) expect(() => { compile(``) }).toThrow(`cannot accept both type and non-type arguments`) }) @@ -927,9 +948,9 @@ const emit = defineEmit(['a', 'b']) expect(() => compile(``) ).toThrow(`cannot reference locally declared variables`) }) @@ -947,9 +968,9 @@ const emit = defineEmit(['a', 'b']) expect(() => compile(``) @@ -959,14 +980,14 @@ const emit = defineEmit(['a', 'b']) test('should allow defineProps/Emit() referencing scope var', () => { assertCode( compile(``).content @@ -976,14 +997,14 @@ const emit = defineEmit(['a', 'b']) test('should allow defineProps/Emit() referencing imported binding', () => { assertCode( compile(``).content diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index c29125e0e1c..5789d67392d 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -36,6 +36,7 @@ import { rewriteDefault } from './rewriteDefault' const DEFINE_PROPS = 'defineProps' const DEFINE_EMIT = 'defineEmit' +const DEFINE_EMITS = 'defineEmits' export interface SFCScriptCompileOptions { /** @@ -286,10 +287,10 @@ export function compileScript( return false } - function processDefineEmit(node: Node): boolean { - if (isCallOf(node, DEFINE_EMIT)) { + function processDefineEmits(node: Node): boolean { + if (isCallOf(node, DEFINE_EMIT) || isCallOf(node, DEFINE_EMITS)) { if (hasDefineEmitCall) { - error(`duplicate ${DEFINE_EMIT}() call`, node) + error(`duplicate ${DEFINE_EMITS}() call`, node) } hasDefineEmitCall = true emitRuntimeDecl = node.arguments[0] @@ -309,7 +310,7 @@ export function compileScript( emitTypeDecl = typeArg } else { error( - `type argument passed to ${DEFINE_EMIT}() must be a function type ` + + `type argument passed to ${DEFINE_EMITS}() must be a function type ` + `or a literal type with call signatures.`, typeArg ) @@ -627,7 +628,9 @@ export function compileScript( const existing = userImports[local] if ( source === 'vue' && - (imported === DEFINE_PROPS || imported === DEFINE_EMIT) + (imported === DEFINE_PROPS || + imported === DEFINE_EMIT || + imported === DEFINE_EMITS) ) { removeSpecifier(i) } else if (existing) { @@ -651,11 +654,11 @@ export function compileScript( } } - // process `defineProps` and `defineEmit` calls + // process `defineProps` and `defineEmit(s)` calls if ( node.type === 'ExpressionStatement' && (processDefineProps(node.expression) || - processDefineEmit(node.expression)) + processDefineEmits(node.expression)) ) { s.remove(node.start! + startOffset, node.end! + startOffset) } @@ -669,14 +672,14 @@ export function compileScript( decl.id.end! ) } - const isDefineEmit = processDefineEmit(decl.init) - if (isDefineEmit) { + const isDefineEmits = processDefineEmits(decl.init) + if (isDefineEmits) { emitIdentifier = scriptSetup.content.slice( decl.id.start!, decl.id.end! ) } - if (isDefineProps || isDefineEmit) + if (isDefineProps || isDefineEmits) if (node.declarations.length === 1) { s.remove(node.start! + startOffset, node.end! + startOffset) } else { @@ -1040,7 +1043,9 @@ function walkDeclaration( for (const { id, init } of node.declarations) { const isDefineCall = !!( isConst && - (isCallOf(init, DEFINE_PROPS) || isCallOf(init, DEFINE_EMIT)) + (isCallOf(init, DEFINE_PROPS) || + isCallOf(init, DEFINE_EMIT) || + isCallOf(init, DEFINE_EMITS)) ) if (id.type === 'Identifier') { let bindingType diff --git a/packages/runtime-core/__tests__/apiSetupHelpers.spec.ts b/packages/runtime-core/__tests__/apiSetupHelpers.spec.ts index 3c0e228bec4..12a8c67bb57 100644 --- a/packages/runtime-core/__tests__/apiSetupHelpers.spec.ts +++ b/packages/runtime-core/__tests__/apiSetupHelpers.spec.ts @@ -5,15 +5,15 @@ import { render, SetupContext } from '@vue/runtime-test' -import { defineEmit, defineProps, useContext } from '../src/apiSetupHelpers' +import { defineEmits, defineProps, useContext } from '../src/apiSetupHelpers' describe('SFC