Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-dom): invoker value is actually unknown at runtime #8953

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/runtime-dom/__tests__/patchEvents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,11 @@ describe(`runtime-dom: events patching`, () => {
testElement.dispatchEvent(new CustomEvent('foobar'))
expect(fn2).toHaveBeenCalledTimes(1)
})

it('handles an unknown type', () => {
const el = document.createElement('div')
patchProp(el, 'onClick', null, 'test')
el.dispatchEvent(new Event('click'))
expect('[Vue warn]: Wrong type passed to the event invoker, did you maybe forget @ or : in front of your prop? Received test').toHaveBeenWarned()
})
})
21 changes: 16 additions & 5 deletions packages/runtime-dom/src/modules/events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { hyphenate, isArray } from '@vue/shared'
import { hyphenate, isArray, isString, isFunction } from '@vue/shared'
import {
ErrorCodes,
ComponentInternalInstance,
callWithAsyncErrorHandling
callWithAsyncErrorHandling, warn
Tofandel marked this conversation as resolved.
Show resolved Hide resolved
} from '@vue/runtime-core'

interface Invoker extends EventListener {
Expand Down Expand Up @@ -81,7 +81,7 @@ const getNow = () =>
cachedNow || (p.then(() => (cachedNow = 0)), (cachedNow = Date.now()))

function createInvoker(
initialValue: EventValue,
initialValue: EventValue | unknown,
instance: ComponentInternalInstance | null
) {
const invoker: Invoker = (e: Event & { _vts?: number }) => {
Expand Down Expand Up @@ -109,11 +109,22 @@ function createInvoker(
[e]
)
}
invoker.value = initialValue
invoker.value = sanitizeEventValue(initialValue)
Tofandel marked this conversation as resolved.
Show resolved Hide resolved
invoker.attached = getNow()
return invoker
}

function sanitizeEventValue(value: unknown): EventValue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether sanitizing the listener is the correct approach. I'm not opposed to adding a warning in dev, but the other parts of this will also impact production builds, and that seems unnecessary for handling a bug that should be fixed during dev.

Copy link
Contributor Author

@Tofandel Tofandel Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is if it does happen in prod or otherwise, it's a non recoverable error that kills the Vue component from working further, and it's a runtime error, so not always visible directly, but maybe this could be caught further up as well in the Vue compiler

The problem is first and foremost a type issue, we go from an unknown type but mark it in ts as something assumed so it does need to be sanitized in prod as well to ensure no error does happen

if (isFunction(value) || isArray(value)) {
return value as EventValue
}

if (__DEV__) {
warn('Wrong type passed to the event invoker, did you maybe forget @ or : in front of your prop? Received ' + (isString(value) ? value : typeof value))
Tofandel marked this conversation as resolved.
Show resolved Hide resolved
}
return () => {}
}

function patchStopImmediatePropagation(
e: Event,
value: EventValue
Expand All @@ -124,7 +135,7 @@ function patchStopImmediatePropagation(
originalStop.call(e)
;(e as any)._stopped = true
}
return value.map(fn => (e: Event) => !(e as any)._stopped && fn && fn(e))
return (value as Function[]).map(fn => (e: Event) => !(e as any)._stopped && fn && fn(e))
} else {
return value
}
Expand Down