Skip to content

Commit

Permalink
lib: check number of arguments in EventTarget's function
Browse files Browse the repository at this point in the history
For now, addEventListener() only checks number of arguments.
removeEventListener() and dispatchEvent() also need checking
number of arguments.

PR-URL: #45668
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
  • Loading branch information
deokjinkim authored and danielleadams committed Jan 3, 2023
1 parent 6297e77 commit 1284789
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/event_target.js
Expand Up @@ -633,6 +633,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 @@ -663,6 +665,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 @@ -676,3 +676,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',
});
}

0 comments on commit 1284789

Please sign in to comment.