1
- import { hyphenate , isArray } from '@vue/shared'
1
+ import { NOOP , hyphenate , isArray , isFunction , isString } from '@vue/shared'
2
2
import {
3
3
type ComponentInternalInstance ,
4
4
ErrorCodes ,
5
5
callWithAsyncErrorHandling ,
6
+ warn ,
6
7
} from '@vue/runtime-core'
7
8
8
9
interface Invoker extends EventListener {
@@ -36,20 +37,27 @@ export function patchEvent(
36
37
el : Element & { [ veiKey ] ?: Record < string , Invoker | undefined > } ,
37
38
rawName : string ,
38
39
prevValue : EventValue | null ,
39
- nextValue : EventValue | null ,
40
+ nextValue : EventValue | unknown ,
40
41
instance : ComponentInternalInstance | null = null ,
41
42
) {
42
43
// vei = vue event invokers
43
44
const invokers = el [ veiKey ] || ( el [ veiKey ] = { } )
44
45
const existingInvoker = invokers [ rawName ]
45
46
if ( nextValue && existingInvoker ) {
46
47
// patch
47
- existingInvoker . value = nextValue
48
+ existingInvoker . value = __DEV__
49
+ ? sanitizeEventValue ( nextValue , rawName )
50
+ : ( nextValue as EventValue )
48
51
} else {
49
52
const [ name , options ] = parseName ( rawName )
50
53
if ( nextValue ) {
51
54
// add
52
- const invoker = ( invokers [ rawName ] = createInvoker ( nextValue , instance ) )
55
+ const invoker = ( invokers [ rawName ] = createInvoker (
56
+ __DEV__
57
+ ? sanitizeEventValue ( nextValue , rawName )
58
+ : ( nextValue as EventValue ) ,
59
+ instance ,
60
+ ) )
53
61
addEventListener ( el , name , invoker , options )
54
62
} else if ( existingInvoker ) {
55
63
// remove
@@ -116,6 +124,18 @@ function createInvoker(
116
124
return invoker
117
125
}
118
126
127
+ function sanitizeEventValue ( value : unknown , propName : string ) : EventValue {
128
+ if ( isFunction ( value ) || isArray ( value ) ) {
129
+ return value as EventValue
130
+ }
131
+ warn (
132
+ `Wrong type passed to the event invoker, did you maybe forget @ or : ` +
133
+ `in front of your prop?\nReceived ` +
134
+ `${ propName } =${ isString ( value ) ? JSON . stringify ( value ) : `[${ typeof value } ]` } ` ,
135
+ )
136
+ return NOOP
137
+ }
138
+
119
139
function patchStopImmediatePropagation (
120
140
e : Event ,
121
141
value : EventValue ,
@@ -126,7 +146,9 @@ function patchStopImmediatePropagation(
126
146
originalStop . call ( e )
127
147
; ( e as any ) . _stopped = true
128
148
}
129
- return value . map ( fn => ( e : Event ) => ! ( e as any ) . _stopped && fn && fn ( e ) )
149
+ return ( value as Function [ ] ) . map (
150
+ fn => ( e : Event ) => ! ( e as any ) . _stopped && fn && fn ( e ) ,
151
+ )
130
152
} else {
131
153
return value
132
154
}
0 commit comments