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

lib: check number of arguments in EventTarget's function #45668

Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions lib/internal/event_target.js
Expand Up @@ -636,6 +636,8 @@ class EventTarget {
removeEventListener(type, listener, options = kEmptyObject) {
if (!isEventTarget(this))
throw new ERR_INVALID_THIS('EventTarget');
if (arguments.length < 2)
throw new ERR_MISSING_ARGS('type', 'listener');
if (!validateEventListener(listener))
return;

Expand Down Expand Up @@ -666,6 +668,8 @@ class EventTarget {
dispatchEvent(event) {
if (!isEventTarget(this))
throw new ERR_INVALID_THIS('EventTarget');
if (arguments.length < 1)
throw new ERR_MISSING_ARGS('event');

if (!(event instanceof Event))
throw new ERR_INVALID_ARG_TYPE('event', 'Event', event);
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-eventtarget.js
Expand Up @@ -685,3 +685,32 @@ let asyncTest = Promise.resolve();
et.dispatchEvent(new Event('foo'));
});
}

{
const et = new EventTarget();

throws(() => et.addEventListener(), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
});

throws(() => et.addEventListener('foo'), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
});

throws(() => et.removeEventListener(), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
});

throws(() => et.removeEventListener('foo'), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
});

throws(() => et.dispatchEvent(), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
});
}