File tree 2 files changed +26
-6
lines changed
2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -92,7 +92,7 @@ describe('component: emit', () => {
92
92
} )
93
93
render ( h ( Foo ) , nodeOps . createElement ( 'div' ) )
94
94
expect (
95
- `Component emitted event "bar" but it is not declared`
95
+ `Component emitted event "bar" but it is neither declared`
96
96
) . toHaveBeenWarned ( )
97
97
} )
98
98
@@ -109,10 +109,26 @@ describe('component: emit', () => {
109
109
} )
110
110
render ( h ( Foo ) , nodeOps . createElement ( 'div' ) )
111
111
expect (
112
- `Component emitted event "bar" but it is not declared`
112
+ `Component emitted event "bar" but it is neither declared`
113
113
) . toHaveBeenWarned ( )
114
114
} )
115
115
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
+
116
132
test ( 'validator warning' , ( ) => {
117
133
const Foo = defineComponent ( {
118
134
emits : {
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
11
11
import { ComponentInternalInstance } from './component'
12
12
import { callWithAsyncErrorHandling , ErrorCodes } from './errorHandling'
13
13
import { warn } from './warning'
14
+ import { normalizePropsOptions } from './componentProps'
14
15
15
16
export type ObjectEmitsOptions = Record <
16
17
string ,
@@ -48,10 +49,13 @@ export function emit(
48
49
const options = normalizeEmitsOptions ( instance . type . emits )
49
50
if ( options ) {
50
51
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
+ }
55
59
} else {
56
60
const validator = options [ event ]
57
61
if ( isFunction ( validator ) ) {
You can’t perform that action at this time.
0 commit comments