From 73731593fd53e8a80b429844bd35bc92bf53f7cf Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 28 May 2020 16:52:52 +0300 Subject: [PATCH 1/5] events: deal with no argument case --- doc/api/errors.md | 5 +++++ lib/internal/errors.js | 2 ++ lib/internal/event_target.js | 4 ++++ test/parallel/test-eventtarget.js | 6 ++++++ 4 files changed, 17 insertions(+) diff --git a/doc/api/errors.md b/doc/api/errors.md index ccbd1bd406c0b4..1614224dd5b042 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -908,6 +908,11 @@ Encoding provided to `TextDecoder()` API was not one of the Thrown when an attempt is made to recursively dispatch an event on `EventTarget`. + +### `ERR_EVENT_NAME_REQUIRED` + +Thrown when an Event is created without passing its constructor a valid `type`. + ### `ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE` diff --git a/lib/internal/errors.js b/lib/internal/errors.js index d3ca1ea1a6bdaf..eb0bd841e968a6 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -831,6 +831,8 @@ E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported', RangeError); E('ERR_EVAL_ESM_CANNOT_PRINT', '--print cannot be used with ESM input', Error); E('ERR_EVENT_RECURSION', 'The event "%s" is already being dispatched', Error); +E('ERR_EVENT_NAME_REQUIRED', 'The event constructor must be called with at' + + 'least 1 argument.', TypeError); E('ERR_FALSY_VALUE_REJECTION', function(reason) { this.reason = reason; return 'Promise was rejected with falsy value'; diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 5ece72ea1d9433..0910888ae499f1 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_EVENT_NAME_REQUIRED } } = require('internal/errors'); @@ -45,6 +46,9 @@ class Event { constructor(type, options) { + if (arguments.length === 0) { + throw new ERR_EVENT_NAME_REQUIRED(); + } 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..050280b34829d5 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -29,6 +29,12 @@ ok(EventTarget); strictEqual(ev.defaultPrevented, false); strictEqual(typeof ev.timeStamp, 'number'); + // no argument behavior + throws(() => { + new Event(); + }, TypeError); + + // compatibility properties with the DOM deepStrictEqual(ev.composedPath(), []); strictEqual(ev.returnValue, true); strictEqual(ev.bubbles, false); From a1a43261afe7a3d000bd421db9825cc62aa96e6d Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 28 May 2020 17:18:02 +0300 Subject: [PATCH 2/5] fixup! arguments --- doc/api/errors.md | 10 +++++----- lib/internal/errors.js | 3 +-- lib/internal/event_target.js | 4 ++-- test/parallel/test-eventtarget.js | 13 ++++++++----- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 1614224dd5b042..dfd8ef45457250 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -908,11 +908,6 @@ Encoding provided to `TextDecoder()` API was not one of the Thrown when an attempt is made to recursively dispatch an event on `EventTarget`. - -### `ERR_EVENT_NAME_REQUIRED` - -Thrown when an Event is created without passing its constructor a valid `type`. - ### `ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE` @@ -1290,6 +1285,11 @@ the worker thread. The provided address family is not understood by the Node.js API. + +### `ERR_INVALID_ARGUMENT_COUNT` + +Thrown when an invalid number of arguments is passed to a function. + ### `ERR_INVALID_ARG_TYPE` diff --git a/lib/internal/errors.js b/lib/internal/errors.js index eb0bd841e968a6..b267234d39e037 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -831,8 +831,6 @@ E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported', RangeError); E('ERR_EVAL_ESM_CANNOT_PRINT', '--print cannot be used with ESM input', Error); E('ERR_EVENT_RECURSION', 'The event "%s" is already being dispatched', Error); -E('ERR_EVENT_NAME_REQUIRED', 'The event constructor must be called with at' + - 'least 1 argument.', TypeError); E('ERR_FALSY_VALUE_REJECTION', function(reason) { this.reason = reason; return 'Promise was rejected with falsy value'; @@ -974,6 +972,7 @@ E('ERR_INVALID_ADDRESS_FAMILY', function(addressType, host, port) { this.port = port; return `Invalid address family: ${addressType} ${host}:${port}`; }, RangeError); +E('ERR_INVALID_ARG_COUNT', 'Required %d arguments but got %d', TypeError); E('ERR_INVALID_ARG_TYPE', (name, expected, actual) => { assert(typeof name === 'string', "'name' must be a string"); diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 0910888ae499f1..f01540231f863e 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -15,7 +15,7 @@ const { ERR_INVALID_ARG_TYPE, ERR_EVENT_RECURSION, ERR_OUT_OF_RANGE, - ERR_EVENT_NAME_REQUIRED + ERR_INVALID_ARG_COUNT } } = require('internal/errors'); @@ -47,7 +47,7 @@ class Event { constructor(type, options) { if (arguments.length === 0) { - throw new ERR_EVENT_NAME_REQUIRED(); + throw new ERR_INVALID_ARG_COUNT(1, arguments.length); } if (options != null && typeof options !== 'object') throw new ERR_INVALID_ARG_TYPE('options', 'object', options); diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 050280b34829d5..2d01c957469913 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -29,11 +29,6 @@ ok(EventTarget); strictEqual(ev.defaultPrevented, false); strictEqual(typeof ev.timeStamp, 'number'); - // no argument behavior - throws(() => { - new Event(); - }, TypeError); - // compatibility properties with the DOM deepStrictEqual(ev.composedPath(), []); strictEqual(ev.returnValue, true); @@ -66,6 +61,14 @@ ok(EventTarget); 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'); From df00bc0c8264f5869e935bdcc2371ddec67f4b81 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 28 May 2020 17:20:09 +0300 Subject: [PATCH 3/5] fixup! lint --- doc/api/errors.md | 2 +- test/parallel/test-eventtarget.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index dfd8ef45457250..e4de61dc54eaac 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1286,7 +1286,7 @@ the worker thread. The provided address family is not understood by the Node.js API. -### `ERR_INVALID_ARGUMENT_COUNT` +### `ERR_INVALID_ARG_COUNT` Thrown when an invalid number of arguments is passed to a function. diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 2d01c957469913..497f03009f5425 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -29,7 +29,7 @@ ok(EventTarget); strictEqual(ev.defaultPrevented, false); strictEqual(typeof ev.timeStamp, 'number'); - // compatibility properties with the DOM + // Compatibility properties with the DOM deepStrictEqual(ev.composedPath(), []); strictEqual(ev.returnValue, true); strictEqual(ev.bubbles, false); @@ -60,14 +60,14 @@ 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'); +{ + // 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 }); From 0b740f6b6d3fec55affa8441028f2246c18729b5 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 28 May 2020 20:22:34 +0300 Subject: [PATCH 4/5] fixup! use ERR_MISSING_ARGS --- doc/api/errors.md | 5 ----- lib/internal/errors.js | 1 - lib/internal/event_target.js | 4 ++-- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index e4de61dc54eaac..ccbd1bd406c0b4 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1285,11 +1285,6 @@ the worker thread. The provided address family is not understood by the Node.js API. - -### `ERR_INVALID_ARG_COUNT` - -Thrown when an invalid number of arguments is passed to a function. - ### `ERR_INVALID_ARG_TYPE` diff --git a/lib/internal/errors.js b/lib/internal/errors.js index b267234d39e037..d3ca1ea1a6bdaf 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -972,7 +972,6 @@ E('ERR_INVALID_ADDRESS_FAMILY', function(addressType, host, port) { this.port = port; return `Invalid address family: ${addressType} ${host}:${port}`; }, RangeError); -E('ERR_INVALID_ARG_COUNT', 'Required %d arguments but got %d', TypeError); E('ERR_INVALID_ARG_TYPE', (name, expected, actual) => { assert(typeof name === 'string', "'name' must be a string"); diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index f01540231f863e..47f7b1d592a1c7 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -15,7 +15,7 @@ const { ERR_INVALID_ARG_TYPE, ERR_EVENT_RECURSION, ERR_OUT_OF_RANGE, - ERR_INVALID_ARG_COUNT + ERR_MISSING_ARGS } } = require('internal/errors'); @@ -47,7 +47,7 @@ class Event { constructor(type, options) { if (arguments.length === 0) { - throw new ERR_INVALID_ARG_COUNT(1, arguments.length); + throw new ERR_MISSING_ARGS('type'); } if (options != null && typeof options !== 'object') throw new ERR_INVALID_ARG_TYPE('options', 'object', options); From 9e77c56370e27affcfa8bd98ba7d0ea8e327fca2 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 4 Jun 2020 12:45:13 +0300 Subject: [PATCH 5/5] fixup! fix test --- test/parallel/test-eventtarget.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 497f03009f5425..cf52030aee5a05 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -428,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]'); }