fix(runtime-core): fix warning for missing event handler (#11489)

fix #4803
close #8268
This commit is contained in:
skirtle 2024-08-07 04:29:41 +01:00 committed by GitHub
parent a917c0539c
commit e359ff0046
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View File

@ -155,12 +155,12 @@ describe('component: emit', () => {
render() {}, render() {},
created() { created() {
// @ts-expect-error // @ts-expect-error
this.$emit('bar') this.$emit('bar-baz')
}, },
}) })
render(h(Foo), nodeOps.createElement('div')) render(h(Foo), nodeOps.createElement('div'))
expect( expect(
`Component emitted event "bar" but it is neither declared`, `Component emitted event "bar-baz" but it is neither declared in the emits option nor as an "onBarBaz" prop`,
).toHaveBeenWarned() ).toHaveBeenWarned()
}) })
@ -172,12 +172,12 @@ describe('component: emit', () => {
render() {}, render() {},
created() { created() {
// @ts-expect-error // @ts-expect-error
this.$emit('bar') this.$emit('bar-baz')
}, },
}) })
render(h(Foo), nodeOps.createElement('div')) render(h(Foo), nodeOps.createElement('div'))
expect( expect(
`Component emitted event "bar" but it is neither declared`, `Component emitted event "bar-baz" but it is neither declared in the emits option nor as an "onBarBaz" prop`,
).toHaveBeenWarned() ).toHaveBeenWarned()
}) })
@ -197,6 +197,22 @@ describe('component: emit', () => {
).not.toHaveBeenWarned() ).not.toHaveBeenWarned()
}) })
test('should not warn if has equivalent onXXX prop with kebab-cased event', () => {
const Foo = defineComponent({
props: ['onFooBar'],
emits: [],
render() {},
created() {
// @ts-expect-error
this.$emit('foo-bar')
},
})
render(h(Foo), nodeOps.createElement('div'))
expect(
`Component emitted event "foo-bar" but it is neither declared`,
).not.toHaveBeenWarned()
})
test('validator warning', () => { test('validator warning', () => {
const Foo = defineComponent({ const Foo = defineComponent({
emits: { emits: {

View File

@ -102,10 +102,10 @@ export function emit(
event.startsWith(compatModelEventPrefix)) event.startsWith(compatModelEventPrefix))
) )
) { ) {
if (!propsOptions || !(toHandlerKey(event) in propsOptions)) { if (!propsOptions || !(toHandlerKey(camelize(event)) in propsOptions)) {
warn( warn(
`Component emitted event "${event}" but it is neither declared in ` + `Component emitted event "${event}" but it is neither declared in ` +
`the emits option nor as an "${toHandlerKey(event)}" prop.`, `the emits option nor as an "${toHandlerKey(camelize(event))}" prop.`,
) )
} }
} else { } else {