Skip to content

Commit bc2e821

Browse files
benjamingrcodebytere
authored andcommitted
events: deal with no argument case
PR-URL: #33611 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 858c6b9 commit bc2e821

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/internal/event_target.js

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
ERR_INVALID_ARG_TYPE,
1616
ERR_EVENT_RECURSION,
1717
ERR_OUT_OF_RANGE,
18+
ERR_MISSING_ARGS
1819
}
1920
} = require('internal/errors');
2021

@@ -45,6 +46,9 @@ class Event {
4546

4647

4748
constructor(type, options) {
49+
if (arguments.length === 0) {
50+
throw new ERR_MISSING_ARGS('type');
51+
}
4852
if (options != null && typeof options !== 'object')
4953
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
5054
const { cancelable, bubbles, composed } = { ...options };

test/parallel/test-eventtarget.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ok(EventTarget);
2929
strictEqual(ev.defaultPrevented, false);
3030
strictEqual(typeof ev.timeStamp, 'number');
3131

32+
// Compatibility properties with the DOM
3233
deepStrictEqual(ev.composedPath(), []);
3334
strictEqual(ev.returnValue, true);
3435
strictEqual(ev.bubbles, false);
@@ -59,7 +60,15 @@ ok(EventTarget);
5960
ev.cancelBubble = 'some-truthy-value';
6061
strictEqual(ev.cancelBubble, true);
6162
}
62-
63+
{
64+
// No argument behavior - throw TypeError
65+
throws(() => {
66+
new Event();
67+
}, TypeError);
68+
// Too many arguments passed behavior - ignore additional arguments
69+
const ev = new Event('foo', {}, {});
70+
strictEqual(ev.type, 'foo');
71+
}
6372
{
6473
const ev = new Event('foo', { cancelable: true });
6574
strictEqual(ev.type, 'foo');
@@ -419,6 +428,6 @@ ok(EventTarget);
419428
{
420429
const target = new EventTarget();
421430
strictEqual(target.toString(), '[object EventTarget]');
422-
const event = new Event();
431+
const event = new Event('');
423432
strictEqual(event.toString(), '[object Event]');
424433
}

0 commit comments

Comments
 (0)