Skip to content

Commit 9c3de25

Browse files
authored
Flow: types first in reconciler (#25362)
This contains one code change, renaming the local function `ChildReconciler` to `createChildReconciler` as it's called as a function, not a constructor and to free up the name for the return value.
1 parent 0033d1a commit 9c3de25

13 files changed

+118
-59
lines changed

packages/react-reconciler/src/ReactChildFiber.new.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,18 @@ function resolveLazy(lazyType) {
264264
return init(payload);
265265
}
266266

267+
type ChildReconciler = (
268+
returnFiber: Fiber,
269+
currentFirstChild: Fiber | null,
270+
newChild: any,
271+
lanes: Lanes,
272+
) => Fiber | null;
273+
267274
// This wrapper function exists because I expect to clone the code in each path
268275
// to be able to optimize each path individually by branching early. This needs
269276
// a compiler or we can do it manually. Helpers that don't need this branching
270277
// live outside of this function.
271-
function ChildReconciler(shouldTrackSideEffects) {
278+
function createChildReconciler(shouldTrackSideEffects): ChildReconciler {
272279
function deleteChild(returnFiber: Fiber, childToDelete: Fiber): void {
273280
if (!shouldTrackSideEffects) {
274281
// Noop.
@@ -1352,8 +1359,10 @@ function ChildReconciler(shouldTrackSideEffects) {
13521359
return reconcileChildFibers;
13531360
}
13541361

1355-
export const reconcileChildFibers = ChildReconciler(true);
1356-
export const mountChildFibers = ChildReconciler(false);
1362+
export const reconcileChildFibers: ChildReconciler = createChildReconciler(
1363+
true,
1364+
);
1365+
export const mountChildFibers: ChildReconciler = createChildReconciler(false);
13571366

13581367
export function cloneChildFibers(
13591368
current: Fiber | null,

packages/react-reconciler/src/ReactChildFiber.old.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,18 @@ function resolveLazy(lazyType) {
264264
return init(payload);
265265
}
266266

267+
type ChildReconciler = (
268+
returnFiber: Fiber,
269+
currentFirstChild: Fiber | null,
270+
newChild: any,
271+
lanes: Lanes,
272+
) => Fiber | null;
273+
267274
// This wrapper function exists because I expect to clone the code in each path
268275
// to be able to optimize each path individually by branching early. This needs
269276
// a compiler or we can do it manually. Helpers that don't need this branching
270277
// live outside of this function.
271-
function ChildReconciler(shouldTrackSideEffects) {
278+
function createChildReconciler(shouldTrackSideEffects): ChildReconciler {
272279
function deleteChild(returnFiber: Fiber, childToDelete: Fiber): void {
273280
if (!shouldTrackSideEffects) {
274281
// Noop.
@@ -1352,8 +1359,10 @@ function ChildReconciler(shouldTrackSideEffects) {
13521359
return reconcileChildFibers;
13531360
}
13541361

1355-
export const reconcileChildFibers = ChildReconciler(true);
1356-
export const mountChildFibers = ChildReconciler(false);
1362+
export const reconcileChildFibers: ChildReconciler = createChildReconciler(
1363+
true,
1364+
);
1365+
export const mountChildFibers: ChildReconciler = createChildReconciler(false);
13571366

13581367
export function cloneChildFibers(
13591368
current: Fiber | null,

packages/react-reconciler/src/ReactFiberClassComponent.new.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const fakeInternalInstance = {};
8383

8484
// React.Component uses a shared frozen object by default.
8585
// We'll use it to determine whether we need to initialize legacy refs.
86-
export const emptyRefsObject = new React.Component().refs;
86+
export const emptyRefsObject: $FlowFixMe = new React.Component().refs;
8787

8888
let didWarnAboutStateAssignmentForComponent;
8989
let didWarnAboutUninitializedState;

packages/react-reconciler/src/ReactFiberClassComponent.old.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const fakeInternalInstance = {};
8383

8484
// React.Component uses a shared frozen object by default.
8585
// We'll use it to determine whether we need to initialize legacy refs.
86-
export const emptyRefsObject = new React.Component().refs;
86+
export const emptyRefsObject: $FlowFixMe = new React.Component().refs;
8787

8888
let didWarnAboutStateAssignmentForComponent;
8989
let didWarnAboutUninitializedState;

packages/react-reconciler/src/ReactFiberContext.new.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ if (__DEV__) {
2424
warnedAboutMissingGetChildContext = {};
2525
}
2626

27-
export const emptyContextObject = {};
27+
// $FlowFixMe[incompatible-exact]
28+
export const emptyContextObject: {} = {};
2829
if (__DEV__) {
2930
Object.freeze(emptyContextObject);
3031
}

packages/react-reconciler/src/ReactFiberContext.old.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ if (__DEV__) {
2424
warnedAboutMissingGetChildContext = {};
2525
}
2626

27-
export const emptyContextObject = {};
27+
// $FlowFixMe[incompatible-exact]
28+
export const emptyContextObject: {} = {};
2829
if (__DEV__) {
2930
Object.freeze(emptyContextObject);
3031
}

packages/react-reconciler/src/ReactFiberHotReloading.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ export type FindHostInstancesForRefresh = (
6363
export const setRefreshHandler: (
6464
handler: RefreshHandler | null,
6565
) => void = enableNewReconciler ? setRefreshHandler_new : setRefreshHandler_old;
66-
export const resolveFunctionForHotReloading = enableNewReconciler
66+
export const resolveFunctionForHotReloading: typeof resolveFunctionForHotReloading_new = enableNewReconciler
6767
? resolveFunctionForHotReloading_new
6868
: resolveFunctionForHotReloading_old;
69-
export const resolveClassForHotReloading = enableNewReconciler
69+
export const resolveClassForHotReloading: typeof resolveClassForHotReloading_new = enableNewReconciler
7070
? resolveClassForHotReloading_new
7171
: resolveClassForHotReloading_old;
72-
export const resolveForwardRefForHotReloading = enableNewReconciler
72+
export const resolveForwardRefForHotReloading: typeof resolveForwardRefForHotReloading_new = enableNewReconciler
7373
? resolveForwardRefForHotReloading_new
7474
: resolveForwardRefForHotReloading_old;
7575
export const isCompatibleFamilyForHotReloading: (

packages/react-reconciler/src/ReactFiberReconciler.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -90,107 +90,113 @@ import {
9090
getCurrentUpdatePriority as getCurrentUpdatePriority_new,
9191
} from './ReactFiberReconciler.new';
9292

93-
export const createContainer = enableNewReconciler
93+
export const createContainer: typeof createContainer_new = enableNewReconciler
9494
? createContainer_new
9595
: createContainer_old;
96-
export const createHydrationContainer = enableNewReconciler
96+
export const createHydrationContainer: typeof createHydrationContainer_new = enableNewReconciler
9797
? createHydrationContainer_new
9898
: createHydrationContainer_old;
99-
export const updateContainer = enableNewReconciler
99+
export const updateContainer: typeof updateContainer_new = enableNewReconciler
100100
? updateContainer_new
101101
: updateContainer_old;
102-
export const batchedUpdates = enableNewReconciler
102+
export const batchedUpdates: typeof batchedUpdates_new = enableNewReconciler
103103
? batchedUpdates_new
104104
: batchedUpdates_old;
105-
export const deferredUpdates = enableNewReconciler
105+
export const deferredUpdates: typeof deferredUpdates_new = enableNewReconciler
106106
? deferredUpdates_new
107107
: deferredUpdates_old;
108-
export const discreteUpdates = enableNewReconciler
108+
export const discreteUpdates: typeof discreteUpdates_new = enableNewReconciler
109109
? discreteUpdates_new
110110
: discreteUpdates_old;
111-
export const flushControlled = enableNewReconciler
111+
export const flushControlled: typeof flushControlled_new = enableNewReconciler
112112
? flushControlled_new
113113
: flushControlled_old;
114-
export const flushSync = enableNewReconciler ? flushSync_new : flushSync_old;
115-
export const isAlreadyRendering = enableNewReconciler
114+
export const flushSync: typeof flushSync_new = enableNewReconciler
115+
? flushSync_new
116+
: flushSync_old;
117+
export const isAlreadyRendering: typeof isAlreadyRendering_new = enableNewReconciler
116118
? isAlreadyRendering_new
117119
: isAlreadyRendering_old;
118-
export const flushPassiveEffects = enableNewReconciler
120+
export const flushPassiveEffects: typeof flushPassiveEffects_new = enableNewReconciler
119121
? flushPassiveEffects_new
120122
: flushPassiveEffects_old;
121-
export const getPublicRootInstance = enableNewReconciler
123+
export const getPublicRootInstance: typeof getPublicRootInstance_new = enableNewReconciler
122124
? getPublicRootInstance_new
123125
: getPublicRootInstance_old;
124-
export const attemptSynchronousHydration = enableNewReconciler
126+
export const attemptSynchronousHydration: typeof attemptSynchronousHydration_new = enableNewReconciler
125127
? attemptSynchronousHydration_new
126128
: attemptSynchronousHydration_old;
127-
export const attemptDiscreteHydration = enableNewReconciler
129+
export const attemptDiscreteHydration: typeof attemptDiscreteHydration_new = enableNewReconciler
128130
? attemptDiscreteHydration_new
129131
: attemptDiscreteHydration_old;
130-
export const attemptContinuousHydration = enableNewReconciler
132+
export const attemptContinuousHydration: typeof attemptContinuousHydration_new = enableNewReconciler
131133
? attemptContinuousHydration_new
132134
: attemptContinuousHydration_old;
133-
export const attemptHydrationAtCurrentPriority = enableNewReconciler
135+
export const attemptHydrationAtCurrentPriority: typeof attemptHydrationAtCurrentPriority_new = enableNewReconciler
134136
? attemptHydrationAtCurrentPriority_new
135137
: attemptHydrationAtCurrentPriority_old;
136-
export const getCurrentUpdatePriority = enableNewReconciler
138+
export const getCurrentUpdatePriority: typeof getCurrentUpdatePriority_new = enableNewReconciler
137139
? getCurrentUpdatePriority_new
138-
: getCurrentUpdatePriority_old;
139-
export const findHostInstance = enableNewReconciler
140+
: /* $FlowFixMe[incompatible-type] opaque types EventPriority from new and old
141+
* are incompatible. */
142+
getCurrentUpdatePriority_old;
143+
export const findHostInstance: typeof findHostInstance_new = enableNewReconciler
140144
? findHostInstance_new
141145
: findHostInstance_old;
142-
export const findHostInstanceWithWarning = enableNewReconciler
146+
export const findHostInstanceWithWarning: typeof findHostInstanceWithWarning_new = enableNewReconciler
143147
? findHostInstanceWithWarning_new
144148
: findHostInstanceWithWarning_old;
145-
export const findHostInstanceWithNoPortals = enableNewReconciler
149+
export const findHostInstanceWithNoPortals: typeof findHostInstanceWithNoPortals_new = enableNewReconciler
146150
? findHostInstanceWithNoPortals_new
147151
: findHostInstanceWithNoPortals_old;
148-
export const shouldError = enableNewReconciler
152+
export const shouldError: typeof shouldError_new = enableNewReconciler
149153
? shouldError_new
150154
: shouldError_old;
151-
export const shouldSuspend = enableNewReconciler
155+
export const shouldSuspend: typeof shouldSuspend_new = enableNewReconciler
152156
? shouldSuspend_new
153157
: shouldSuspend_old;
154-
export const injectIntoDevTools = enableNewReconciler
158+
export const injectIntoDevTools: typeof injectIntoDevTools_new = enableNewReconciler
155159
? injectIntoDevTools_new
156160
: injectIntoDevTools_old;
157-
export const createPortal = enableNewReconciler
161+
export const createPortal: typeof createPortal_new = enableNewReconciler
158162
? createPortal_new
159163
: createPortal_old;
160-
export const createComponentSelector = enableNewReconciler
164+
export const createComponentSelector: typeof createComponentSelector_new = enableNewReconciler
161165
? createComponentSelector_new
162166
: createComponentSelector_old;
163167

164-
export const createHasPseudoClassSelector = enableNewReconciler
168+
export const createHasPseudoClassSelector: typeof createHasPseudoClassSelector_new = enableNewReconciler
165169
? createHasPseudoClassSelector_new
166170
: createHasPseudoClassSelector_old;
167-
export const createRoleSelector = enableNewReconciler
171+
export const createRoleSelector: typeof createRoleSelector_new = enableNewReconciler
168172
? createRoleSelector_new
169173
: createRoleSelector_old;
170-
export const createTextSelector = enableNewReconciler
174+
export const createTextSelector: typeof createTextSelector_new = enableNewReconciler
171175
? createTextSelector_new
172176
: createTextSelector_old;
173-
export const createTestNameSelector = enableNewReconciler
177+
export const createTestNameSelector: typeof createTestNameSelector_new = enableNewReconciler
174178
? createTestNameSelector_new
175179
: createTestNameSelector_old;
176-
export const getFindAllNodesFailureDescription = enableNewReconciler
180+
export const getFindAllNodesFailureDescription: typeof getFindAllNodesFailureDescription_new = enableNewReconciler
177181
? getFindAllNodesFailureDescription_new
178182
: getFindAllNodesFailureDescription_old;
179-
export const findAllNodes = enableNewReconciler
183+
export const findAllNodes: typeof findAllNodes_new = enableNewReconciler
180184
? findAllNodes_new
181185
: findAllNodes_old;
182-
export const findBoundingRects = enableNewReconciler
186+
export const findBoundingRects: typeof findBoundingRects_new = enableNewReconciler
183187
? findBoundingRects_new
184188
: findBoundingRects_old;
185-
export const focusWithin = enableNewReconciler
189+
export const focusWithin: typeof focusWithin_new = enableNewReconciler
186190
? focusWithin_new
187191
: focusWithin_old;
188-
export const observeVisibleRects = enableNewReconciler
192+
export const observeVisibleRects: typeof observeVisibleRects_new = enableNewReconciler
189193
? observeVisibleRects_new
190194
: observeVisibleRects_old;
191-
export const registerMutableSourceForHydration = enableNewReconciler
195+
export const registerMutableSourceForHydration: typeof registerMutableSourceForHydration_new = enableNewReconciler
192196
? registerMutableSourceForHydration_new
193197
: registerMutableSourceForHydration_old;
194-
export const runWithPriority = enableNewReconciler
198+
/* $FlowFixMe[incompatible-type] opaque types EventPriority from new and old
199+
* are incompatible. */
200+
export const runWithPriority: typeof runWithPriority_new = enableNewReconciler
195201
? runWithPriority_new
196202
: runWithPriority_old;

packages/react-reconciler/src/ReactFiberUnwindWork.new.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function unwindWork(
6363
current: Fiber | null,
6464
workInProgress: Fiber,
6565
renderLanes: Lanes,
66-
) {
66+
): Fiber | null {
6767
// Note: This intentionally doesn't check if we're hydrating because comparing
6868
// to the current tree provider fiber is just as fast and less error-prone.
6969
// Ideally we would have a special version of the work loop only

packages/react-reconciler/src/ReactFiberUnwindWork.old.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function unwindWork(
6363
current: Fiber | null,
6464
workInProgress: Fiber,
6565
renderLanes: Lanes,
66-
) {
66+
): Fiber | null {
6767
// Note: This intentionally doesn't check if we're hydrating because comparing
6868
// to the current tree provider fiber is just as fast and less error-prone.
6969
// Ideally we would have a special version of the work loop only

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ export function getWorkInProgressRootRenderLanes(): Lanes {
580580
return workInProgressRootRenderLanes;
581581
}
582582

583-
export function requestEventTime() {
583+
export function requestEventTime(): number {
584584
if ((executionContext & (RenderContext | CommitContext)) !== NoContext) {
585585
// We're inside React, so it's fine to read the actual time.
586586
return now();
@@ -595,7 +595,7 @@ export function requestEventTime() {
595595
return currentEventTime;
596596
}
597597

598-
export function getCurrentTime() {
598+
export function getCurrentTime(): number {
599599
return now();
600600
}
601601

@@ -1567,7 +1567,7 @@ declare function flushSync<R>(fn: () => R): R;
15671567
// eslint-disable-next-line no-redeclare
15681568
declare function flushSync(): void;
15691569
// eslint-disable-next-line no-redeclare
1570-
export function flushSync(fn): void {
1570+
export function flushSync<R>(fn: (() => R) | void): R | void {
15711571
// In legacy mode, we flush pending passive effects at the beginning of the
15721572
// next event, not at the end of the previous one.
15731573
if (
@@ -1615,7 +1615,7 @@ export function isAlreadyRendering(): boolean {
16151615
);
16161616
}
16171617

1618-
export function isInvalidExecutionContextForEventFunction() {
1618+
export function isInvalidExecutionContextForEventFunction(): boolean {
16191619
// Used to throw if certain APIs are called from the wrong context.
16201620
return (executionContext & RenderContext) !== NoContext;
16211621
}

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ export function getWorkInProgressRootRenderLanes(): Lanes {
580580
return workInProgressRootRenderLanes;
581581
}
582582

583-
export function requestEventTime() {
583+
export function requestEventTime(): number {
584584
if ((executionContext & (RenderContext | CommitContext)) !== NoContext) {
585585
// We're inside React, so it's fine to read the actual time.
586586
return now();
@@ -595,7 +595,7 @@ export function requestEventTime() {
595595
return currentEventTime;
596596
}
597597

598-
export function getCurrentTime() {
598+
export function getCurrentTime(): number {
599599
return now();
600600
}
601601

@@ -1567,7 +1567,7 @@ declare function flushSync<R>(fn: () => R): R;
15671567
// eslint-disable-next-line no-redeclare
15681568
declare function flushSync(): void;
15691569
// eslint-disable-next-line no-redeclare
1570-
export function flushSync(fn): void {
1570+
export function flushSync<R>(fn: (() => R) | void): R | void {
15711571
// In legacy mode, we flush pending passive effects at the beginning of the
15721572
// next event, not at the end of the previous one.
15731573
if (
@@ -1615,7 +1615,7 @@ export function isAlreadyRendering(): boolean {
16151615
);
16161616
}
16171617

1618-
export function isInvalidExecutionContextForEventFunction() {
1618+
export function isInvalidExecutionContextForEventFunction(): boolean {
16191619
// Used to throw if certain APIs are called from the wrong context.
16201620
return (executionContext & RenderContext) !== NoContext;
16211621
}

0 commit comments

Comments
 (0)