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.
  • Loading branch information
deokjinkim committed Nov 29, 2022
1 parent a1c62cf commit 604b3c8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion lib/readline.js
Expand Up @@ -145,7 +145,7 @@ Interface.prototype.question = function question(query, options, cb) {
};
options.signal.addEventListener('abort', onAbort, { once: true });
const cleanup = () => {
options.signal.removeEventListener(onAbort);
options.signal.removeEventListener('abort', onAbort);
};
const originalCb = cb;
cb = typeof cb === 'function' ? (answer) => {
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-eventtarget.js
Expand Up @@ -685,3 +685,37 @@ let asyncTest = Promise.resolve();
et.dispatchEvent(new Event('foo'));
});
}

{
const et = new EventTarget();

throws(() => et.addEventListener(), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
message: 'The "type" and "listener" arguments must be specified'
});

throws(() => et.addEventListener('foo'), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
message: 'The "type" and "listener" arguments must be specified'
});

throws(() => et.removeEventListener(), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
message: 'The "type" and "listener" arguments must be specified'
});

throws(() => et.removeEventListener('foo'), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
message: 'The "type" and "listener" arguments must be specified'
});

throws(() => et.dispatchEvent(), {
code: 'ERR_MISSING_ARGS',
name: 'TypeError',
message: 'The "event" argument must be specified'
});
}

0 comments on commit 604b3c8

Please sign in to comment.