Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: make event static properties non writable and configurable #50425

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 17 additions & 5 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayFrom,
ArrayPrototypeReduce,
Boolean,
Error,
FunctionPrototypeCall,
Expand Down Expand Up @@ -314,11 +315,6 @@ class Event {
throw new ERR_INVALID_THIS('Event');
this.#propagationStopped = true;
}

static NONE = 0;
static CAPTURING_PHASE = 1;
static AT_TARGET = 2;
static BUBBLING_PHASE = 3;
}

ObjectDefineProperties(
Expand Down Expand Up @@ -354,6 +350,22 @@ ObjectDefineProperties(
isTrusted: isTrustedDescriptor,
});

const staticProps = ['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE'];

ObjectDefineProperties(
Event,
ArrayPrototypeReduce(staticProps, (result, staticProp, index = 0) => {
result[staticProp] = {
__proto__: null,
writable: false,
configurable: false,
enumerable: true,
value: index,
};
return result;
}, {}),
);

function isCustomEvent(value) {
return isEvent(value) && (value?.[kDetail] !== undefined);
}
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-event-target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

require('../common');
const assert = require('assert');

const eventPhases = {
'NONE': 0,
'CAPTURING_PHASE': 1,
'AT_TARGET': 2,
'BUBBLING_PHASE': 3
};

for (const [prop, value] of Object.entries(eventPhases)) {
// Check if the value of the property matches the expected value
assert.strictEqual(Event[prop], value, `Expected Event.${prop} to be ${value}, but got ${Event[prop]}`);

const desc = Object.getOwnPropertyDescriptor(Event, prop);
assert.strictEqual(desc.writable, false, `${prop} should not be writable`);
assert.strictEqual(desc.configurable, false, `${prop} should not be configurable`);
assert.strictEqual(desc.enumerable, true, `${prop} should be enumerable`);
}
BenzeneAlcohol marked this conversation as resolved.
Show resolved Hide resolved