Remove unnecessary code for host text as we skip it anyway
Dan Abramov committed
May 31, 2019 at 14:27 UTC
9f44251ebf60af8331aaff5ced5629675e04e4bd
3 files changed
+5
-170
src/backend/legacy/getChildren.js
+2
-24
@@ -1,7 +1,5 @@
1
// @flow
2
3
-import traverseAllChildrenImpl from './traverseAllChildrenImpl';
4
-
3
// TODO (legacy) Respect component filters
4
5
export default function getChildren(internalInstance: Object): Array<any> {
@@ -21,29 +19,9 @@ export default function getChildren(internalInstance: Object): Array<any> {
19
children = [internalInstance._renderedComponent];
20
} else if (internalInstance._renderedChildren) {
21
children = renderedChildrenToArray(internalInstance._renderedChildren);
24
- } else if (
25
- internalInstance._currentElement &&
26
- internalInstance._currentElement.props
27
- ) {
28
- // This is a native node without rendered children -- meaning the children
29
- // prop is the unfiltered list of children.
30
- // This may include 'null' or even other invalid values, so we need to
31
- // filter it the same way that ReactDOM does.
32
- // Instead of pulling in the whole React library, we just copied over the
33
- // 'traverseAllChildrenImpl' method.
34
- // https://github.com/facebook/react/blob/240b84ed8e1db715d759afaae85033718a0b24e1/src/isomorphic/children/ReactChildren.js#L112-L158
35
- const unfilteredChildren = internalInstance._currentElement.props.children;
36
- traverseAllChildrenImpl(
37
- unfilteredChildren,
38
- '', // nameSoFar
39
- (_traverseContext, child) => {
40
- const childType = typeof child;
41
- if (childType === 'string' || childType === 'number') {
42
- children.push(child);
43
- }
44
- }
45
- );
22
}
23
+ // Note: we skip the case where children are just strings or numbers
24
+ // because the new DevTools skips over host text nodes anyway.
25
26
const instance = internalInstance._instance;
27
if (instance) {
src/backend/legacy/renderer.js
+3
@@ -52,6 +52,9 @@ export function attach(
52
const rootIDs: Set<number> = new Set();
53
54
function getID(internalInstance: InternalInstance): number {
55
+ if (typeof internalInstance !== 'object') {
56
+ throw new Error('Invalid internal instance: ' + internalInstance);
57
+ }
58
if (!internalInstanceToIDMap.has(internalInstance)) {
59
const id = getUID();
60
internalInstanceToIDMap.set(internalInstance, id);
src/backend/legacy/traverseAllChildrenImpl.js
deleted
-146
@@ -1,146 +0,0 @@
1
-// @flow
2
-
3
-const SEPARATOR = '.';
4
-const SUBSEPARATOR = ':';
5
-
6
-const FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.
7
-// The Symbol used to tag the ReactElement type. If there is no native Symbol
8
-// nor polyfill, then a plain number is used for performance.
9
-const ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
10
-const REACT_ELEMENT_TYPE =
11
- (typeof Symbol === 'function' && Symbol.for && Symbol.for('react.element')) ||
12
- 0xeac7;
13
-
14
-/**
15
- * Escape and wrap key so it is safe to use as a reactid
16
- *
17
- * @param {string} key to be escaped.
18
- * @return {string} the escaped key.
19
- */
20
-function escape(key: string): string {
21
- const escapeRegex = /[=:]/g;
22
- const escaperLookup = {
23
- '=': '=0',
24
- ':': '=2',
25
- };
26
- const escapedString = ('' + key).replace(escapeRegex, function(match) {
27
- return escaperLookup[match];
28
- });
29
-
30
- return '$' + escapedString;
31
-}
32
-
33
-/**
34
- * Generate a key string that identifies a component within a set.
35
- *
36
- * @param {*} component A component that could contain a manual key.
37
- * @param {number} index Index that is used if a manual key is not provided.
38
- * @return {string}
39
- */
40
-function getComponentKey(component, index) {
41
- // Do some typechecking here since we call this blindly. We want to ensure
42
- // that we don't block potential future ES APIs.
43
- if (
44
- typeof component === 'object' &&
45
- component !== null &&
46
- component.key != null
47
- ) {
48
- // Explicit key
49
- return escape(component.key);
50
- }
51
- // Implicit key determined by the index in the set
52
- return index.toString(36);
53
-}
54
-
55
-/**
56
- * We do a copied the 'traverseAllChildrenImpl' method from
57
- * `React.Children` so that we don't pull in the whole React library.
58
- * @param {?*} children Children tree container.
59
- * @param {!string} nameSoFar Name of the key path so far.
60
- * @param {!function} callback Callback to invoke with each child found.
61
- * @param {?*} traverseContext Used to pass information throughout the traversal
62
- * process.
63
- * @return {!number} The number of children in this subtree.
64
- */
65
-export default function traverseAllChildrenImpl(
66
- children: any,
67
- nameSoFar: string,
68
- callback: Function,
69
- traverseContext: any
70
-): number {
71
- const type = typeof children;
72
-
73
- if (type === 'undefined' || type === 'boolean') {
74
- // All of the above are perceived as null.
75
- children = null;
76
- }
77
-
78
- if (
79
- children === null ||
80
- type === 'string' ||
81
- type === 'number' ||
82
- // The following is inlined from ReactElement. This means we can optimize
83
- // some checks. React Fiber also inlines this logic for similar purposes.
84
- (type === 'object' && children.$$typeof === REACT_ELEMENT_TYPE)
85
- ) {
86
- callback(
87
- traverseContext,
88
- children,
89
- // If it's the only child, treat the name as if it was wrapped in an array
90
- // so that it's consistent if the number of children grows.
91
- nameSoFar === '' ? SEPARATOR + getComponentKey(children, 0) : nameSoFar
92
- );
93
- return 1;
94
- }
95
-
96
- let child;
97
- let nextName;
98
- let subtreeCount = 0; // Count of children found in the current subtree.
99
- let nextNamePrefix = nameSoFar === '' ? SEPARATOR : nameSoFar + SUBSEPARATOR;
100
-
101
- if (Array.isArray(children)) {
102
- for (let i = 0; i < children.length; i++) {
103
- child = children[i];
104
- nextName = nextNamePrefix + getComponentKey(child, i);
105
- subtreeCount += traverseAllChildrenImpl(
106
- child,
107
- nextName,
108
- callback,
109
- traverseContext
110
- );
111
- }
112
- } else {
113
- let iteratorFn =
114
- (ITERATOR_SYMBOL && children[ITERATOR_SYMBOL]) ||
115
- children[FAUX_ITERATOR_SYMBOL];
116
- if (typeof iteratorFn === 'function') {
117
- let iterator = iteratorFn.call(children);
118
- let step;
119
- let ii = 0;
120
- while (!(step = iterator.next()).done) {
121
- child = step.value;
122
- nextName = nextNamePrefix + getComponentKey(child, ii++);
123
- subtreeCount += traverseAllChildrenImpl(
124
- child,
125
- nextName,
126
- callback,
127
- traverseContext
128
- );
129
- }
130
- } else if (type === 'object') {
131
- let addendum =
132
- ' If you meant to render a collection of children, use an array ' +
133
- 'instead.';
134
- let childrenString = '' + children;
135
- throw Error(
136
- `The React Devtools cannot render an object as a child. (found: ${
137
- childrenString === '[object Object]'
138
- ? 'object with keys {' + Object.keys(children).join(', ') + '}'
139
- : childrenString
140
- }). ${addendum}`
141
- );
142
- }
143
- }
144
-
145
- return subtreeCount;
146
-}