@samitouri / QOS-React / commits / e8f3f92a88

fixed flow issues and bug resulting in failing tests

Dominic Gannaway committed Mar 2, 2017 at 17:53 UTC e8f3f92a88ac90ba4c95c8fae1b563acf29d959e
5 files changed +98 -48
src/isomorphic/classic/element/ReactDebugCurrentFrame.js
+13 -4
@@ -14,11 +14,16 @@
14
15 import type { Fiber } from 'ReactFiber';
16 import type { DebugID } from 'ReactInstanceType';
17 +import type { ComponentTreeHookType } from '../../hooks/ReactComponentTreeHook';
18
19 const ReactDebugCurrentFrame = {};
20
21 if (__DEV__) {
21 - var ReactComponentTreeHook = require('ReactComponentTreeHook');
22 + const {
23 + getStackAddendumByID,
24 + getStackAddendumByWorkInProgressFiber,
25 + getCurrentStackAddendum,
26 + }: ComponentTreeHookType = require('ReactComponentTreeHook');
27 // Component that is being worked on
28 ReactDebugCurrentFrame.current = (null : Fiber | DebugID | null);
29
@@ -33,16 +38,20 @@ if (__DEV__) {
38 if (typeof current === 'number') {
39 // DebugID from Stack.
40 const debugID = current;
36 - stack = (ReactComponentTreeHook: any).getStackAddendumByID(debugID);
41 + if (getStackAddendumByID) {
42 + stack = getStackAddendumByID(debugID);
43 + }
44 } else if (typeof current.tag === 'number') {
45 // This is a Fiber.
46 // The stack will only be correct if this is a work in progress
47 // version and we're calling it during reconciliation.
48 const workInProgress = current;
42 - stack = ReactComponentTreeHook.getStackAddendumByWorkInProgressFiber(workInProgress);
49 + stack = getStackAddendumByWorkInProgressFiber(workInProgress);
50 }
51 } else if (element !== null) {
45 - stack = (ReactComponentTreeHook: any).getCurrentStackAddendum(element);
52 + if (getCurrentStackAddendum) {
53 + stack = getCurrentStackAddendum(element);
54 + }
55 }
56 return stack;
57 };
src/isomorphic/hooks/ReactComponentTreeHook.js
+43 -21
@@ -55,7 +55,21 @@ function describeFiber(fiber : Fiber) : string {
55 }
56 }
57
58 -var ReactComponentTreeHook = {
58 +export type ComponentTreeHookType = {
59 + getStackAddendumByWorkInProgressFiber: (Fiber) => any,
60 + getStackAddendumByID?: () => any,
61 + getCurrentStackAddendum?: () => any,
62 + purgeUnmountedComponents?: () => any,
63 + getOwnerID?: (DebugID) => any,
64 + getParentID?: (DebugID) => any,
65 + getDisplayName?: (DebugID) => any,
66 + getText?: (DebugID) => any,
67 + getUpdateCount?: (DebugID) => any,
68 + getChildIDs?: (DebugID) => any,
69 + getRegisteredIDs?: () => any,
70 +};
71 +
72 +var ReactComponentTreeHook: ComponentTreeHookType = {
73 // This function can only be called with a work-in-progress fiber and
74 // only during begin or complete phase. Do not call it under any other
75 // circumstances.
@@ -195,9 +209,9 @@ if (__DEV__) {
209 };
210 }
211
198 - var unmountedIDs: Array<DebugID> = [];
212 + const unmountedIDs: Array<DebugID> = [];
213
200 - var purgeDeep = function(id) {
214 + const purgeDeep = function(id) {
215 var item = getItem(id);
216 if (item) {
217 var {childIDs} = item;
@@ -206,7 +220,7 @@ if (__DEV__) {
220 }
221 };
222
209 - var getDisplayName = function(element: ?ReactElement): string {
223 + const getDisplayNameFromElement = function(element: ?ReactElement): string {
224 if (element == null) {
225 return '#empty';
226 } else if (typeof element === 'string' || typeof element === 'number') {
@@ -218,10 +232,26 @@ if (__DEV__) {
232 }
233 };
234
221 - var describeID = function(id: DebugID): string {
222 - var name = getDisplayName((id: any));
235 + const getDisplayName = function(id: DebugID): ?string {
236 var element = getElement(id);
224 - var ownerID: any = getOwnerID(id);
237 + if (!element) {
238 + return null;
239 + }
240 + return getDisplayNameFromElement(element);
241 + };
242 +
243 + const getOwnerID = function(id: DebugID): ?DebugID {
244 + var element = getElement(id);
245 + if (!element || !element._owner) {
246 + return null;
247 + }
248 + return element._owner._debugID;
249 + };
250 +
251 + const describeID = function(id: DebugID): string {
252 + var name = getDisplayName(id);
253 + var element = getElement(id);
254 + var ownerID = getOwnerID(id);
255 var ownerName;
256 if (ownerID) {
257 ownerName = getDisplayName(ownerID);
@@ -235,25 +265,17 @@ if (__DEV__) {
265 return describeComponentFrame(name, element && element._source, ownerName);
266 };
267
238 - var getOwnerID = function(id: DebugID): ?DebugID {
239 - var element = getElement(id);
240 - if (!element || !element._owner) {
241 - return null;
242 - }
243 - return element._owner._debugID;
244 - };
245 -
246 - var getElement = function(id: DebugID): ?ReactElement {
268 + const getElement = function(id: DebugID): ?ReactElement {
269 var item = getItem(id);
270 return item ? item.element : null;
271 };
272
251 - var getParentID = function(id: DebugID): ?DebugID {
273 + const getParentID = function(id: DebugID): ?DebugID {
274 var item = getItem(id);
275 return item ? item.parentID : null;
276 };
255 -
256 - var getStackAddendumByID = function(id: ?DebugID): string {
277 +
278 + const getStackAddendumByID = function(id: ?DebugID): string {
279 var info = '';
280 while (id) {
281 info += describeID(id);
@@ -385,7 +407,7 @@ if (__DEV__) {
407 getCurrentStackAddendum(topElement: ?ReactElement): string {
408 var info = '';
409 if (topElement) {
388 - var name = getDisplayName(topElement);
410 + var name = getDisplayNameFromElement(topElement);
411 var owner = topElement._owner;
412 info += describeComponentFrame(
413 name,
@@ -420,7 +442,7 @@ if (__DEV__) {
442 if (!element) {
443 return null;
444 }
423 - return getDisplayName(element);
445 + return getDisplayNameFromElement(element);
446 },
447
448 getElement,
src/renderers/shared/ReactDebugTool.js
+35 -20
@@ -14,7 +14,6 @@
14
15 var ReactInvalidSetStateWarningHook = require('ReactInvalidSetStateWarningHook');
16 var ReactHostOperationHistoryHook = require('ReactHostOperationHistoryHook');
17 -var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
17 var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
18
19 var performanceNow = require('fbjs/lib/performanceNow');
@@ -67,8 +66,19 @@ export type FlushHistory = Array<HistoryItem>;
66 var ReactDebugTool = ((null: any): typeof ReactDebugTool);
67
68 if (__DEV__) {
70 - var hooks = [];
71 - var didHookThrowForEvent = {};
69 + const hooks = [];
70 + const didHookThrowForEvent = {};
71 + const ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
72 + const {
73 + purgeUnmountedComponents,
74 + getOwnerID,
75 + getParentID,
76 + getDisplayName,
77 + getText,
78 + getUpdateCount,
79 + getChildIDs,
80 + getRegisteredIDs,
81 + } = ReactComponentTreeHook;
82
83 const callHook = function(event, fn, context, arg1, arg2, arg3, arg4, arg5) {
84 try {
@@ -108,22 +118,24 @@ if (__DEV__) {
118 var lifeCycleTimerHasWarned = false;
119
120 const clearHistory = function() {
111 - (ReactComponentTreeHook: any).purgeUnmountedComponents();
121 + if (purgeUnmountedComponents) {
122 + purgeUnmountedComponents();
123 + }
124 ReactHostOperationHistoryHook.clearHistory();
125 };
126
127 const getTreeSnapshot = function(registeredIDs) {
116 - return registeredIDs.reduce((tree, id) => {
117 - var ownerID = (ReactComponentTreeHook: any).getOwnerID(id);
118 - var parentID = (ReactComponentTreeHook: any).getParentID(id);
128 + return registeredIDs && registeredIDs.reduce((tree, id) => {
129 + var ownerID = getOwnerID && getOwnerID(id);
130 + var parentID = getParentID && getParentID(id);
131 tree[id] = {
120 - displayName: (ReactComponentTreeHook: any).getDisplayName(id),
121 - text: (ReactComponentTreeHook: any).getText(id),
122 - updateCount: (ReactComponentTreeHook: any).getUpdateCount(id),
123 - childIDs: (ReactComponentTreeHook: any).getChildIDs(id),
132 + displayName: getDisplayName && getDisplayName(id),
133 + text: getText && getText(id),
134 + updateCount: getUpdateCount && getUpdateCount(id),
135 + childIDs: getChildIDs && getChildIDs(id),
136 // Text nodes don't have owners but this is close enough.
137 ownerID: ownerID ||
126 - parentID && (ReactComponentTreeHook: any).getOwnerID(parentID) ||
138 + parentID && getOwnerID && getOwnerID(parentID) ||
139 0,
140 parentID,
141 };
@@ -144,13 +156,16 @@ if (__DEV__) {
156 }
157
158 if (previousMeasurements.length || previousOperations.length) {
147 - var registeredIDs = (ReactComponentTreeHook: any).getRegisteredIDs();
148 - flushHistory.push({
149 - duration: performanceNow() - previousStartTime,
150 - measurements: previousMeasurements || [],
151 - operations: previousOperations || [],
152 - treeSnapshot: getTreeSnapshot(registeredIDs),
153 - });
159 + var registeredIDs = getRegisteredIDs && getRegisteredIDs();
160 +
161 + if (registeredIDs) {
162 + flushHistory.push({
163 + duration: performanceNow() - previousStartTime,
164 + measurements: previousMeasurements || [],
165 + operations: previousOperations || [],
166 + treeSnapshot: getTreeSnapshot(registeredIDs),
167 + });
168 + }
169 }
170
171 clearHistory();
@@ -280,7 +295,7 @@ if (__DEV__) {
295 }
296
297 var markName = `${debugID}::${markType}`;
283 - var displayName = (ReactComponentTreeHook: any).getDisplayName(debugID) || 'Unknown';
298 + var displayName = getDisplayName && getDisplayName(debugID) || 'Unknown';
299
300 // Chrome has an issue of dropping markers recorded too fast:
301 // https://bugs.chromium.org/p/chromium/issues/detail?id=640652
src/renderers/shared/stack/reconciler/ReactRef.js
+4 -2
@@ -21,7 +21,9 @@ var ReactRef = {};
21
22 if (__DEV__) {
23 var ReactCompositeComponentTypes = require('ReactCompositeComponentTypes');
24 - var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
24 + var {
25 + getStackAddendumByID,
26 + } = require('react/lib/ReactComponentTreeHook');
27 var warning = require('fbjs/lib/warning');
28
29 var warnedAboutStatelessRefs = {};
@@ -53,7 +55,7 @@ function attachRef(ref, component, owner) {
55 'Stateless function components cannot be given refs. ' +
56 'Attempts to access this ref will fail.%s%s',
57 info,
56 - (ReactComponentTreeHook: any).getStackAddendumByID(component._debugID)
58 + getStackAddendumByID && getStackAddendumByID(component._debugID)
59 );
60 }
61 }
src/shared/utils/flattenChildren.js
+3 -1
@@ -51,6 +51,8 @@ function flattenSingleChildIntoContext(
51 if (!ReactComponentTreeHook) {
52 ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
53 }
54 + const { getStackAddendumByID } = ReactComponentTreeHook;
55 +
56 if (!keyUnique) {
57 warning(
58 false,
@@ -58,7 +60,7 @@ function flattenSingleChildIntoContext(
60 '`%s`. Child keys must be unique; when two children share a key, only ' +
61 'the first child will be used.%s',
62 KeyEscapeUtils.unescape(name),
61 - (ReactComponentTreeHook: any).getStackAddendumByID(selfDebugID)
63 + getStackAddendumByID && getStackAddendumByID(selfDebugID)
64 );
65 }
66 }