Skip to content

Commit e8dc887

Browse files
authored
fix(ext/node): fix inspect of CustomEvent of Node.js (#29668)
1 parent 9494ba3 commit e8dc887

File tree

4 files changed

+353
-17
lines changed

4 files changed

+353
-17
lines changed

ext/node/polyfills/internal/event_target.mjs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// TODO(petamoriken): enable prefer-primordials for node polyfills
55
// deno-lint-ignore-file prefer-primordials
66

7+
import { primordials } from "ext:core/mod.js";
8+
const {
9+
NumberIsInteger,
10+
ObjectAssign,
11+
SymbolFor,
12+
SymbolToStringTag,
13+
} = primordials;
714
import {
815
ERR_EVENT_RECURSION,
916
ERR_INVALID_ARG_TYPE,
@@ -105,7 +112,7 @@ class Event extends WebEvent {
105112
return name;
106113
}
107114

108-
const opts = Object.assign({}, options, {
115+
const opts = ObjectAssign({}, options, {
109116
depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth,
110117
});
111118

@@ -316,6 +323,15 @@ Object.defineProperties(
316323
eventPhase: kEnumerableProperty,
317324
cancelBubble: kEnumerableProperty,
318325
stopPropagation: kEnumerableProperty,
326+
// The parent class `WebEvent` has this field defined, which has greater
327+
// precedence than Symbol.for('nodejs.util.inspect.custom') and shadow it.
328+
// Overwriting it to `undefined` cancels the shadowing.
329+
[SymbolFor("Deno.privateCustomInspect")]: {
330+
value: undefined,
331+
writable: true,
332+
enumerable: false,
333+
configurable: true,
334+
},
319335
},
320336
);
321337

@@ -340,6 +356,7 @@ class CustomEvent extends Event {
340356
}
341357
super(type, options);
342358
this[kDetail] = options?.detail ?? null;
359+
this[SymbolToStringTag] = "CustomEvent";
343360
}
344361

345362
/**
@@ -353,17 +370,6 @@ class CustomEvent extends Event {
353370
}
354371
}
355372

356-
Object.defineProperties(CustomEvent.prototype, {
357-
[Symbol.toStringTag]: {
358-
__proto__: null,
359-
writable: false,
360-
enumerable: false,
361-
configurable: true,
362-
value: "CustomEvent",
363-
},
364-
detail: kEnumerableProperty,
365-
});
366-
367373
class NodeCustomEvent extends Event {
368374
constructor(type, options) {
369375
super(type, options);
@@ -763,9 +769,7 @@ class EventTarget extends WebEventTarget {
763769
}
764770

765771
const opts = ObjectAssign({}, options, {
766-
depth: Number.isInteger(options.depth)
767-
? options.depth - 1
768-
: options.depth,
772+
depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth,
769773
});
770774

771775
return `${name} ${inspect({}, opts)}`;

tests/node_compat/config.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@
536536
"test-event-emitter-symbols.js",
537537
"test-event-target.js",
538538
"test-events-add-abort-listener.mjs",
539+
"test-events-customevent.js",
539540
"test-events-getmaxlisteners.js",
540541
"test-events-list.js",
541542
"test-events-listener-count-with-listener.js",

tests/node_compat/runner/TODO.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- deno-fmt-ignore-file -->
22
# Remaining Node Tests
33

4-
1201 tests out of 3993 have been ported from Node 23.9.0 (30.08% ported, 70.52% remaining).
4+
1202 tests out of 3993 have been ported from Node 23.9.0 (30.10% ported, 70.50% remaining).
55

66
NOTE: This file should not be manually edited. Please edit `tests/node_compat/config.json` and run `deno task setup` in `tests/node_compat/runner` dir instead.
77

@@ -743,7 +743,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
743743
- [parallel/test-esm-loader-hooks-inspect-wait.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-esm-loader-hooks-inspect-wait.js)
744744
- [parallel/test-eval-disallow-code-generation-from-strings.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-eval-disallow-code-generation-from-strings.js)
745745
- [parallel/test-eventemitter-asyncresource.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-eventemitter-asyncresource.js)
746-
- [parallel/test-events-customevent.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-events-customevent.js)
747746
- [parallel/test-events-static-geteventlisteners.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-events-static-geteventlisteners.js)
748747
- [parallel/test-eventsource-disabled.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-eventsource-disabled.js)
749748
- [parallel/test-eventsource.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-eventsource.js)

0 commit comments

Comments
 (0)