From ddfd05fba9062f1a25ce571faf2c92a3c18e4b63 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 15 May 2017 17:30:22 +0100 Subject: [PATCH 01/22] fixed conflicts with master --- src/renderers/native/ReactNativeFiber.js | 2 + src/renderers/native/ReactNativeInspector.js | 64 ++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/renderers/native/ReactNativeInspector.js diff --git a/src/renderers/native/ReactNativeFiber.js b/src/renderers/native/ReactNativeFiber.js index 79a4acac1804..74533b6131db 100644 --- a/src/renderers/native/ReactNativeFiber.js +++ b/src/renderers/native/ReactNativeFiber.js @@ -23,6 +23,7 @@ const ReactNativeInjection = require('ReactNativeInjection'); const ReactNativeTagHandles = require('ReactNativeTagHandles'); const ReactNativeViewConfigRegistry = require('ReactNativeViewConfigRegistry'); const ReactPortal = require('ReactPortal'); +const ReactNativeInspector = require('ReactNativeInspector'); const ReactVersion = require('ReactVersion'); const UIManager = require('UIManager'); @@ -465,6 +466,7 @@ if (typeof injectInternals === 'function') { injectInternals({ findFiberByHostInstance: ReactNativeComponentTree.getClosestInstanceFromNode, findHostInstanceByFiber: NativeRenderer.findHostInstance, + getInspectorDataForViewTag: ReactNativeInspector.getInspectorDataForViewTag, // This is an enum because we may add more (e.g. profiler build) bundleType: __DEV__ ? 1 : 0, version: ReactVersion, diff --git a/src/renderers/native/ReactNativeInspector.js b/src/renderers/native/ReactNativeInspector.js new file mode 100644 index 000000000000..b6c6c107666f --- /dev/null +++ b/src/renderers/native/ReactNativeInspector.js @@ -0,0 +1,64 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactNativeInspector + * @flow + */ +'use strict'; + +const ReactNativeComponentTree = require('ReactNativeComponentTree'); + +if (__DEV__) { + var traverseOwnerTreeUp = function (hierarchy, instance) { + if (instance) { + hierarchy.unshift(instance); + const owner = typeof instance.tag === 'number' ? + // Fiber + instance._debugOwner : + // Stack + instance._currentElement._owner; + traverseOwnerTreeUp(hierarchy, owner); + } + }; + + var getOwnerHierarchy = function (instance) { + var hierarchy = []; + traverseOwnerTreeUp(hierarchy, instance); + return hierarchy; + }; + + var lastNotNativeInstance = function (hierarchy) { + for (let i = hierarchy.length - 1; i > 1; i--) { + const instance = hierarchy[i]; + if (!instance.viewConfig) { + return instance; + } + } + return hierarchy[0]; + }; + + var getInspectorDataForViewTag = function(viewTag: any): Object { + const fiber = ReactNativeComponentTree.findFiberByHostInstance(viewTag); + const hierarchy = getOwnerHierarchy(fiber); + const instance = lastNotNativeInstance(hierarchy); + const props = instance.memoizedProps || {}; + const source = instance._debugSource; + + return { + hierarchy, + instance, + props, + source, + }; + }; +} + +module.exports = { + getInspectorDataForViewTag, +}; + From 12e836770d7457c2e70f67abcc3275563069787c Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 15 May 2017 17:40:38 +0100 Subject: [PATCH 02/22] splits fiber and stack implementations --- src/renderers/native/ReactNativeFiber.js | 4 +- ...pector.js => ReactNativeFiberInspector.js} | 9 +-- src/renderers/native/ReactNativeStack.js | 2 + .../native/ReactNativeStackInspector.js | 59 +++++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) rename src/renderers/native/{ReactNativeInspector.js => ReactNativeFiberInspector.js} (85%) create mode 100644 src/renderers/native/ReactNativeStackInspector.js diff --git a/src/renderers/native/ReactNativeFiber.js b/src/renderers/native/ReactNativeFiber.js index 74533b6131db..b8e59bfe9f7d 100644 --- a/src/renderers/native/ReactNativeFiber.js +++ b/src/renderers/native/ReactNativeFiber.js @@ -23,7 +23,7 @@ const ReactNativeInjection = require('ReactNativeInjection'); const ReactNativeTagHandles = require('ReactNativeTagHandles'); const ReactNativeViewConfigRegistry = require('ReactNativeViewConfigRegistry'); const ReactPortal = require('ReactPortal'); -const ReactNativeInspector = require('ReactNativeInspector'); +const ReactNativeFiberInspector = require('ReactNativeFiberInspector'); const ReactVersion = require('ReactVersion'); const UIManager = require('UIManager'); @@ -466,7 +466,7 @@ if (typeof injectInternals === 'function') { injectInternals({ findFiberByHostInstance: ReactNativeComponentTree.getClosestInstanceFromNode, findHostInstanceByFiber: NativeRenderer.findHostInstance, - getInspectorDataForViewTag: ReactNativeInspector.getInspectorDataForViewTag, + getInspectorDataForViewTag: ReactNativeFiberInspector.getInspectorDataForViewTag, // This is an enum because we may add more (e.g. profiler build) bundleType: __DEV__ ? 1 : 0, version: ReactVersion, diff --git a/src/renderers/native/ReactNativeInspector.js b/src/renderers/native/ReactNativeFiberInspector.js similarity index 85% rename from src/renderers/native/ReactNativeInspector.js rename to src/renderers/native/ReactNativeFiberInspector.js index b6c6c107666f..a4f8ad258c9b 100644 --- a/src/renderers/native/ReactNativeInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -6,7 +6,7 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * - * @providesModule ReactNativeInspector + * @providesModule ReactNativeFiberInspector * @flow */ 'use strict'; @@ -17,12 +17,7 @@ if (__DEV__) { var traverseOwnerTreeUp = function (hierarchy, instance) { if (instance) { hierarchy.unshift(instance); - const owner = typeof instance.tag === 'number' ? - // Fiber - instance._debugOwner : - // Stack - instance._currentElement._owner; - traverseOwnerTreeUp(hierarchy, owner); + traverseOwnerTreeUp(hierarchy, instance._debugOwner); } }; diff --git a/src/renderers/native/ReactNativeStack.js b/src/renderers/native/ReactNativeStack.js index a9fa118cf27b..e3ca928834e9 100644 --- a/src/renderers/native/ReactNativeStack.js +++ b/src/renderers/native/ReactNativeStack.js @@ -16,6 +16,7 @@ var ReactNativeInjection = require('ReactNativeInjection'); var ReactNativeMount = require('ReactNativeMount'); var ReactNativeStackInjection = require('ReactNativeStackInjection'); var ReactUpdates = require('ReactUpdates'); +var ReactNativeStackInspector = require('ReactNativeStackInspector'); var findNodeHandle = require('findNodeHandle'); @@ -81,6 +82,7 @@ if ( }, Mount: ReactNativeMount, Reconciler: require('ReactReconciler'), + getInspectorDataForViewTag: ReactNativeStackInspector.getInspectorDataForViewTag, }); } diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js new file mode 100644 index 000000000000..f828b25722f0 --- /dev/null +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactNativeStackInspector + * @flow + */ +'use strict'; + +const ReactNativeComponentTree = require('ReactNativeComponentTree'); + +if (__DEV__) { + var traverseOwnerTreeUp = function (hierarchy, instance) { + if (instance) { + hierarchy.unshift(instance); + traverseOwnerTreeUp(hierarchy, instance._currentElement._owner); + } + }; + + var getOwnerHierarchy = function (instance) { + var hierarchy = []; + traverseOwnerTreeUp(hierarchy, instance); + return hierarchy; + }; + + var lastNotNativeInstance = function (hierarchy) { + for (let i = hierarchy.length - 1; i > 1; i--) { + const instance = hierarchy[i]; + if (!instance.viewConfig) { + return instance; + } + } + return hierarchy[0]; + }; + + var getInspectorDataForViewTag = function(viewTag: any): Object { + const component = ReactNativeComponentTree.getClosestInstanceFromNode(viewTag); + const hierarchy = getOwnerHierarchy(component); + const instance = lastNotNativeInstance(hierarchy); + const props = (instance._instance || {}).props || {}; + const source = instance._currentElement && instance._currentElement._source; + + return { + hierarchy, + instance, + props, + source, + }; + }; +} + +module.exports = { + getInspectorDataForViewTag, +}; + From 9cb93e57e2f9267b2bc2f8467754017a5bbc8bf8 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 15 May 2017 17:45:27 +0100 Subject: [PATCH 03/22] prettier run --- src/renderers/native/ReactNativeFiberInspector.js | 9 ++++----- src/renderers/native/ReactNativeStackInspector.js | 13 +++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index a4f8ad258c9b..7056748463c8 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -14,20 +14,20 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); if (__DEV__) { - var traverseOwnerTreeUp = function (hierarchy, instance) { + var traverseOwnerTreeUp = function(hierarchy, instance) { if (instance) { hierarchy.unshift(instance); traverseOwnerTreeUp(hierarchy, instance._debugOwner); } }; - var getOwnerHierarchy = function (instance) { + var getOwnerHierarchy = function(instance) { var hierarchy = []; traverseOwnerTreeUp(hierarchy, instance); return hierarchy; }; - var lastNotNativeInstance = function (hierarchy) { + var lastNotNativeInstance = function(hierarchy) { for (let i = hierarchy.length - 1; i > 1; i--) { const instance = hierarchy[i]; if (!instance.viewConfig) { @@ -43,7 +43,7 @@ if (__DEV__) { const instance = lastNotNativeInstance(hierarchy); const props = instance.memoizedProps || {}; const source = instance._debugSource; - + return { hierarchy, instance, @@ -56,4 +56,3 @@ if (__DEV__) { module.exports = { getInspectorDataForViewTag, }; - diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js index f828b25722f0..24796f1fd76d 100644 --- a/src/renderers/native/ReactNativeStackInspector.js +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -14,20 +14,20 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); if (__DEV__) { - var traverseOwnerTreeUp = function (hierarchy, instance) { + var traverseOwnerTreeUp = function(hierarchy, instance) { if (instance) { hierarchy.unshift(instance); traverseOwnerTreeUp(hierarchy, instance._currentElement._owner); } }; - var getOwnerHierarchy = function (instance) { + var getOwnerHierarchy = function(instance) { var hierarchy = []; traverseOwnerTreeUp(hierarchy, instance); return hierarchy; }; - var lastNotNativeInstance = function (hierarchy) { + var lastNotNativeInstance = function(hierarchy) { for (let i = hierarchy.length - 1; i > 1; i--) { const instance = hierarchy[i]; if (!instance.viewConfig) { @@ -38,12 +38,14 @@ if (__DEV__) { }; var getInspectorDataForViewTag = function(viewTag: any): Object { - const component = ReactNativeComponentTree.getClosestInstanceFromNode(viewTag); + const component = ReactNativeComponentTree.getClosestInstanceFromNode( + viewTag, + ); const hierarchy = getOwnerHierarchy(component); const instance = lastNotNativeInstance(hierarchy); const props = (instance._instance || {}).props || {}; const source = instance._currentElement && instance._currentElement._source; - + return { hierarchy, instance, @@ -56,4 +58,3 @@ if (__DEV__) { module.exports = { getInspectorDataForViewTag, }; - From 179926e41dad91e920d5b866c948da51c4afd022 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 16 May 2017 13:38:07 +0100 Subject: [PATCH 04/22] updated fiber implementation based on feedback --- src/renderers/native/ReactNativeComponentTree.js | 1 + src/renderers/native/ReactNativeFiberInspector.js | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/renderers/native/ReactNativeComponentTree.js b/src/renderers/native/ReactNativeComponentTree.js index e091667789dc..0cb2b021cf21 100644 --- a/src/renderers/native/ReactNativeComponentTree.js +++ b/src/renderers/native/ReactNativeComponentTree.js @@ -57,6 +57,7 @@ function uncacheFiberNode(tag) { } function getInstanceFromTag(tag) { + console.log(instanceCache) return instanceCache[tag] || null; } diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 7056748463c8..ac3fef239eeb 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -12,6 +12,7 @@ 'use strict'; const ReactNativeComponentTree = require('ReactNativeComponentTree'); +const ReactFiberTreeReflection = require('ReactFiberTreeReflection'); if (__DEV__) { var traverseOwnerTreeUp = function(hierarchy, instance) { @@ -37,11 +38,20 @@ if (__DEV__) { return hierarchy[0]; }; + const { + getClosestInstanceFromNode, + getFiberCurrentPropsFromNode, + } = ReactNativeComponentTree; + + const { + findCurrentFiberUsingSlowPath, + } = ReactFiberTreeReflection; + var getInspectorDataForViewTag = function(viewTag: any): Object { - const fiber = ReactNativeComponentTree.findFiberByHostInstance(viewTag); + const fiber = findCurrentFiberUsingSlowPath(getClosestInstanceFromNode(viewTag)); const hierarchy = getOwnerHierarchy(fiber); const instance = lastNotNativeInstance(hierarchy); - const props = instance.memoizedProps || {}; + const props = getFiberCurrentPropsFromNode(instance.stateNode) || {}; const source = instance._debugSource; return { From 6edea829a57d71b9877c5fc9545cb64288081380 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 16 May 2017 16:34:17 +0100 Subject: [PATCH 05/22] updated fiber implementation for selecting inspector hierarchy --- .../native/ReactNativeComponentTree.js | 1 - .../native/ReactNativeFiberInspector.js | 43 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/renderers/native/ReactNativeComponentTree.js b/src/renderers/native/ReactNativeComponentTree.js index 0cb2b021cf21..e091667789dc 100644 --- a/src/renderers/native/ReactNativeComponentTree.js +++ b/src/renderers/native/ReactNativeComponentTree.js @@ -57,7 +57,6 @@ function uncacheFiberNode(tag) { } function getInstanceFromTag(tag) { - console.log(instanceCache) return instanceCache[tag] || null; } diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index ac3fef239eeb..c886220b1102 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -13,6 +13,7 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); const ReactFiberTreeReflection = require('ReactFiberTreeReflection'); +const getComponentName = require('getComponentName'); if (__DEV__) { var traverseOwnerTreeUp = function(hierarchy, instance) { @@ -38,6 +39,41 @@ if (__DEV__) { return hierarchy[0]; }; + var getHostNode = function(instance, findNodeHandle) { + let hostNode = null; + let fiber = instance; + // Stateless components make this complicated. + // Look for children first. + while (fiber) { + hostNode = findNodeHandle(fiber.stateNode); + if (hostNode) { + break; + } + fiber = fiber.child; + } + // Look for parents second. + fiber = instance.return; + while (fiber) { + hostNode = findNodeHandle(fiber.stateNode); + if (hostNode) { + break; + } + fiber = fiber.return; + } + return hostNode; + }; + + var createHierarchy = function(fiberHierarchy) { + return fiberHierarchy.map((fiber) => ({ + name: getComponentName(fiber), + getInspectorData: findNodeHandle => ({ + hostNode: getHostNode(fiber, findNodeHandle), + props: fiber.stateNode ? getFiberCurrentPropsFromNode(fiber.stateNode) : {}, + source: fiber._debugSource, + }), + })); + }; + const { getClosestInstanceFromNode, getFiberCurrentPropsFromNode, @@ -49,15 +85,18 @@ if (__DEV__) { var getInspectorDataForViewTag = function(viewTag: any): Object { const fiber = findCurrentFiberUsingSlowPath(getClosestInstanceFromNode(viewTag)); - const hierarchy = getOwnerHierarchy(fiber); - const instance = lastNotNativeInstance(hierarchy); + const fiberHierarchy = getOwnerHierarchy(fiber); + const instance = lastNotNativeInstance(fiberHierarchy); + const hierarchy = createHierarchy(fiberHierarchy); const props = getFiberCurrentPropsFromNode(instance.stateNode) || {}; const source = instance._debugSource; + const selection = fiberHierarchy.indexOf(instance); return { hierarchy, instance, props, + selection, source, }; }; From 416c207e0a2eb0ce95497c496e7047004ec110f6 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 22 May 2017 10:48:50 +0100 Subject: [PATCH 06/22] run of prettier --- src/renderers/native/ReactNativeFiberInspector.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index c886220b1102..b887322b55f5 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -64,11 +64,13 @@ if (__DEV__) { }; var createHierarchy = function(fiberHierarchy) { - return fiberHierarchy.map((fiber) => ({ + return fiberHierarchy.map(fiber => ({ name: getComponentName(fiber), getInspectorData: findNodeHandle => ({ hostNode: getHostNode(fiber, findNodeHandle), - props: fiber.stateNode ? getFiberCurrentPropsFromNode(fiber.stateNode) : {}, + props: fiber.stateNode + ? getFiberCurrentPropsFromNode(fiber.stateNode) + : {}, source: fiber._debugSource, }), })); @@ -79,12 +81,12 @@ if (__DEV__) { getFiberCurrentPropsFromNode, } = ReactNativeComponentTree; - const { - findCurrentFiberUsingSlowPath, - } = ReactFiberTreeReflection; + const {findCurrentFiberUsingSlowPath} = ReactFiberTreeReflection; var getInspectorDataForViewTag = function(viewTag: any): Object { - const fiber = findCurrentFiberUsingSlowPath(getClosestInstanceFromNode(viewTag)); + const fiber = findCurrentFiberUsingSlowPath( + getClosestInstanceFromNode(viewTag), + ); const fiberHierarchy = getOwnerHierarchy(fiber); const instance = lastNotNativeInstance(fiberHierarchy); const hierarchy = createHierarchy(fiberHierarchy); From 532912aec258fad0b7f4c6851694b720292206fe Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 22 May 2017 11:07:55 +0100 Subject: [PATCH 07/22] fixed flow issues --- src/renderers/native/ReactNativeFiberInspector.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index b887322b55f5..d963ed3b96ac 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -15,15 +15,17 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); const ReactFiberTreeReflection = require('ReactFiberTreeReflection'); const getComponentName = require('getComponentName'); +import type {Fiber} from 'ReactFiber'; + if (__DEV__) { - var traverseOwnerTreeUp = function(hierarchy, instance) { + var traverseOwnerTreeUp = function(hierarchy, instance: any) { if (instance) { hierarchy.unshift(instance); traverseOwnerTreeUp(hierarchy, instance._debugOwner); } }; - var getOwnerHierarchy = function(instance) { + var getOwnerHierarchy = function(instance: any) { var hierarchy = []; traverseOwnerTreeUp(hierarchy, instance); return hierarchy; @@ -39,7 +41,7 @@ if (__DEV__) { return hierarchy[0]; }; - var getHostNode = function(instance, findNodeHandle) { + var getHostNode = function(instance: Fiber, findNodeHandle) { let hostNode = null; let fiber = instance; // Stateless components make this complicated. From 159efdb15ce691b11b65e81645499944e62a1a41 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 22 May 2017 12:24:11 +0100 Subject: [PATCH 08/22] updated stack implementation --- .../native/ReactNativeFiberInspector.js | 2 +- .../native/ReactNativeStackInspector.js | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index d963ed3b96ac..1d5b93f7fa5d 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -85,7 +85,7 @@ if (__DEV__) { const {findCurrentFiberUsingSlowPath} = ReactFiberTreeReflection; - var getInspectorDataForViewTag = function(viewTag: any): Object { + var getInspectorDataForViewTag = function(viewTag: number): Object { const fiber = findCurrentFiberUsingSlowPath( getClosestInstanceFromNode(viewTag), ); diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js index 24796f1fd76d..92d012b27a22 100644 --- a/src/renderers/native/ReactNativeStackInspector.js +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -12,6 +12,7 @@ 'use strict'; const ReactNativeComponentTree = require('ReactNativeComponentTree'); +const getComponentName = require('getComponentName'); if (__DEV__) { var traverseOwnerTreeUp = function(hierarchy, instance) { @@ -37,19 +38,33 @@ if (__DEV__) { return hierarchy[0]; }; + var createHierarchy = function(componentHierarchy) { + return componentHierarchy.map(component => ({ + name: getComponentName(component), + getInspectorData: () => ({ + hostNode: component.getHostNode(), + props: (component._instance || {}).props || {}, + source: component._currentElement && component._currentElement._source, + }), + })); + }; + var getInspectorDataForViewTag = function(viewTag: any): Object { const component = ReactNativeComponentTree.getClosestInstanceFromNode( viewTag, ); - const hierarchy = getOwnerHierarchy(component); - const instance = lastNotNativeInstance(hierarchy); + const componentHierarchy = getOwnerHierarchy(component); + const instance = lastNotNativeInstance(componentHierarchy); + const hierarchy = createHierarchy(componentHierarchy); const props = (instance._instance || {}).props || {}; const source = instance._currentElement && instance._currentElement._source; + const selection = componentHierarchy.indexOf(instance); return { hierarchy, instance, props, + selection, source, }; }; From 3a3d8c34e1292b34bbf85d6fde6d6c7d945198d1 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 22 May 2017 16:44:02 +0100 Subject: [PATCH 09/22] fixes an implementation difference where before it wasnt properly understoof --- .../native/ReactNativeFiberInspector.js | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 1d5b93f7fa5d..90baf9856987 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -41,28 +41,18 @@ if (__DEV__) { return hierarchy[0]; }; - var getHostNode = function(instance: Fiber, findNodeHandle) { - let hostNode = null; - let fiber = instance; - // Stateless components make this complicated. - // Look for children first. + var getHostNode = function(fiber: Fiber, findNodeHandle) { + let hostNode; + // look for children first for the hostNode + // as composite fibers do not have a hostNode while (fiber) { hostNode = findNodeHandle(fiber.stateNode); if (hostNode) { - break; + return hostNode; } fiber = fiber.child; } - // Look for parents second. - fiber = instance.return; - while (fiber) { - hostNode = findNodeHandle(fiber.stateNode); - if (hostNode) { - break; - } - fiber = fiber.return; - } - return hostNode; + return null; }; var createHierarchy = function(fiberHierarchy) { From ba9c3fab5e85922f1e3221a9a363f629714de30f Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 13:29:20 +0100 Subject: [PATCH 10/22] addresses comments in PR feedback --- .../native/ReactNativeFiberInspector.js | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 90baf9856987..ed12f8c376a2 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -14,6 +14,7 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); const ReactFiberTreeReflection = require('ReactFiberTreeReflection'); const getComponentName = require('getComponentName'); +const emptyObject = require('fbjs/lib/emptyObject'); import type {Fiber} from 'ReactFiber'; @@ -34,19 +35,27 @@ if (__DEV__) { var lastNotNativeInstance = function(hierarchy) { for (let i = hierarchy.length - 1; i > 1; i--) { const instance = hierarchy[i]; - if (!instance.viewConfig) { + const stateNode = instance.stateNode; + + if (!stateNode.viewConfig) { return instance; } } return hierarchy[0]; }; + var getHostProps = function(fiber) { + return ReactFiberTreeReflection.findCurrentHostFiber(fiber).memoizedProps; + }; + var getHostNode = function(fiber: Fiber, findNodeHandle) { let hostNode; // look for children first for the hostNode // as composite fibers do not have a hostNode while (fiber) { - hostNode = findNodeHandle(fiber.stateNode); + if (fiber.stateNode !== null) { + hostNode = findNodeHandle(fiber.stateNode); + } if (hostNode) { return hostNode; } @@ -60,18 +69,13 @@ if (__DEV__) { name: getComponentName(fiber), getInspectorData: findNodeHandle => ({ hostNode: getHostNode(fiber, findNodeHandle), - props: fiber.stateNode - ? getFiberCurrentPropsFromNode(fiber.stateNode) - : {}, + props: fiber.stateNode ? getHostProps(fiber) : emptyObject, source: fiber._debugSource, }), })); }; - const { - getClosestInstanceFromNode, - getFiberCurrentPropsFromNode, - } = ReactNativeComponentTree; + const {getClosestInstanceFromNode} = ReactNativeComponentTree; const {findCurrentFiberUsingSlowPath} = ReactFiberTreeReflection; @@ -82,7 +86,7 @@ if (__DEV__) { const fiberHierarchy = getOwnerHierarchy(fiber); const instance = lastNotNativeInstance(fiberHierarchy); const hierarchy = createHierarchy(fiberHierarchy); - const props = getFiberCurrentPropsFromNode(instance.stateNode) || {}; + const props = getHostProps(instance) || emptyObject; const source = instance._debugSource; const selection = fiberHierarchy.indexOf(instance); From 28bef25a5dc8518e26de9ceea1ee3d4297feb9e5 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 13:30:57 +0100 Subject: [PATCH 11/22] updated stack inspector to use emptyObject --- src/renderers/native/ReactNativeStackInspector.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js index 92d012b27a22..ffe6505db4e3 100644 --- a/src/renderers/native/ReactNativeStackInspector.js +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -13,6 +13,7 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); const getComponentName = require('getComponentName'); +const emptyObject = require('fbjs/lib/emptyObject'); if (__DEV__) { var traverseOwnerTreeUp = function(hierarchy, instance) { @@ -43,7 +44,7 @@ if (__DEV__) { name: getComponentName(component), getInspectorData: () => ({ hostNode: component.getHostNode(), - props: (component._instance || {}).props || {}, + props: (component._instance || emptyObject).props || emptyObject, source: component._currentElement && component._currentElement._source, }), })); @@ -56,7 +57,7 @@ if (__DEV__) { const componentHierarchy = getOwnerHierarchy(component); const instance = lastNotNativeInstance(componentHierarchy); const hierarchy = createHierarchy(componentHierarchy); - const props = (instance._instance || {}).props || {}; + const props = (instance._instance || emptyObject).props || emptyObject; const source = instance._currentElement && instance._currentElement._source; const selection = componentHierarchy.indexOf(instance); From 0f88fa53d59c384740627adf0490e8100a2c3f1e Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 14:12:47 +0100 Subject: [PATCH 12/22] Update ReactNativeFiberInspector.js Fixes a flow error --- src/renderers/native/ReactNativeFiberInspector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index ed12f8c376a2..fa82b84fd351 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -45,7 +45,7 @@ if (__DEV__) { }; var getHostProps = function(fiber) { - return ReactFiberTreeReflection.findCurrentHostFiber(fiber).memoizedProps; + return (ReactFiberTreeReflection.findCurrentHostFiber(fiber) || emptyObject).memoizedProps; }; var getHostNode = function(fiber: Fiber, findNodeHandle) { From 450da6084b6aa406987480fec096829101e3d371 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 14:38:56 +0100 Subject: [PATCH 13/22] fixes last flow error --- src/renderers/native/ReactNativeFiberInspector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index fa82b84fd351..a6c017dc5fd0 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -48,7 +48,7 @@ if (__DEV__) { return (ReactFiberTreeReflection.findCurrentHostFiber(fiber) || emptyObject).memoizedProps; }; - var getHostNode = function(fiber: Fiber, findNodeHandle) { + var getHostNode = function(fiber: Fiber | null, findNodeHandle) { let hostNode; // look for children first for the hostNode // as composite fibers do not have a hostNode From 6215bf91c8c4531bf8b30c9f3146392e18a45471 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 14:50:39 +0100 Subject: [PATCH 14/22] prettier --- src/renderers/native/ReactNativeFiberInspector.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index a6c017dc5fd0..1cbfc1307417 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -45,7 +45,8 @@ if (__DEV__) { }; var getHostProps = function(fiber) { - return (ReactFiberTreeReflection.findCurrentHostFiber(fiber) || emptyObject).memoizedProps; + return (ReactFiberTreeReflection.findCurrentHostFiber(fiber) || emptyObject) + .memoizedProps; }; var getHostNode = function(fiber: Fiber | null, findNodeHandle) { From d378e9c24850de09b639ad33f5deb699fade50ce Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 17:38:29 +0100 Subject: [PATCH 15/22] applied changes to how viewConfig works for fibers and extracted measure out --- .../native/ReactNativeFiberInspector.js | 38 +++++++++++++++---- .../native/ReactNativeStackInspector.js | 6 ++- src/shared/utils/getComponentName.js | 6 ++- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 1cbfc1307417..78b87e196dd9 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -15,6 +15,13 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); const ReactFiberTreeReflection = require('ReactFiberTreeReflection'); const getComponentName = require('getComponentName'); const emptyObject = require('fbjs/lib/emptyObject'); +const ReactTypeOfWork = require('ReactTypeOfWork'); +const UIManager = require('UIManager'); + +const { + HostComponent, + ClassComponent, +} = ReactTypeOfWork; import type {Fiber} from 'ReactFiber'; @@ -32,12 +39,24 @@ if (__DEV__) { return hierarchy; }; - var lastNotNativeInstance = function(hierarchy) { + var isHostLikeInstance = function(fiber) { + const stateNode = fiber.stateNode; + const tag = fiber.tag; + + if (tag !== HostComponent && tag !== ClassComponent) { + return false; + } + if (!stateNode.viewConfig) { + return false; + } + return true; + } + + var lastNonHostInstance = function(hierarchy) { for (let i = hierarchy.length - 1; i > 1; i--) { const instance = hierarchy[i]; - const stateNode = instance.stateNode; - if (!stateNode.viewConfig) { + if (!isHostLikeInstance(instance)) { return instance; } } @@ -54,7 +73,7 @@ if (__DEV__) { // look for children first for the hostNode // as composite fibers do not have a hostNode while (fiber) { - if (fiber.stateNode !== null) { + if (fiber.stateNode !== null && fiber.tag === HostComponent) { hostNode = findNodeHandle(fiber.stateNode); } if (hostNode) { @@ -65,11 +84,16 @@ if (__DEV__) { return null; }; + var stripTopSecret = str => str.replace('topsecret-', ''); + var createHierarchy = function(fiberHierarchy) { return fiberHierarchy.map(fiber => ({ - name: getComponentName(fiber), + name: stripTopSecret(getComponentName(fiber)), getInspectorData: findNodeHandle => ({ - hostNode: getHostNode(fiber, findNodeHandle), + measure: callback => UIManager.measure( + getHostNode(fiber, findNodeHandle), + callback + ), props: fiber.stateNode ? getHostProps(fiber) : emptyObject, source: fiber._debugSource, }), @@ -85,7 +109,7 @@ if (__DEV__) { getClosestInstanceFromNode(viewTag), ); const fiberHierarchy = getOwnerHierarchy(fiber); - const instance = lastNotNativeInstance(fiberHierarchy); + const instance = lastNonHostInstance(fiberHierarchy); const hierarchy = createHierarchy(fiberHierarchy); const props = getHostProps(instance) || emptyObject; const source = instance._debugSource; diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js index ffe6505db4e3..51b40f506f2c 100644 --- a/src/renderers/native/ReactNativeStackInspector.js +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -14,6 +14,7 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); const getComponentName = require('getComponentName'); const emptyObject = require('fbjs/lib/emptyObject'); +const UIManager = require('UIManager'); if (__DEV__) { var traverseOwnerTreeUp = function(hierarchy, instance) { @@ -43,7 +44,10 @@ if (__DEV__) { return componentHierarchy.map(component => ({ name: getComponentName(component), getInspectorData: () => ({ - hostNode: component.getHostNode(), + measure: callback => UIManager.measure( + component.getHostNode(), + callback + ), props: (component._instance || emptyObject).props || emptyObject, source: component._currentElement && component._currentElement._source, }), diff --git a/src/shared/utils/getComponentName.js b/src/shared/utils/getComponentName.js index b5360efc230b..e62f6d902777 100644 --- a/src/shared/utils/getComponentName.js +++ b/src/shared/utils/getComponentName.js @@ -34,7 +34,11 @@ function getComponentName( return type.displayName || type.name; } } - return null; + // this is needed for RN components + if (instanceOrFiber.constructor && instanceOrFiber.constructor.displayName) { + return instanceOrFiber.constructor.displayName; + } + return 'null'; } module.exports = getComponentName; From 76b721a71103f485fdcf38002199a11506ffdf29 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 17:40:27 +0100 Subject: [PATCH 16/22] fixed bad paste --- .../native/ReactNativeFiberInspector.js | 26 +++---------------- .../native/ReactNativeStackInspector.js | 6 ++--- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 78b87e196dd9..e902c51c0136 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -18,10 +18,7 @@ const emptyObject = require('fbjs/lib/emptyObject'); const ReactTypeOfWork = require('ReactTypeOfWork'); const UIManager = require('UIManager'); -const { - HostComponent, - ClassComponent, -} = ReactTypeOfWork; +const {HostComponent} = ReactTypeOfWork; import type {Fiber} from 'ReactFiber'; @@ -39,24 +36,11 @@ if (__DEV__) { return hierarchy; }; - var isHostLikeInstance = function(fiber) { - const stateNode = fiber.stateNode; - const tag = fiber.tag; - - if (tag !== HostComponent && tag !== ClassComponent) { - return false; - } - if (!stateNode.viewConfig) { - return false; - } - return true; - } - var lastNonHostInstance = function(hierarchy) { for (let i = hierarchy.length - 1; i > 1; i--) { const instance = hierarchy[i]; - if (!isHostLikeInstance(instance)) { + if (instance.tag !== HostComponent) { return instance; } } @@ -90,10 +74,8 @@ if (__DEV__) { return fiberHierarchy.map(fiber => ({ name: stripTopSecret(getComponentName(fiber)), getInspectorData: findNodeHandle => ({ - measure: callback => UIManager.measure( - getHostNode(fiber, findNodeHandle), - callback - ), + measure: callback => + UIManager.measure(getHostNode(fiber, findNodeHandle), callback), props: fiber.stateNode ? getHostProps(fiber) : emptyObject, source: fiber._debugSource, }), diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js index 51b40f506f2c..a5e7d3ce6da9 100644 --- a/src/renderers/native/ReactNativeStackInspector.js +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -44,10 +44,8 @@ if (__DEV__) { return componentHierarchy.map(component => ({ name: getComponentName(component), getInspectorData: () => ({ - measure: callback => UIManager.measure( - component.getHostNode(), - callback - ), + measure: callback => + UIManager.measure(component.getHostNode(), callback), props: (component._instance || emptyObject).props || emptyObject, source: component._currentElement && component._currentElement._source, }), From 4ce3fc87ca16b38e8fb573a578d1e4d1add1fb8d Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 17:56:21 +0100 Subject: [PATCH 17/22] fixes flow errors --- src/renderers/native/ReactNativeFiberInspector.js | 2 +- src/shared/utils/getComponentName.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index e902c51c0136..951b5fb4791a 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -68,7 +68,7 @@ if (__DEV__) { return null; }; - var stripTopSecret = str => str.replace('topsecret-', ''); + var stripTopSecret = str => typeof str === 'string' && str.replace('topsecret-', ''); var createHierarchy = function(fiberHierarchy) { return fiberHierarchy.map(fiber => ({ diff --git a/src/shared/utils/getComponentName.js b/src/shared/utils/getComponentName.js index e62f6d902777..e7488e7e0479 100644 --- a/src/shared/utils/getComponentName.js +++ b/src/shared/utils/getComponentName.js @@ -35,7 +35,7 @@ function getComponentName( } } // this is needed for RN components - if (instanceOrFiber.constructor && instanceOrFiber.constructor.displayName) { + if ((instanceOrFiber: any).constructor && instanceOrFiber.constructor.displayName) { return instanceOrFiber.constructor.displayName; } return 'null'; From fc0c137de9c876d6e80a2f01fa4621cef7048dc2 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 18:06:29 +0100 Subject: [PATCH 18/22] prettyify --- src/renderers/native/ReactNativeFiberInspector.js | 3 ++- src/shared/utils/getComponentName.js | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 951b5fb4791a..08e78446df5a 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -68,7 +68,8 @@ if (__DEV__) { return null; }; - var stripTopSecret = str => typeof str === 'string' && str.replace('topsecret-', ''); + var stripTopSecret = str => + typeof str === 'string' && str.replace('topsecret-', ''); var createHierarchy = function(fiberHierarchy) { return fiberHierarchy.map(fiber => ({ diff --git a/src/shared/utils/getComponentName.js b/src/shared/utils/getComponentName.js index e7488e7e0479..bb74377cd583 100644 --- a/src/shared/utils/getComponentName.js +++ b/src/shared/utils/getComponentName.js @@ -35,7 +35,10 @@ function getComponentName( } } // this is needed for RN components - if ((instanceOrFiber: any).constructor && instanceOrFiber.constructor.displayName) { + if ( + (instanceOrFiber: any).constructor && + instanceOrFiber.constructor.displayName + ) { return instanceOrFiber.constructor.displayName; } return 'null'; From a6f8e6becee4ad5a1486048a85abc60ee38f1be9 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 18:17:38 +0100 Subject: [PATCH 19/22] revmoed getComponentName changes --- src/shared/utils/getComponentName.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/shared/utils/getComponentName.js b/src/shared/utils/getComponentName.js index bb74377cd583..939b2ec1559c 100644 --- a/src/shared/utils/getComponentName.js +++ b/src/shared/utils/getComponentName.js @@ -34,13 +34,6 @@ function getComponentName( return type.displayName || type.name; } } - // this is needed for RN components - if ( - (instanceOrFiber: any).constructor && - instanceOrFiber.constructor.displayName - ) { - return instanceOrFiber.constructor.displayName; - } return 'null'; } From 3e84ec9bbefc6d241bb71bca2f0b5d6fad256d51 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 18:42:52 +0100 Subject: [PATCH 20/22] updated logic for getHostProps and addressed other PR feedback --- .../native/ReactNativeFiberInspector.js | 27 ++++++++++++------- .../native/ReactNativeStackInspector.js | 19 +++++++++++-- src/shared/utils/getComponentName.js | 2 +- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 08e78446df5a..804c2b5abb86 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -18,10 +18,14 @@ const emptyObject = require('fbjs/lib/emptyObject'); const ReactTypeOfWork = require('ReactTypeOfWork'); const UIManager = require('UIManager'); +const {getClosestInstanceFromNode} = ReactNativeComponentTree; +const {findCurrentFiberUsingSlowPath} = ReactFiberTreeReflection; const {HostComponent} = ReactTypeOfWork; import type {Fiber} from 'ReactFiber'; +let getInspectorDataForViewTag; + if (__DEV__) { var traverseOwnerTreeUp = function(hierarchy, instance: any) { if (instance) { @@ -48,8 +52,11 @@ if (__DEV__) { }; var getHostProps = function(fiber) { - return (ReactFiberTreeReflection.findCurrentHostFiber(fiber) || emptyObject) - .memoizedProps; + const host = ReactFiberTreeReflection.findCurrentHostFiber(fiber); + if (host) { + return host.memoizedProps || emptyObject; + } + return emptyObject; }; var getHostNode = function(fiber: Fiber | null, findNodeHandle) { @@ -77,24 +84,20 @@ if (__DEV__) { getInspectorData: findNodeHandle => ({ measure: callback => UIManager.measure(getHostNode(fiber, findNodeHandle), callback), - props: fiber.stateNode ? getHostProps(fiber) : emptyObject, + props: getHostProps(fiber), source: fiber._debugSource, }), })); }; - const {getClosestInstanceFromNode} = ReactNativeComponentTree; - - const {findCurrentFiberUsingSlowPath} = ReactFiberTreeReflection; - - var getInspectorDataForViewTag = function(viewTag: number): Object { + getInspectorDataForViewTag = function(viewTag: number): Object { const fiber = findCurrentFiberUsingSlowPath( getClosestInstanceFromNode(viewTag), ); const fiberHierarchy = getOwnerHierarchy(fiber); const instance = lastNonHostInstance(fiberHierarchy); const hierarchy = createHierarchy(fiberHierarchy); - const props = getHostProps(instance) || emptyObject; + const props = getHostProps(instance); const source = instance._debugSource; const selection = fiberHierarchy.indexOf(instance); @@ -106,6 +109,12 @@ if (__DEV__) { source, }; }; +} else { + getInspectorDataForViewTag = () => { + throw new Error( + 'getInspectorDataForViewTag() is not available in production', + ); + }; } module.exports = { diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js index a5e7d3ce6da9..66cce2bcfc73 100644 --- a/src/renderers/native/ReactNativeStackInspector.js +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -16,6 +16,8 @@ const getComponentName = require('getComponentName'); const emptyObject = require('fbjs/lib/emptyObject'); const UIManager = require('UIManager'); +let getInspectorDataForViewTag; + if (__DEV__) { var traverseOwnerTreeUp = function(hierarchy, instance) { if (instance) { @@ -40,6 +42,13 @@ if (__DEV__) { return hierarchy[0]; }; + var getHostProps = function(component) { + const instance = component._instance; + if (instance) { + return instance.props; + } + }; + var createHierarchy = function(componentHierarchy) { return componentHierarchy.map(component => ({ name: getComponentName(component), @@ -52,14 +61,14 @@ if (__DEV__) { })); }; - var getInspectorDataForViewTag = function(viewTag: any): Object { + getInspectorDataForViewTag = function(viewTag: any): Object { const component = ReactNativeComponentTree.getClosestInstanceFromNode( viewTag, ); const componentHierarchy = getOwnerHierarchy(component); const instance = lastNotNativeInstance(componentHierarchy); const hierarchy = createHierarchy(componentHierarchy); - const props = (instance._instance || emptyObject).props || emptyObject; + const props = getHostProps(instance) || emptyObject; const source = instance._currentElement && instance._currentElement._source; const selection = componentHierarchy.indexOf(instance); @@ -71,6 +80,12 @@ if (__DEV__) { source, }; }; +} else { + getInspectorDataForViewTag = () => { + throw new Error( + 'getInspectorDataForViewTag() is not available in production', + ); + }; } module.exports = { diff --git a/src/shared/utils/getComponentName.js b/src/shared/utils/getComponentName.js index 939b2ec1559c..b5360efc230b 100644 --- a/src/shared/utils/getComponentName.js +++ b/src/shared/utils/getComponentName.js @@ -34,7 +34,7 @@ function getComponentName( return type.displayName || type.name; } } - return 'null'; + return null; } module.exports = getComponentName; From e0e443e72e362ea7e0ae3b3940a25bdd5dbc7f75 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 18:46:46 +0100 Subject: [PATCH 21/22] improved throwing to invariant and update stack implemenation based on feedback --- src/renderers/native/ReactNativeFiberInspector.js | 6 ++++-- src/renderers/native/ReactNativeStackInspector.js | 11 +++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 804c2b5abb86..2238c38e5c20 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -17,6 +17,7 @@ const getComponentName = require('getComponentName'); const emptyObject = require('fbjs/lib/emptyObject'); const ReactTypeOfWork = require('ReactTypeOfWork'); const UIManager = require('UIManager'); +const invariant = require('fbjs/lib/invariant'); const {getClosestInstanceFromNode} = ReactNativeComponentTree; const {findCurrentFiberUsingSlowPath} = ReactFiberTreeReflection; @@ -111,9 +112,10 @@ if (__DEV__) { }; } else { getInspectorDataForViewTag = () => { - throw new Error( + invariant( + false, 'getInspectorDataForViewTag() is not available in production', - ); + ); }; } diff --git a/src/renderers/native/ReactNativeStackInspector.js b/src/renderers/native/ReactNativeStackInspector.js index 66cce2bcfc73..42ecbf7d5398 100644 --- a/src/renderers/native/ReactNativeStackInspector.js +++ b/src/renderers/native/ReactNativeStackInspector.js @@ -15,6 +15,7 @@ const ReactNativeComponentTree = require('ReactNativeComponentTree'); const getComponentName = require('getComponentName'); const emptyObject = require('fbjs/lib/emptyObject'); const UIManager = require('UIManager'); +const invariant = require('fbjs/lib/invariant'); let getInspectorDataForViewTag; @@ -45,8 +46,9 @@ if (__DEV__) { var getHostProps = function(component) { const instance = component._instance; if (instance) { - return instance.props; + return instance.props || emptyObject; } + return emptyObject; }; var createHierarchy = function(componentHierarchy) { @@ -55,7 +57,7 @@ if (__DEV__) { getInspectorData: () => ({ measure: callback => UIManager.measure(component.getHostNode(), callback), - props: (component._instance || emptyObject).props || emptyObject, + props: getHostProps(component), source: component._currentElement && component._currentElement._source, }), })); @@ -68,7 +70,7 @@ if (__DEV__) { const componentHierarchy = getOwnerHierarchy(component); const instance = lastNotNativeInstance(componentHierarchy); const hierarchy = createHierarchy(componentHierarchy); - const props = getHostProps(instance) || emptyObject; + const props = getHostProps(instance); const source = instance._currentElement && instance._currentElement._source; const selection = componentHierarchy.indexOf(instance); @@ -82,7 +84,8 @@ if (__DEV__) { }; } else { getInspectorDataForViewTag = () => { - throw new Error( + invariant( + false, 'getInspectorDataForViewTag() is not available in production', ); }; From b05ef94f1339dfeff0081ee010d0701352749fa8 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Tue, 23 May 2017 20:10:59 +0100 Subject: [PATCH 22/22] prettier --- src/renderers/native/ReactNativeFiberInspector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/native/ReactNativeFiberInspector.js b/src/renderers/native/ReactNativeFiberInspector.js index 2238c38e5c20..f4ad2b6e2e2c 100644 --- a/src/renderers/native/ReactNativeFiberInspector.js +++ b/src/renderers/native/ReactNativeFiberInspector.js @@ -115,7 +115,7 @@ if (__DEV__) { invariant( false, 'getInspectorDataForViewTag() is not available in production', - ); + ); }; }