GeezerVison is showing github.com — lib: make event static properties non writable and configurable · nodejs/node@3844af2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/event_target.js +17-5Lines changed: 17 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ArrayFrom, | | 5 | + ArrayPrototypeReduce, | 5 | 6 | Boolean, | 6 | 7 | Error, | 7 | 8 | FunctionPrototypeCall, | @@ -318,11 +319,6 @@ class Event { | 318 | 319 | throw new ERR_INVALID_THIS('Event'); | 319 | 320 | this.#propagationStopped = true; | 320 | 321 | } | 321 | | - | 322 | | - static NONE = 0; | 323 | | - static CAPTURING_PHASE = 1; | 324 | | - static AT_TARGET = 2; | 325 | | - static BUBBLING_PHASE = 3; | 326 | 322 | } | 327 | 323 |
| 328 | 324 | ObjectDefineProperties( | @@ -358,6 +354,22 @@ ObjectDefineProperties( | 358 | 354 | isTrusted: isTrustedDescriptor, | 359 | 355 | }); | 360 | 356 |
| | 357 | +const staticProps = ['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE']; | | 358 | + | | 359 | +ObjectDefineProperties( | | 360 | + Event, | | 361 | + ArrayPrototypeReduce(staticProps, (result, staticProp, index = 0) => { | | 362 | + result[staticProp] = { | | 363 | + __proto__: null, | | 364 | + writable: false, | | 365 | + configurable: false, | | 366 | + enumerable: true, | | 367 | + value: index, | | 368 | + }; | | 369 | + return result; | | 370 | + }, {}), | | 371 | +); | | 372 | + | 361 | 373 | function isCustomEvent(value) { | 362 | 374 | return isEvent(value) && (value?.[kDetail] !== undefined); | 363 | 375 | } | |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +require('../common'); | | 4 | +const assert = require('assert'); | | 5 | + | | 6 | +const eventPhases = { | | 7 | + 'NONE': 0, | | 8 | + 'CAPTURING_PHASE': 1, | | 9 | + 'AT_TARGET': 2, | | 10 | + 'BUBBLING_PHASE': 3 | | 11 | +}; | | 12 | + | | 13 | +for (const [prop, value] of Object.entries(eventPhases)) { | | 14 | + // Check if the value of the property matches the expected value | | 15 | + assert.strictEqual(Event[prop], value, `Expected Event.${prop} to be ${value}, but got ${Event[prop]}`); | | 16 | + | | 17 | + const desc = Object.getOwnPropertyDescriptor(Event, prop); | | 18 | + assert.strictEqual(desc.writable, false, `${prop} should not be writable`); | | 19 | + assert.strictEqual(desc.configurable, false, `${prop} should not be configurable`); | | 20 | + assert.strictEqual(desc.enumerable, true, `${prop} should be enumerable`); | | 21 | +} |
|