From 1b4776f31974ff6ad256380deacb4e2f69703750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Wed, 19 Jul 2017 10:36:57 -0700 Subject: [PATCH] In SSR, Store Elements of Composites on the Stack for Warnings (#10187) In DEV mode this keeps an additional sub-stack within the stack of the server renderer. This substack keeps track of all the elements that we resolved to get to to the next processing frame. This lets us recreate the full composite component stack for warnings. Normally the stack only contains host components. We reset this every time we're going to resolve another sibling. --- .../shared/server/ReactPartialRenderer.js | 63 ++++++++++++------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/src/renderers/shared/server/ReactPartialRenderer.js b/src/renderers/shared/server/ReactPartialRenderer.js index ffd1e40823..ecb3b0600d 100644 --- a/src/renderers/shared/server/ReactPartialRenderer.js +++ b/src/renderers/shared/server/ReactPartialRenderer.js @@ -47,17 +47,7 @@ if (__DEV__) { }; var describeComponentFrame = require('describeComponentFrame'); - var describeStackFrame = function( - frame: { - tag?: string, - children: Array<*>, - childIndex: number, - }, - ): string { - var element = frame.children[frame.childIndex - 1]; - if (!element) { - return ''; - } + var describeStackFrame = function(element): string { var source = element._source; var type = element.type; var name = typeof type === 'string' @@ -69,11 +59,21 @@ if (__DEV__) { var {ReactDebugCurrentFrame} = require('ReactGlobalSharedState'); var currentDebugStack = null; + var currentDebugElementStack = null; var setCurrentDebugStack = function(stack) { + currentDebugElementStack = stack[stack.length - 1].debugElementStack; + // We are about to enter a new composite stack, reset the array. + currentDebugElementStack.length = 0; currentDebugStack = stack; ReactDebugCurrentFrame.getCurrentStack = getStackAddendum; }; + var pushElementToDebugStack = function(element) { + if (currentDebugElementStack !== null) { + currentDebugElementStack.push(element); + } + }; var resetCurrentDebugStack = function() { + currentDebugElementStack = null; currentDebugStack = null; ReactDebugCurrentFrame.getCurrentStack = null; }; @@ -84,7 +84,10 @@ if (__DEV__) { let stack = ''; let debugStack = currentDebugStack; for (let i = debugStack.length - 1; i >= 0; i--) { - stack += describeStackFrame(debugStack[i]); + let debugElementStack = debugStack[i].debugElementStack; + for (let ii = debugElementStack.length - 1; ii >= 0; ii--) { + stack += describeStackFrame(debugElementStack[ii]); + } } return stack; }; @@ -306,10 +309,15 @@ function createOpenTagMarkup( function resolve(child, context) { // TODO: We'll need to support Arrays (and strings) after Fiber is rolled out invariant(!Array.isArray(child), 'Did not expect to receive an Array child'); - while (React.isValidElement(child) && typeof child.type === 'function') { + while (React.isValidElement(child)) { + if (__DEV__) { + pushElementToDebugStack(child); + } var Component = child.type; + if (typeof Component !== 'function') { + break; + } var publicContext = processContext(Component, context); - var inst; var queue = []; var replace = false; @@ -407,14 +415,16 @@ function resolve(child, context) { class ReactDOMServerRenderer { constructor(element, makeStaticMarkup) { - this.stack = [ - { - children: [element], - childIndex: 0, - context: emptyObject, - footer: '', - }, - ]; + var topFrame = { + children: [element], + childIndex: 0, + context: emptyObject, + footer: '', + }; + if (__DEV__) { + topFrame.debugElementStack = []; + } + this.stack = [topFrame]; this.exhausted = false; this.currentSelectValue = null; this.previousWasTextNode = false; @@ -448,6 +458,7 @@ class ReactDOMServerRenderer { } out += this.render(child, frame.context); if (__DEV__) { + // TODO: Handle reentrant server render calls. This doesn't. resetCurrentDebugStack(); } } @@ -747,13 +758,17 @@ class ReactDOMServerRenderer { } else { children = toArray(props.children); } - this.stack.push({ + var frame = { tag, children, childIndex: 0, context: context, footer: footer, - }); + }; + if (__DEV__) { + frame.debugElementStack = []; + } + this.stack.push(frame); return out; } }