Skip to content

Commit

Permalink
feat(runtime-core): skip emit warn if has equivalent onXXX prop
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 16, 2020
1 parent bfd6744 commit 0709380
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 18 additions & 2 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('component: emit', () => {
})
render(h(Foo), nodeOps.createElement('div'))
expect(
`Component emitted event "bar" but it is not declared`
`Component emitted event "bar" but it is neither declared`
).toHaveBeenWarned()
})

Expand All @@ -109,10 +109,26 @@ describe('component: emit', () => {
})
render(h(Foo), nodeOps.createElement('div'))
expect(
`Component emitted event "bar" but it is not declared`
`Component emitted event "bar" but it is neither declared`
).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', () => {
const Foo = defineComponent({
emits: {
Expand Down
12 changes: 8 additions & 4 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { ComponentInternalInstance } from './component'
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
import { warn } from './warning'
import { normalizePropsOptions } from './componentProps'

export type ObjectEmitsOptions = Record<
string,
Expand Down Expand Up @@ -48,10 +49,13 @@ export function emit(
const options = normalizeEmitsOptions(instance.type.emits)
if (options) {
if (!(event in options)) {
warn(
`Component emitted event "${event}" but it is not declared in the ` +
`emits option.`
)
const propsOptions = normalizePropsOptions(instance.type.props)[0]
if (!propsOptions || !(`on` + capitalize(event) in propsOptions)) {
warn(
`Component emitted event "${event}" but it is neither declared in ` +
`the emits option nor as an "on${capitalize(event)}" prop.`
)
}
} else {
const validator = options[event]
if (isFunction(validator)) {
Expand Down

0 comments on commit 0709380

Please sign in to comment.