From 28ce2aaedd62020621d5a82fe6a314fc8eda9335 Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Tue, 20 Nov 2018 18:36:42 +0800 Subject: [PATCH 1/8] [react] [flow] Add missing @flow annotations for: - forwardRef.js - memo.js - ReactLazy.js --- packages/react/src/ReactLazy.js | 2 ++ packages/react/src/forwardRef.js | 2 ++ packages/react/src/memo.js | 2 ++ packages/shared/ReactLazyComponent.js | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/react/src/ReactLazy.js b/packages/react/src/ReactLazy.js index 28415fcf265..b62bf43acf1 100644 --- a/packages/react/src/ReactLazy.js +++ b/packages/react/src/ReactLazy.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ import type {LazyComponent, Thenable} from 'shared/ReactLazyComponent'; diff --git a/packages/react/src/forwardRef.js b/packages/react/src/forwardRef.js index c2617e4eef9..63fd7ae495c 100644 --- a/packages/react/src/forwardRef.js +++ b/packages/react/src/forwardRef.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ import {REACT_FORWARD_REF_TYPE, REACT_MEMO_TYPE} from 'shared/ReactSymbols'; diff --git a/packages/react/src/memo.js b/packages/react/src/memo.js index 0fda3a8f67e..83f02e77462 100644 --- a/packages/react/src/memo.js +++ b/packages/react/src/memo.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ import {REACT_MEMO_TYPE} from 'shared/ReactSymbols'; diff --git a/packages/shared/ReactLazyComponent.js b/packages/shared/ReactLazyComponent.js index f3042ab11ae..2033a7fad9e 100644 --- a/packages/shared/ReactLazyComponent.js +++ b/packages/shared/ReactLazyComponent.js @@ -14,7 +14,7 @@ export type Thenable = { export type LazyComponent = { $$typeof: Symbol | number, _ctor: () => Thenable<{default: T}, mixed>, - _status: 0 | 1 | 2, + _status: -1 | 0 | 1 | 2, // TODO: what status is -1? _result: any, }; From 5fb67d05c9472f3fc48f082ee40ab378810f9965 Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Tue, 20 Nov 2018 20:26:02 +0800 Subject: [PATCH 2/8] [eslint] Enable rule --- .eslintrc.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index 51c9395f73c..cd308d281a7 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -19,6 +19,7 @@ module.exports = { 'no-for-of-loops', 'react', 'react-internal', + 'flowtype', ], parser: 'espree', @@ -89,6 +90,7 @@ module.exports = { 'react-internal/no-primitive-constructors': ERROR, 'react-internal/no-to-warn-dev-within-to-throw': ERROR, 'react-internal/warning-and-invariant-args': ERROR, + 'flowtype/no-types-missing-file-annotation': ERROR, }, overrides: [ From a53c23f89cae04b3fcf38509279942b4da93b6de Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Tue, 20 Nov 2018 20:26:30 +0800 Subject: [PATCH 3/8] [flow] Add missing @flow annotations for: - packages/react-art/src/ReactARTHostConfig.js - packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js - packages/react-dom/src/__tests__/ReactCompositeComponent-test.js - packages/react-dom/src/shared/DOMNamespaces.js - packages/react-dom/src/shared/assertValidProps.js - packages/react-native-renderer/src/__mocks__/deepDiffer.js - packages/scheduler/src/Scheduler.js - packages/scheduler/src/__tests__/SchedulerDOM-test.js - packages/shared/forks/invokeGuardedCallbackImpl.www.js --- packages/react-art/src/ReactARTHostConfig.js | 2 +- .../react-dom/src/__tests__/ReactComponentLifeCycle-test.js | 1 + .../react-dom/src/__tests__/ReactCompositeComponent-test.js | 1 + packages/react-dom/src/shared/DOMNamespaces.js | 2 ++ packages/react-dom/src/shared/assertValidProps.js | 6 ++++-- packages/react-native-renderer/src/__mocks__/deepDiffer.js | 2 ++ packages/scheduler/src/Scheduler.js | 2 +- packages/scheduler/src/__tests__/SchedulerDOM-test.js | 1 + packages/shared/forks/invokeGuardedCallbackImpl.www.js | 3 +++ 9 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/react-art/src/ReactARTHostConfig.js b/packages/react-art/src/ReactARTHostConfig.js index 034b2d7290a..5bc78277f8b 100644 --- a/packages/react-art/src/ReactARTHostConfig.js +++ b/packages/react-art/src/ReactARTHostConfig.js @@ -421,6 +421,6 @@ export function unhideInstance(instance, props) { } } -export function unhideTextInstance(textInstance, text): void { +export function unhideTextInstance(textInstance, text) { // Noop } diff --git a/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js b/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js index 8879bca9987..f876196d049 100644 --- a/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js +++ b/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. * * @emails react-core + * @flow */ 'use strict'; diff --git a/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js b/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js index 43ac793cf59..84267933e4c 100644 --- a/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. * * @emails react-core + * @flow */ 'use strict'; diff --git a/packages/react-dom/src/shared/DOMNamespaces.js b/packages/react-dom/src/shared/DOMNamespaces.js index ca0e8fb3770..f19aa6e1ff8 100644 --- a/packages/react-dom/src/shared/DOMNamespaces.js +++ b/packages/react-dom/src/shared/DOMNamespaces.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'; diff --git a/packages/react-dom/src/shared/assertValidProps.js b/packages/react-dom/src/shared/assertValidProps.js index 9fe255b58da..59045e80e36 100644 --- a/packages/react-dom/src/shared/assertValidProps.js +++ b/packages/react-dom/src/shared/assertValidProps.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ import invariant from 'shared/invariant'; @@ -31,7 +33,7 @@ function assertValidProps(tag: string, props: ?Object) { '%s is a void element tag and must neither have `children` nor ' + 'use `dangerouslySetInnerHTML`.%s', tag, - __DEV__ ? ReactDebugCurrentFrame.getStackAddendum() : '', + __DEV__ && ReactDebugCurrentFrame ? ReactDebugCurrentFrame.getStackAddendum() : '', ); } if (props.dangerouslySetInnerHTML != null) { @@ -63,7 +65,7 @@ function assertValidProps(tag: string, props: ?Object) { 'The `style` prop expects a mapping from style properties to values, ' + "not a string. For example, style={{marginRight: spacing + 'em'}} when " + 'using JSX.%s', - __DEV__ ? ReactDebugCurrentFrame.getStackAddendum() : '', + __DEV__ && ReactDebugCurrentFrame ? ReactDebugCurrentFrame.getStackAddendum() : '', ); } diff --git a/packages/react-native-renderer/src/__mocks__/deepDiffer.js b/packages/react-native-renderer/src/__mocks__/deepDiffer.js index a4ede524658..88111330ed4 100644 --- a/packages/react-native-renderer/src/__mocks__/deepDiffer.js +++ b/packages/react-native-renderer/src/__mocks__/deepDiffer.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ 'use strict'; diff --git a/packages/scheduler/src/Scheduler.js b/packages/scheduler/src/Scheduler.js index c815d0071bd..f080a503839 100644 --- a/packages/scheduler/src/Scheduler.js +++ b/packages/scheduler/src/Scheduler.js @@ -98,7 +98,7 @@ function flushFirstCallback() { // A callback may return a continuation. The continuation should be scheduled // with the same priority and expiration as the just-finished callback. if (typeof continuationCallback === 'function') { - var continuationNode: CallbackNode = { + var continuationNode = { callback: continuationCallback, priorityLevel, expirationTime, diff --git a/packages/scheduler/src/__tests__/SchedulerDOM-test.js b/packages/scheduler/src/__tests__/SchedulerDOM-test.js index 562113f65bb..9c7b1b37547 100644 --- a/packages/scheduler/src/__tests__/SchedulerDOM-test.js +++ b/packages/scheduler/src/__tests__/SchedulerDOM-test.js @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. * * @emails react-core + * @flow */ 'use strict'; diff --git a/packages/shared/forks/invokeGuardedCallbackImpl.www.js b/packages/shared/forks/invokeGuardedCallbackImpl.www.js index c624679a8a7..2a0452e6065 100644 --- a/packages/shared/forks/invokeGuardedCallbackImpl.www.js +++ b/packages/shared/forks/invokeGuardedCallbackImpl.www.js @@ -3,11 +3,14 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ import invariant from 'shared/invariant'; // Provided by www +// $FlowIssue const ReactFbErrorUtils = require('ReactFbErrorUtils'); invariant( typeof ReactFbErrorUtils.invokeGuardedCallback === 'function', From 1856f59ade6524b72f9ea720eab045572503bac9 Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Tue, 20 Nov 2018 20:28:24 +0800 Subject: [PATCH 4/8] [flow] Fix ReactLazy ctor type --- packages/react/src/ReactLazy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/src/ReactLazy.js b/packages/react/src/ReactLazy.js index b62bf43acf1..62453d2a006 100644 --- a/packages/react/src/ReactLazy.js +++ b/packages/react/src/ReactLazy.js @@ -11,7 +11,7 @@ import type {LazyComponent, Thenable} from 'shared/ReactLazyComponent'; import {REACT_LAZY_TYPE} from 'shared/ReactSymbols'; -export function lazy(ctor: () => Thenable): LazyComponent { +export function lazy(ctor: () => Thenable<{ default: T }, mixed>): LazyComponent { return { $$typeof: REACT_LAZY_TYPE, _ctor: ctor, From 6f795b2f4c04f93b3a40caddf355d26e8953f2d1 Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Wed, 21 Nov 2018 01:25:15 +0800 Subject: [PATCH 5/8] tests: use shared shallowEqual func instead --- .../__tests__/ReactCompositeComponent-test.js | 38 +------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js b/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js index 84267933e4c..69451536629 100644 --- a/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js +++ b/packages/react-dom/src/__tests__/ReactCompositeComponent-test.js @@ -5,11 +5,12 @@ * LICENSE file in the root directory of this source tree. * * @emails react-core - * @flow */ 'use strict'; +import shallowEqual from 'shared/shallowEqual'; + let ChildUpdates; let MorphingComponent; let React; @@ -20,41 +21,6 @@ let ReactTestUtils; let PropTypes; describe('ReactCompositeComponent', () => { - const hasOwnProperty = Object.prototype.hasOwnProperty; - - /** - * Performs equality by iterating through keys on an object and returning false - * when any key has values which are not strictly equal between the arguments. - * Returns true when the values of all keys are strictly equal. - */ - function shallowEqual(objA: mixed, objB: mixed): boolean { - if (Object.is(objA, objB)) { - return true; - } - if ( - typeof objA !== 'object' || - objA === null || - typeof objB !== 'object' || - objB === null - ) { - return false; - } - const keysA = Object.keys(objA); - const keysB = Object.keys(objB); - if (keysA.length !== keysB.length) { - return false; - } - for (let i = 0; i < keysA.length; i++) { - if ( - !hasOwnProperty.call(objB, keysA[i]) || - !Object.is(objA[keysA[i]], objB[keysA[i]]) - ) { - return false; - } - } - return true; - } - function shallowCompare(instance, nextProps, nextState) { return ( !shallowEqual(instance.props, nextProps) || From ab84d4e985866eaf1c2e925a8e4b21e10c1fec4e Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Wed, 21 Nov 2018 01:25:56 +0800 Subject: [PATCH 6/8] Run Prettier --- packages/react/src/ReactLazy.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/react/src/ReactLazy.js b/packages/react/src/ReactLazy.js index 62453d2a006..4b8eb08df29 100644 --- a/packages/react/src/ReactLazy.js +++ b/packages/react/src/ReactLazy.js @@ -11,7 +11,9 @@ import type {LazyComponent, Thenable} from 'shared/ReactLazyComponent'; import {REACT_LAZY_TYPE} from 'shared/ReactSymbols'; -export function lazy(ctor: () => Thenable<{ default: T }, mixed>): LazyComponent { +export function lazy( + ctor: () => Thenable<{default: T}, mixed>, +): LazyComponent { return { $$typeof: REACT_LAZY_TYPE, _ctor: ctor, From 104b31f10c539b9ccf9ad1382929eaa8fdccd271 Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Wed, 21 Nov 2018 20:37:37 +0800 Subject: [PATCH 7/8] [flow] Annotate and fix Flow types for EventPropagators.js --- packages/events/EventPropagators.js | 26 +++++++++++++++++----- packages/events/ReactSyntheticEventType.js | 3 +++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/events/EventPropagators.js b/packages/events/EventPropagators.js index 5163c2b7a3f..a53d33c7005 100644 --- a/packages/events/EventPropagators.js +++ b/packages/events/EventPropagators.js @@ -3,8 +3,15 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ +import type {Fiber} from 'react-reconciler/src/ReactFiber'; +import type { + ReactSyntheticEvent, + DispatchConfig, +} from './ReactSyntheticEventType'; import { getParentInstance, traverseTwoPhase, @@ -106,24 +113,33 @@ function accumulateDispatches(inst, ignoredDirection, event) { * `dispatchMarker`. * @param {SyntheticEvent} event */ -function accumulateDirectDispatchesSingle(event) { +function accumulateDirectDispatchesSingle(event: ReactSyntheticEvent) { if (event && event.dispatchConfig.registrationName) { accumulateDispatches(event._targetInst, null, event); } } -export function accumulateTwoPhaseDispatches(events) { +export function accumulateTwoPhaseDispatches( + events: Array, +) { forEachAccumulated(events, accumulateTwoPhaseDispatchesSingle); } -export function accumulateTwoPhaseDispatchesSkipTarget(events) { +export function accumulateTwoPhaseDispatchesSkipTarget( + events: Array, +) { forEachAccumulated(events, accumulateTwoPhaseDispatchesSingleSkipTarget); } -export function accumulateEnterLeaveDispatches(leave, enter, from, to) { +export function accumulateEnterLeaveDispatches( + leave: DispatchConfig, + enter: DispatchConfig, + from: Fiber, + to: Fiber, +) { traverseEnterLeave(from, to, accumulateDispatches, leave, enter); } -export function accumulateDirectDispatches(events) { +export function accumulateDirectDispatches(events: Array) { forEachAccumulated(events, accumulateDirectDispatchesSingle); } diff --git a/packages/events/ReactSyntheticEventType.js b/packages/events/ReactSyntheticEventType.js index 07ddf270f2e..849dcb9b3e5 100644 --- a/packages/events/ReactSyntheticEventType.js +++ b/packages/events/ReactSyntheticEventType.js @@ -30,4 +30,7 @@ export type ReactSyntheticEvent = { nativeEventTarget: EventTarget, ) => ReactSyntheticEvent, isPersistent: () => boolean, + _targetInst: Fiber, + _dispatchListeners: ReactSyntheticEvent | Array, + _dispatchInstances: Fiber | Array, } & SyntheticEvent<>; From 82a599a531c10b48adfd4b1fa2d21978d5de9e6d Mon Sep 17 00:00:00 2001 From: raunofreiberg Date: Thu, 22 Nov 2018 19:00:35 +0800 Subject: [PATCH 8/8] [flow] Add @flow to BeforeInputEventPlugin for more obvious CI failure --- packages/react-dom/src/events/BeforeInputEventPlugin.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/react-dom/src/events/BeforeInputEventPlugin.js b/packages/react-dom/src/events/BeforeInputEventPlugin.js index 6895e0dc625..d7105bc47a5 100644 --- a/packages/react-dom/src/events/BeforeInputEventPlugin.js +++ b/packages/react-dom/src/events/BeforeInputEventPlugin.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ import type {TopLevelType} from 'events/TopLevelEventTypes';