feat(runtime-core): skip emit warn if has equivalent onXXX prop

This commit is contained in:
Evan You 2020-04-16 11:27:52 -04:00
parent bfd6744fb1
commit 0709380c5f
2 changed files with 26 additions and 6 deletions

View File

@ -92,7 +92,7 @@ describe('component: emit', () => {
}) })
render(h(Foo), nodeOps.createElement('div')) render(h(Foo), nodeOps.createElement('div'))
expect( expect(
`Component emitted event "bar" but it is not declared` `Component emitted event "bar" but it is neither declared`
).toHaveBeenWarned() ).toHaveBeenWarned()
}) })
@ -109,10 +109,26 @@ describe('component: emit', () => {
}) })
render(h(Foo), nodeOps.createElement('div')) render(h(Foo), nodeOps.createElement('div'))
expect( expect(
`Component emitted event "bar" but it is not declared` `Component emitted event "bar" but it is neither declared`
).toHaveBeenWarned() ).toHaveBeenWarned()
}) })
test('should not warn if has equivalent onXXX prop', () => {
const Foo = defineComponent({
props: ['onFoo'],
emits: [],
render() {},
created() {
// @ts-ignore
this.$emit('foo')
}
})
render(h(Foo), nodeOps.createElement('div'))
expect(
`Component emitted event "bar" but it is neither declared`
).not.toHaveBeenWarned()
})
test('validator warning', () => { test('validator warning', () => {
const Foo = defineComponent({ const Foo = defineComponent({
emits: { emits: {

View File

@ -11,6 +11,7 @@ import {
import { ComponentInternalInstance } from './component' import { ComponentInternalInstance } from './component'
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling' import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
import { warn } from './warning' import { warn } from './warning'
import { normalizePropsOptions } from './componentProps'
export type ObjectEmitsOptions = Record< export type ObjectEmitsOptions = Record<
string, string,
@ -48,10 +49,13 @@ export function emit(
const options = normalizeEmitsOptions(instance.type.emits) const options = normalizeEmitsOptions(instance.type.emits)
if (options) { if (options) {
if (!(event in options)) { if (!(event in options)) {
warn( const propsOptions = normalizePropsOptions(instance.type.props)[0]
`Component emitted event "${event}" but it is not declared in the ` + if (!propsOptions || !(`on` + capitalize(event) in propsOptions)) {
`emits option.` warn(
) `Component emitted event "${event}" but it is neither declared in ` +
`the emits option nor as an "on${capitalize(event)}" prop.`
)
}
} else { } else {
const validator = options[event] const validator = options[event]
if (isFunction(validator)) { if (isFunction(validator)) {