From bc2e821ccc65f32b56b0ec4a0e195433c84b09aa Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 28 May 2020 16:52:52 +0300 Subject: [PATCH] events: deal with no argument case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/33611 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/internal/event_target.js | 4 ++++ test/parallel/test-eventtarget.js | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 5ece72ea1d9433..47f7b1d592a1c7 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -15,6 +15,7 @@ const { ERR_INVALID_ARG_TYPE, ERR_EVENT_RECURSION, ERR_OUT_OF_RANGE, + ERR_MISSING_ARGS } } = require('internal/errors'); @@ -45,6 +46,9 @@ class Event { constructor(type, options) { + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('type'); + } if (options != null && typeof options !== 'object') throw new ERR_INVALID_ARG_TYPE('options', 'object', options); const { cancelable, bubbles, composed } = { ...options }; diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index c0e760d13b1369..cf52030aee5a05 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -29,6 +29,7 @@ ok(EventTarget); strictEqual(ev.defaultPrevented, false); strictEqual(typeof ev.timeStamp, 'number'); + // Compatibility properties with the DOM deepStrictEqual(ev.composedPath(), []); strictEqual(ev.returnValue, true); strictEqual(ev.bubbles, false); @@ -59,7 +60,15 @@ ok(EventTarget); ev.cancelBubble = 'some-truthy-value'; strictEqual(ev.cancelBubble, true); } - +{ + // No argument behavior - throw TypeError + throws(() => { + new Event(); + }, TypeError); + // Too many arguments passed behavior - ignore additional arguments + const ev = new Event('foo', {}, {}); + strictEqual(ev.type, 'foo'); +} { const ev = new Event('foo', { cancelable: true }); strictEqual(ev.type, 'foo'); @@ -419,6 +428,6 @@ ok(EventTarget); { const target = new EventTarget(); strictEqual(target.toString(), '[object EventTarget]'); - const event = new Event(); + const event = new Event(''); strictEqual(event.toString(), '[object Event]'); }