Skip to content

Commit

Permalink
fix(runtime-core): fix warning for missing event handler (#11489)
Browse files Browse the repository at this point in the history
fix #4803
close #8268
  • Loading branch information
skirtles-code authored Aug 7, 2024
1 parent a917c05 commit e359ff0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
24 changes: 20 additions & 4 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ describe('component: emit', () => {
render() {},
created() {
// @ts-expect-error
this.$emit('bar')
this.$emit('bar-baz')
},
})
render(h(Foo), nodeOps.createElement('div'))
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()
})

Expand All @@ -172,12 +172,12 @@ describe('component: emit', () => {
render() {},
created() {
// @ts-expect-error
this.$emit('bar')
this.$emit('bar-baz')
},
})
render(h(Foo), nodeOps.createElement('div'))
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()
})

Expand All @@ -197,6 +197,22 @@ describe('component: emit', () => {
).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', () => {
const Foo = defineComponent({
emits: {
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ export function emit(
event.startsWith(compatModelEventPrefix))
)
) {
if (!propsOptions || !(toHandlerKey(event) in propsOptions)) {
if (!propsOptions || !(toHandlerKey(camelize(event)) in propsOptions)) {
warn(
`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 {
Expand Down

0 comments on commit e359ff0

Please sign in to comment.