Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
events: deal with no argument case
  • Loading branch information
benjamingr committed Jun 4, 2020
commit 73731593fd53e8a80b429844bd35bc92bf53f7cf
5 changes: 5 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

<a id="ERR_EVENT_NAME_REQUIRED"></a>
### `ERR_EVENT_NAME_REQUIRED`

Thrown when an Event is created without passing its constructor a valid `type`.

<a id="ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE"></a>
### `ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE`

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_EVENT_RECURSION,
ERR_OUT_OF_RANGE,
ERR_EVENT_NAME_REQUIRED
}
} = require('internal/errors');

Expand Down Expand Up @@ -45,6 +46,9 @@ class Event {


constructor(type, options) {
if (arguments.length === 0) {
throw new ERR_EVENT_NAME_REQUIRED();
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
}
if (options != null && typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
const { cancelable, bubbles, composed } = { ...options };
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down