From 566259567773a0af56e6c19da615e6802d73f834 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sat, 7 Jul 2018 01:09:41 +0100 Subject: [PATCH] Refactor stack handling (no functional changes) (#13165) * Refactor ReactDebugCurrentFiber to use named exports This makes the difference between it and ReactFiberCurrentFrame a bit clearer. ReactDebugCurrentFiber is Fiber's own implementation. ReactFiberCurrentFrame is the thing that holds a reference to the current implementation and delegates to it. * Unify ReactFiberComponentTreeHook and ReactDebugCurrentFiber Conceptually they're very related. ReactFiberComponentTreeHook contains implementation details of reading Fiber's stack (both in DEV and PROD). ReactDebugCurrentFiber contained a reference to the current fiber, and used the above utility. It was confusing when to use which one. Colocating them makes it clearer what you could do with each method. In the future, the plan is to stop using these methods explicitly in most places, and instead delegate to a warning system that includes stacks automatically. This change makes future refactorings simpler by colocating related logic. * Rename methods to better reflect their meanings Clarify which are DEV or PROD-only. Clarify which can return null. I believe the "work in progress only" was a mistake. I introduced it because I wasn't sure what guarantees we have around .return. But we know for sure that following a .return chain gives us an accurate stack even if we get into WIP trees because we don't have reparenting. So it's fine to relax that naming. * Rename ReactDebugCurrentFiber -> ReactCurrentFiber It's not completely DEV-only anymore. Individual methods already specify whether they work in DEV or PROD in their names. --- .../src/client/ReactDOMFiberComponent.js | 39 ++++--- .../src/client/ReactDOMFiberInput.js | 19 ++-- .../src/client/ReactDOMFiberSelect.js | 14 ++- .../src/client/ReactDOMFiberTextarea.js | 5 +- .../src/client/validateDOMNesting.js | 5 +- .../src/ReactNativeRenderer.js | 6 +- .../src/ReactCapturedValue.js | 4 +- .../react-reconciler/src/ReactChildFiber.js | 24 ++--- .../react-reconciler/src/ReactCurrentFiber.js | 101 ++++++++++++++++++ .../src/ReactDebugCurrentFiber.js | 71 ------------ .../src/ReactFiberBeginWork.js | 26 +++-- .../src/ReactFiberCommitWork.js | 12 +-- .../react-reconciler/src/ReactFiberContext.js | 10 +- .../src/ReactFiberReconciler.js | 8 +- .../src/ReactFiberScheduler.js | 21 ++-- .../src/ReactStrictModeWarnings.js | 7 +- .../shared/ReactFiberComponentTreeHook.js | 54 ---------- 17 files changed, 201 insertions(+), 225 deletions(-) create mode 100644 packages/react-reconciler/src/ReactCurrentFiber.js delete mode 100644 packages/react-reconciler/src/ReactDebugCurrentFiber.js delete mode 100644 packages/shared/ReactFiberComponentTreeHook.js diff --git a/packages/react-dom/src/client/ReactDOMFiberComponent.js b/packages/react-dom/src/client/ReactDOMFiberComponent.js index c7ec03ed12..2839a92fb4 100644 --- a/packages/react-dom/src/client/ReactDOMFiberComponent.js +++ b/packages/react-dom/src/client/ReactDOMFiberComponent.js @@ -8,7 +8,10 @@ */ // TODO: direct imports like some-package/src/* are bad. Fix me. -import ReactDebugCurrentFiber from 'react-reconciler/src/ReactDebugCurrentFiber'; +import { + getCurrentFiberOwnerNameInDevOrNull, + getCurrentFiberStackInDevOrNull, +} from 'react-reconciler/src/ReactCurrentFiber'; import {registrationNameModules} from 'events/EventPluginRegistry'; import warning from 'shared/warning'; @@ -45,10 +48,6 @@ import {validateProperties as validateARIAProperties} from '../shared/ReactDOMIn import {validateProperties as validateInputProperties} from '../shared/ReactDOMNullInputValuePropHook'; import {validateProperties as validateUnknownProperties} from '../shared/ReactDOMUnknownPropertyHook'; -const { - getCurrentFiberOwnerName, - getCurrentFiberStackAddendum, -} = ReactDebugCurrentFiber; let didWarnInvalidHydration = false; let didWarnShadyDOM = false; @@ -62,7 +61,7 @@ const HTML = '__html'; const {html: HTML_NAMESPACE} = Namespaces; -let getStack = () => ''; +let getStackInDevOrNull = () => ''; let warnedUnknownTags; let suppressHydrationWarning; @@ -77,7 +76,7 @@ let normalizeMarkupForTextOrAttribute; let normalizeHTML; if (__DEV__) { - getStack = getCurrentFiberStackAddendum; + getStackInDevOrNull = getCurrentFiberStackInDevOrNull; warnedUnknownTags = { // Chrome is the only major browser not shipping