From e8745083b9c4b9d3b6e8791fa0008c8819c70452 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Fri, 9 Dec 2022 15:07:20 +0900 Subject: [PATCH] lib: check number of arguments in `EventTarget`'s function For now, addEventListener() only checks number of arguments. removeEventListener() and dispatchEvent() also need checking number of arguments. PR-URL: https://github.com/nodejs/node/pull/45668 Reviewed-By: Antoine du Hamel Reviewed-By: Daeyeon Jeong --- lib/internal/event_target.js | 4 ++++ test/parallel/test-eventtarget.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index c0347d79d6cd3e..863c1c6ea8bd01 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -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; @@ -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); diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index b7898391a793b5..f6208a3b8b8844 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -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', + }); +}