@samitouri / QOS-React / commits / e42842f2cd

getCurrentOwnerName

Instead of passing around the owner and the internal fiber everywhere we can set current owner during each commit. That way we have it available globally where ever we need it. That way we don't have to pass it as a DEV only argument nor expose the internal representation to the host config. This doesn't actually get the current owner yet. Will do that in a follow up.

Sebastian Markbage committed Nov 17, 2016 at 14:40 UTC e42842f2cd39598d906d922ad7a12070a9900247
9 files changed +65 -55
src/renderers/dom/fiber/ReactDOMFiberComponent.js
+16 -23
@@ -30,6 +30,7 @@ var ReactDOMFiberTextarea = require('ReactDOMFiberTextarea');
30
31 var emptyFunction = require('emptyFunction');
32 var focusNode = require('focusNode');
33 +var getCurrentOwnerName = require('getCurrentOwnerName');
34 var invariant = require('invariant');
35 var isEventSupported = require('isEventSupported');
36 var setInnerHTML = require('setInnerHTML');
@@ -52,15 +53,10 @@ var HTML = '__html';
53 var DOC_FRAGMENT_TYPE = 11;
54
55
55 -function getDeclarationErrorAddendum(internalInstance) {
56 - if (internalInstance) {
57 - var owner = internalInstance._currentElement._owner || null;
58 - if (owner) {
59 - var name = owner.getName();
60 - if (name) {
61 - return ' This DOM node was rendered by `' + name + '`.';
62 - }
63 - }
56 +function getDeclarationErrorAddendum() {
57 + var ownerName = getCurrentOwnerName();
58 + if (ownerName) {
59 + return ' This DOM node was rendered by `' + ownerName + '`.';
60 }
61 return '';
62 }
@@ -76,10 +72,7 @@ function assertValidProps(component : Fiber, props : ?Object) {
72 '%s is a void element tag and must neither have `children` nor ' +
73 'use `dangerouslySetInnerHTML`.%s',
74 component._tag,
79 - component._currentElement._owner ?
80 - ' Check the render method of ' +
81 - component._currentElement._owner.getName() + '.' :
82 - ''
75 + getDeclarationErrorAddendum()
76 );
77 }
78 if (props.dangerouslySetInnerHTML != null) {
@@ -568,16 +561,16 @@ var ReactDOMFiberComponent = {
561 );
562 }
563 var isCustomComponentTag = isCustomComponent(workInProgress._tag, props);
571 - if (__DEV__ && isCustomComponentTag && !didWarnShadyDOM && el.shadyRoot) {
572 - var owner = workInProgress._currentElement._owner;
573 - var name = owner && owner.getName() || 'A component';
574 - warning(
575 - false,
576 - '%s is using shady DOM. Using shady DOM with React can ' +
577 - 'cause things to break subtly.',
578 - name
579 - );
580 - didWarnShadyDOM = true;
564 + if (__DEV__) {
565 + if (isCustomComponentTag && !didWarnShadyDOM && el.shadyRoot) {
566 + warning(
567 + false,
568 + '%s is using shady DOM. Using shady DOM with React can ' +
569 + 'cause things to break subtly.',
570 + getCurrentOwnerName() || 'A component'
571 + );
572 + didWarnShadyDOM = true;
573 + }
574 }
575 ReactDOMComponentTree.precacheFiberNode(workInProgress, el);
576 updateDOMProperties(workInProgress, null, props, isCustomComponentTag);
src/renderers/dom/fiber/wrappers/ReactDOMFiberInput.js
+5 -7
@@ -18,6 +18,7 @@ var DOMPropertyOperations = require('DOMPropertyOperations');
18 var ReactControlledValuePropTypes = require('ReactControlledValuePropTypes');
19 var ReactDOMComponentTree = require('ReactDOMComponentTree');
20
21 +var getCurrentOwnerName = require('getCurrentOwnerName');
22 var invariant = require('invariant');
23 var warning = require('warning');
24
@@ -78,11 +79,9 @@ var ReactDOMInput = {
79 ReactControlledValuePropTypes.checkPropTypes(
80 'input',
81 props,
81 - inst._currentElement._owner
82 + getCurrentOwnerName()
83 );
84
84 - var owner = inst._currentElement._owner;
85 -
85 if (
86 props.checked !== undefined &&
87 props.defaultChecked !== undefined &&
@@ -96,7 +95,7 @@ var ReactDOMInput = {
95 'both). Decide between using a controlled or uncontrolled input ' +
96 'element and remove one of these props. More info: ' +
97 'https://fb.me/react-controlled-components',
99 - owner && owner.getName() || 'A component',
98 + getCurrentOwnerName() || 'A component',
99 props.type
100 );
101 didWarnCheckedDefaultChecked = true;
@@ -137,7 +136,6 @@ var ReactDOMInput = {
136
137 if (__DEV__) {
138 var controlled = isControlled(props);
140 - var owner = inst._currentElement._owner;
139
140 if (!inst._wrapperState.controlled && controlled && !didWarnUncontrolledToControlled) {
141 warning(
@@ -146,7 +144,7 @@ var ReactDOMInput = {
144 'Input elements should not switch from uncontrolled to controlled (or vice versa). ' +
145 'Decide between using a controlled or uncontrolled input ' +
146 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components',
149 - owner && owner.getName() || 'A component',
147 + getCurrentOwnerName() || 'A component',
148 props.type
149 );
150 didWarnUncontrolledToControlled = true;
@@ -158,7 +156,7 @@ var ReactDOMInput = {
156 'Input elements should not switch from controlled to uncontrolled (or vice versa). ' +
157 'Decide between using a controlled or uncontrolled input ' +
158 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components',
161 - owner && owner.getName() || 'A component',
159 + getCurrentOwnerName() || 'A component',
160 props.type
161 );
162 didWarnControlledToUncontrolled = true;
src/renderers/dom/fiber/wrappers/ReactDOMFiberSelect.js
+8 -10
@@ -17,16 +17,15 @@ import type { Fiber } from 'ReactFiber';
17 var ReactControlledValuePropTypes = require('ReactControlledValuePropTypes');
18 var ReactDOMComponentTree = require('ReactDOMComponentTree');
19
20 +var getCurrentOwnerName = require('getCurrentOwnerName');
21 var warning = require('warning');
22
23 var didWarnValueDefaultValue = false;
24
24 -function getDeclarationErrorAddendum(owner) {
25 - if (owner) {
26 - var name = owner.getName();
27 - if (name) {
28 - return ' Check the render method of `' + name + '`.';
29 - }
25 +function getDeclarationErrorAddendum() {
26 + var ownerName = getCurrentOwnerName();
27 + if (ownerName) {
28 + return ' Check the render method of `' + ownerName + '`.';
29 }
30 return '';
31 }
@@ -37,11 +36,10 @@ var valuePropNames = ['value', 'defaultValue'];
36 * Validation function for `value` and `defaultValue`.
37 */
38 function checkSelectPropTypes(inst, props) {
40 - var owner = inst._currentElement._owner;
39 ReactControlledValuePropTypes.checkPropTypes(
40 'select',
41 props,
44 - owner
42 + getCurrentOwnerName()
43 );
44
45 for (var i = 0; i < valuePropNames.length; i++) {
@@ -56,7 +54,7 @@ function checkSelectPropTypes(inst, props) {
54 'The `%s` prop supplied to <select> must be an array if ' +
55 '`multiple` is true.%s',
56 propName,
59 - getDeclarationErrorAddendum(owner)
57 + getDeclarationErrorAddendum()
58 );
59 } else if (!props.multiple && isArray) {
60 warning(
@@ -64,7 +62,7 @@ function checkSelectPropTypes(inst, props) {
62 'The `%s` prop supplied to <select> must be a scalar ' +
63 'value if `multiple` is false.%s',
64 propName,
67 - getDeclarationErrorAddendum(owner)
65 + getDeclarationErrorAddendum()
66 );
67 }
68 }
src/renderers/dom/fiber/wrappers/ReactDOMFiberTextarea.js
+2 -1
@@ -17,6 +17,7 @@ import type { Fiber } from 'ReactFiber';
17 var ReactControlledValuePropTypes = require('ReactControlledValuePropTypes');
18 var ReactDOMComponentTree = require('ReactDOMComponentTree');
19
20 +var getCurrentOwnerName = require('getCurrentOwnerName');
21 var invariant = require('invariant');
22 var warning = require('warning');
23
@@ -63,7 +64,7 @@ var ReactDOMTextarea = {
64 ReactControlledValuePropTypes.checkPropTypes(
65 'textarea',
66 props,
66 - inst._currentElement._owner
67 + getCurrentOwnerName()
68 );
69 if (
70 props.value !== undefined &&
src/renderers/dom/shared/utils/ReactControlledValuePropTypes.js
+5 -9
@@ -13,7 +13,6 @@
13
14 var React = require('React');
15
16 -var getComponentName = require('getComponentName');
16 var warning = require('warning');
17
18 var hasReadOnlyValue = {
@@ -60,12 +59,9 @@ var propTypes = {
59 };
60
61 var loggedTypeFailures = {};
63 -function getDeclarationErrorAddendum(owner) {
64 - if (owner) {
65 - var name = getComponentName(owner);
66 - if (name) {
67 - return ' Check the render method of `' + name + '`.';
68 - }
62 +function getDeclarationErrorAddendum(ownerName) {
63 + if (ownerName) {
64 + return ' Check the render method of `' + ownerName + '`.';
65 }
66 return '';
67 }
@@ -75,7 +71,7 @@ function getDeclarationErrorAddendum(owner) {
71 * this outside of the ReactDOM controlled form components.
72 */
73 var ReactControlledValuePropTypes = {
78 - checkPropTypes: function(tagName, props, owner) {
74 + checkPropTypes: function(tagName, props, ownerName) {
75 for (var propName in propTypes) {
76 if (propTypes.hasOwnProperty(propName)) {
77 var error = propTypes[propName](
@@ -90,7 +86,7 @@ var ReactControlledValuePropTypes = {
86 // same error.
87 loggedTypeFailures[error.message] = true;
88
93 - var addendum = getDeclarationErrorAddendum(owner);
89 + var addendum = getDeclarationErrorAddendum(ownerName);
90 warning(false, 'Failed form propType: %s%s', error.message, addendum);
91 }
92 }
src/renderers/dom/stack/client/wrappers/ReactDOMInput.js
+2 -3
@@ -72,14 +72,13 @@ var ReactDOMInput = {
72
73 mountWrapper: function(inst, props) {
74 if (__DEV__) {
75 + var owner = inst._currentElement._owner;
76 ReactControlledValuePropTypes.checkPropTypes(
77 'input',
78 props,
78 - inst._currentElement._owner
79 + owner ? owner.getName() : null
80 );
81
81 - var owner = inst._currentElement._owner;
82 -
82 if (
83 props.checked !== undefined &&
84 props.defaultChecked !== undefined &&
src/renderers/dom/stack/client/wrappers/ReactDOMSelect.js
+1 -1
@@ -39,7 +39,7 @@ function checkSelectPropTypes(inst, props) {
39 ReactControlledValuePropTypes.checkPropTypes(
40 'select',
41 props,
42 - owner
42 + owner ? owner.getName() : null
43 );
44
45 for (var i = 0; i < valuePropNames.length; i++) {
src/renderers/dom/stack/client/wrappers/ReactDOMTextarea.js
+2 -1
@@ -57,10 +57,11 @@ var ReactDOMTextarea = {
57
58 mountWrapper: function(inst, props) {
59 if (__DEV__) {
60 + var owner = inst._currentElement._owner;
61 ReactControlledValuePropTypes.checkPropTypes(
62 'textarea',
63 props,
63 - inst._currentElement._owner
64 + owner ? owner.getName() : null
65 );
66 if (
67 props.value !== undefined &&
src/renderers/shared/fiber/getCurrentOwnerName.js new
+24
@@ -0,0 +1,24 @@
1 +/**
2 + * Copyright 2013-present, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @providesModule getCurrentOwnerName
10 + * @flow
11 + */
12 +
13 +'use strict';
14 +
15 +var ReactCurrentOwner = require('ReactCurrentOwner');
16 +var getComponentName = require('getComponentName');
17 +
18 +module.exports = function() : null | string {
19 + if (ReactCurrentOwner.current) {
20 + return getComponentName(ReactCurrentOwner.current);
21 + } else {
22 + return null;
23 + }
24 +};