From 8ae28ff3ac49cbf83f4fc3445d63b9900f3cdcda 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 | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 763542eb0fc2fa..133edc23b2423a 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'); @@ -44,6 +45,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 99d717abda6e8d..82a89caae1fea4 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); @@ -40,7 +41,15 @@ ok(EventTarget); ev.preventDefault(); strictEqual(ev.defaultPrevented, false); } - +{ + // 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');