Skip to content

Commit 0709380

Browse files
committed
feat(runtime-core): skip emit warn if has equivalent onXXX prop
1 parent bfd6744 commit 0709380

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

packages/runtime-core/__tests__/componentEmits.spec.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('component: emit', () => {
9292
})
9393
render(h(Foo), nodeOps.createElement('div'))
9494
expect(
95-
`Component emitted event "bar" but it is not declared`
95+
`Component emitted event "bar" but it is neither declared`
9696
).toHaveBeenWarned()
9797
})
9898

@@ -109,10 +109,26 @@ describe('component: emit', () => {
109109
})
110110
render(h(Foo), nodeOps.createElement('div'))
111111
expect(
112-
`Component emitted event "bar" but it is not declared`
112+
`Component emitted event "bar" but it is neither declared`
113113
).toHaveBeenWarned()
114114
})
115115

116+
test('should not warn if has equivalent onXXX prop', () => {
117+
const Foo = defineComponent({
118+
props: ['onFoo'],
119+
emits: [],
120+
render() {},
121+
created() {
122+
// @ts-ignore
123+
this.$emit('foo')
124+
}
125+
})
126+
render(h(Foo), nodeOps.createElement('div'))
127+
expect(
128+
`Component emitted event "bar" but it is neither declared`
129+
).not.toHaveBeenWarned()
130+
})
131+
116132
test('validator warning', () => {
117133
const Foo = defineComponent({
118134
emits: {

packages/runtime-core/src/componentEmits.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { ComponentInternalInstance } from './component'
1212
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
1313
import { warn } from './warning'
14+
import { normalizePropsOptions } from './componentProps'
1415

1516
export type ObjectEmitsOptions = Record<
1617
string,
@@ -48,10 +49,13 @@ export function emit(
4849
const options = normalizeEmitsOptions(instance.type.emits)
4950
if (options) {
5051
if (!(event in options)) {
51-
warn(
52-
`Component emitted event "${event}" but it is not declared in the ` +
53-
`emits option.`
54-
)
52+
const propsOptions = normalizePropsOptions(instance.type.props)[0]
53+
if (!propsOptions || !(`on` + capitalize(event) in propsOptions)) {
54+
warn(
55+
`Component emitted event "${event}" but it is neither declared in ` +
56+
`the emits option nor as an "on${capitalize(event)}" prop.`
57+
)
58+
}
5559
} else {
5660
const validator = options[event]
5761
if (isFunction(validator)) {

0 commit comments

Comments
 (0)