Skip to content

Commit 23a196d

Browse files
authored
Improve DOM and Web API symbol and iterator typing (#60117)
Summary: ## Context Several DOM implementation details use symbol keys that were excluded from type checking, masking incorrect slot declarations and requiring suppressions. Symbol-key support allows those contracts to be checked. The collection APIs also return iterable generator objects, which should be reflected in their types. ## Changes - Preserve unique-symbol identities and type core `Event` state, abort API state, DOM node metadata, and the existing parent/trusted-dispatch symbol methods. - Correct composed-path and event-phase types, and type the internal timestamp read without changing public `EventInit`. The internal timestamp setter accepts any init object and defines the same enumerable, writable data property, so existing callers keep their option types. - Keep the private dispatch flag, parent cache, and handler-attribute storage boundaries unchanged rather than exposing them for typing. Preserve the emitted `Event` and `EventTarget` runtime code. - Align DOM collection iterator returns with `IteratorObject` and regenerate the public API snapshot. - Extend existing Fantom suites with public collection-iterator and event-reuse assertions. Add no new test suites, private-state tests, or Babel transform changes. - Remove 77 existing suppression comments (19 `$FlowFixMe`, 58 `$FlowExpectedError`) without introducing new ones. Changelog: [General][Fixed] - Correct DOM collection iterable iterator types Reviewed By: christophpurrer Differential Revision: D132890886 Pull Request resolved: #60117
1 parent 2cf3a3c commit 23a196d

16 files changed

Lines changed: 251 additions & 252 deletions

File tree

‎packages/react-native/ReactNativeApi.d.ts‎

Lines changed: 127 additions & 127 deletions
Large diffs are not rendered by default.

‎packages/react-native/src/private/webapis/dom/abort-api/AbortController.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import type {AbortSignal} from './AbortSignal';
3333

34-
const SIGNAL_KEY: symbol = Symbol('aborted');
34+
const SIGNAL_KEY = Symbol('aborted');
3535

3636
/**
3737
* The AbortController.
@@ -41,12 +41,9 @@ export class AbortController {
4141
/**
4242
* Initialize this controller.
4343
*/
44-
// $FlowExpectedError[unsupported-syntax]
45-
// $FlowFixMe[illegal-key]
4644
[SIGNAL_KEY]: AbortSignal;
4745

4846
constructor() {
49-
// $FlowExpectedError[prop-missing]
5047
this[SIGNAL_KEY] = require('./AbortSignal').createAbortSignal();
5148
}
5249

@@ -69,7 +66,6 @@ export class AbortController {
6966
* Get the associated signal of a given controller.
7067
*/
7168
function getSignal(controller: AbortController): AbortSignal {
72-
// $FlowExpectedError[prop-missing]
7369
const signal = controller[SIGNAL_KEY];
7470
if (signal == null) {
7571
throw new TypeError(

‎packages/react-native/src/private/webapis/dom/abort-api/AbortSignal.js‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import EventTarget from '../events/EventTarget';
4141
import {dispatchTrustedEvent} from '../events/internals/EventTargetInternals';
4242
import {AbortController} from './AbortController';
4343

44-
const ABORTED_KEY: symbol = Symbol('aborted');
45-
const REASON_KEY: symbol = Symbol('reason');
44+
const ABORTED_KEY = Symbol('aborted');
45+
const REASON_KEY = Symbol('reason');
4646

4747
/**
4848
* The signal class.
@@ -131,18 +131,13 @@ export class AbortSignal extends EventTarget {
131131
return controller.signal;
132132
}
133133

134-
// $FlowExpectedError[unsupported-syntax]
135-
// $FlowFixMe[illegal-key]
136134
[ABORTED_KEY]: boolean = false;
137-
// $FlowExpectedError[unsupported-syntax]
138-
// $FlowFixMe[illegal-key]
139135
[REASON_KEY]: unknown;
140136

141137
/**
142138
* Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
143139
*/
144140
get aborted(): boolean {
145-
// $FlowExpectedError[prop-missing]
146141
const aborted = this[ABORTED_KEY];
147142
if (typeof aborted !== 'boolean') {
148143
throw new TypeError(
@@ -156,7 +151,6 @@ export class AbortSignal extends EventTarget {
156151
}
157152

158153
get reason(): unknown {
159-
// $FlowExpectedError[prop-missing]
160154
return this[REASON_KEY];
161155
}
162156

@@ -192,14 +186,10 @@ export function abortSignal(
192186
'AbortError',
193187
),
194188
): void {
195-
// $FlowExpectedError[invalid-compare]
196-
// $FlowExpectedError[prop-missing]
197189
if (signal[ABORTED_KEY] !== false) {
198190
return;
199191
}
200-
// $FlowExpectedError[prop-missing]
201192
signal[ABORTED_KEY] = true;
202-
// $FlowExpectedError[prop-missing]
203193
signal[REASON_KEY] = reason;
204194
dispatchTrustedEvent(signal, new Event('abort'));
205195
}

‎packages/react-native/src/private/webapis/dom/events/Event.js‎

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
// flowlint unsafe-getters-setters:off
1717

1818
import type EventTarget from './EventTarget';
19+
import type {EventInitWithTimestamp} from './internals/EventInternals';
1920

2021
import {setPlatformObject} from '../../webidl/PlatformObjects';
2122
import {
23+
BUBBLED_PROP_NAME_KEY,
24+
CAPTURED_PROP_NAME_KEY,
2225
COMPOSED_PATH_KEY,
2326
CURRENT_TARGET_KEY,
2427
EVENT_INIT_TIMESTAMP_KEY,
@@ -70,37 +73,16 @@ export default class Event {
7073
_defaultPrevented: boolean = false;
7174
_timeStamp: number;
7275

73-
// $FlowExpectedError[unsupported-syntax]
74-
// $FlowFixMe[illegal-key]
75-
[COMPOSED_PATH_KEY]: boolean = [];
76-
77-
// $FlowExpectedError[unsupported-syntax]
78-
// $FlowFixMe[illegal-key]
76+
[COMPOSED_PATH_KEY]: ReadonlyArray<EventTarget> = [];
7977
[CURRENT_TARGET_KEY]: EventTarget | null = null;
80-
81-
// $FlowExpectedError[unsupported-syntax]
82-
// $FlowFixMe[illegal-key]
83-
[EVENT_PHASE_KEY]: boolean = Event.NONE;
84-
85-
// $FlowExpectedError[unsupported-syntax]
86-
// $FlowFixMe[illegal-key]
78+
[EVENT_PHASE_KEY]: EventPhase = Event.NONE;
8779
[IN_PASSIVE_LISTENER_FLAG_KEY]: boolean = false;
88-
89-
// $FlowExpectedError[unsupported-syntax]
90-
// $FlowFixMe[illegal-key]
9180
[IS_TRUSTED_KEY]: boolean = false;
92-
93-
// $FlowExpectedError[unsupported-syntax]
94-
// $FlowFixMe[illegal-key]
9581
[STOP_IMMEDIATE_PROPAGATION_FLAG_KEY]: boolean = false;
96-
97-
// $FlowExpectedError[unsupported-syntax]
98-
// $FlowFixMe[illegal-key]
9982
[STOP_PROPAGATION_FLAG_KEY]: boolean = false;
100-
101-
// $FlowExpectedError[unsupported-syntax]
102-
// $FlowFixMe[illegal-key]
10383
[TARGET_KEY]: EventTarget | null = null;
84+
[BUBBLED_PROP_NAME_KEY]: string | null | void;
85+
[CAPTURED_PROP_NAME_KEY]: string | null | void;
10486

10587
constructor(type: string, options?: ?EventInit) {
10688
if (arguments.length < 1) {
@@ -139,8 +121,9 @@ export default class Event {
139121
// For internal construction of events using a custom timestamp (instead of
140122
// event object creation), for use cases like dispatching events from the
141123
// host platform using the original timestamps.
142-
// $FlowExpectedError[prop-missing]
143-
const initTimestamp: number | void = options?.[EVENT_INIT_TIMESTAMP_KEY];
124+
const initTimestamp: number | void = (
125+
options as EventInitWithTimestamp | null | void
126+
)?.[EVENT_INIT_TIMESTAMP_KEY];
144127
this._timeStamp =
145128
initTimestamp !== undefined ? initTimestamp : performance.now();
146129
}

‎packages/react-native/src/private/webapis/dom/events/EventTarget.js‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type EventListenerRegistration = {
6464
type ListenersMap = Map<string, Map<EventListener, EventListenerRegistration>>;
6565

6666
export default class EventTarget {
67+
[CAPTURING_LISTENERS_KEY]: ListenersMap | void;
68+
[BUBBLING_LISTENERS_KEY]: ListenersMap | void;
69+
6770
addEventListener(
6871
type: string,
6972
callback: EventListener | null,
@@ -239,8 +242,6 @@ export default class EventTarget {
239242
* Should implement the "get the parent" algorithm
240243
* (see https://dom.spec.whatwg.org/#get-the-parent).
241244
*/
242-
// $FlowExpectedError[unsupported-syntax]
243-
// $FlowFixMe[illegal-key]
244245
[EVENT_TARGET_GET_THE_PARENT_KEY](): EventTarget | null {
245246
return null;
246247
}
@@ -250,8 +251,6 @@ export default class EventTarget {
250251
* `dispatchEvent` return contract: returns `false` if the event was
251252
* canceled (i.e. `event.defaultPrevented`), otherwise `true`.
252253
*/
253-
// $FlowExpectedError[unsupported-syntax]
254-
// $FlowFixMe[illegal-key]
255254
[INTERNAL_DISPATCH_METHOD_KEY](
256255
event: Event,
257256
rethrowListenerErrors?: boolean,
@@ -504,10 +503,8 @@ function getListenersForPhase(
504503
isCapture: boolean,
505504
): ?ListenersMap {
506505
return isCapture
507-
? // $FlowExpectedError[prop-missing]
508-
eventTarget[CAPTURING_LISTENERS_KEY]
509-
: // $FlowExpectedError[prop-missing]
510-
eventTarget[BUBBLING_LISTENERS_KEY];
506+
? eventTarget[CAPTURING_LISTENERS_KEY]
507+
: eventTarget[BUBBLING_LISTENERS_KEY];
511508
}
512509

513510
function setListenersMap(
@@ -516,17 +513,15 @@ function setListenersMap(
516513
listenersMap: ListenersMap,
517514
): void {
518515
if (isCapture) {
519-
// $FlowExpectedError[prop-missing]
520516
eventTarget[CAPTURING_LISTENERS_KEY] = listenersMap;
521517
} else {
522-
// $FlowExpectedError[prop-missing]
523518
eventTarget[BUBBLING_LISTENERS_KEY] = listenersMap;
524519
}
525520
}
526521

527522
const EVENT_DISPATCH_FLAG = Symbol('Event.dispatch');
528523

529-
function getEventDispatchFlag(event: Event): boolean {
524+
function getEventDispatchFlag(event: Event): boolean | void {
530525
// $FlowExpectedError[prop-missing]
531526
return event[EVENT_DISPATCH_FLAG];
532527
}

‎packages/react-native/src/private/webapis/dom/events/__tests__/EventTarget-itest.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ describe('EventTarget', () => {
8383
expect(methods.size).toBe(0);
8484
});
8585

86+
it('resets dispatch state so an Event can be reused', () => {
87+
const event = new Event('custom');
88+
const target = new EventTarget();
89+
let calls = 0;
90+
91+
target.addEventListener('custom', received => {
92+
calls++;
93+
expect(received).toBe(event);
94+
expect(received.eventPhase).toBe(Event.AT_TARGET);
95+
expect(received.currentTarget).toBe(target);
96+
expect(received.composedPath()).toEqual([target]);
97+
expect(received.composedPath()).not.toBe(received.composedPath());
98+
});
99+
100+
expect(target.dispatchEvent(event)).toBe(true);
101+
expect(event.eventPhase).toBe(Event.NONE);
102+
expect(event.currentTarget).toBeNull();
103+
expect(event.composedPath()).toEqual([]);
104+
105+
expect(target.dispatchEvent(event)).toBe(true);
106+
expect(event.eventPhase).toBe(Event.NONE);
107+
expect(event.currentTarget).toBeNull();
108+
expect(event.composedPath()).toEqual([]);
109+
expect(calls).toBe(2);
110+
});
111+
86112
describe('addEventListener', () => {
87113
it('should throw an error if event or callback are NOT passed', () => {
88114
const eventTarget = new EventTarget();

0 commit comments

Comments
 (0)