Skip to content

Commit 8ae28ff

Browse files
benjamingrBenjamin Gruenbaum
authored and
Benjamin Gruenbaum
committed
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 2935f72 commit 8ae28ff

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
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

@@ -44,6 +45,9 @@ class Event {
4445

4546

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

test/parallel/test-eventtarget.js

+10-1
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);
@@ -40,7 +41,15 @@ ok(EventTarget);
4041
ev.preventDefault();
4142
strictEqual(ev.defaultPrevented, false);
4243
}
43-
44+
{
45+
// No argument behavior - throw TypeError
46+
throws(() => {
47+
new Event();
48+
}, TypeError);
49+
// Too many arguments passed behavior - ignore additional arguments
50+
const ev = new Event('foo', {}, {});
51+
strictEqual(ev.type, 'foo');
52+
}
4453
{
4554
const ev = new Event('foo', { cancelable: true });
4655
strictEqual(ev.type, 'foo');

0 commit comments

Comments
 (0)