Skip to content

Commit

Permalink
lib: make event static properties non writable and configurable
Browse files Browse the repository at this point in the history
The idl definition for Event makes the properties constant
this means that they shouldn't be configurable and writable.
However, they were, and this commit fixes that.

Fixes: #50417
PR-URL: #50425
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
  • Loading branch information
BenzeneAlcohol authored and UlisesGascon committed Dec 11, 2023
1 parent 9950203 commit 3844af2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
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 @@ -318,11 +319,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 @@ -358,6 +354,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`);
}

0 comments on commit 3844af2

Please sign in to comment.