Skip to content

Commit 172e89b

Browse files
Reland Remove redundant initial of isArray (#21188)
* Remove redundant initial of isArray (#21163) * Reapply prettier * Type the isArray function with refinement support This ensures that an argument gets refined just like it does if isArray is used directly. I'm not sure how to express with just a direct reference so I added a function wrapper and confirmed that this does get inlined properly by closure compiler. * A few more * Rename unit test to internal This is not testing a bundle. Co-authored-by: Behnam Mohammadi <itten@live.com>
1 parent ee6a05c commit 172e89b

38 files changed

Lines changed: 141 additions & 97 deletions

‎packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export default {
145145
componentScope=currentScope;
146146
}
147147

148+
constisArray=Array.isArray;
149+
148150
// Next we'll define a few helpers that helps us
149151
// tell if some values don't have to be declared as deps.
150152

@@ -157,7 +159,7 @@ export default {
157159
// ^^^ true for this reference
158160
// False for everything else.
159161
functionisStableKnownHookValue(resolved){
160-
if(!Array.isArray(resolved.defs)){
162+
if(!isArray(resolved.defs)){
161163
returnfalse;
162164
}
163165
constdef=resolved.defs[0];
@@ -226,7 +228,7 @@ export default {
226228
if(
227229
id.type==='ArrayPattern'&&
228230
id.elements.length===2&&
229-
Array.isArray(resolved.identifiers)
231+
isArray(resolved.identifiers)
230232
){
231233
// Is second tuple value the same reference we're checking?
232234
if(id.elements[1]===resolved.identifiers[0]){
@@ -253,10 +255,7 @@ export default {
253255
}
254256
}
255257
}elseif(name==='useTransition'){
256-
if(
257-
id.type==='ArrayPattern'&&
258-
Array.isArray(resolved.identifiers)
259-
){
258+
if(id.type==='ArrayPattern'&&isArray(resolved.identifiers)){
260259
// Is first tuple value the same reference we're checking?
261260
if(id.elements[0]===resolved.identifiers[0]){
262261
// Setter is stable.
@@ -270,7 +269,7 @@ export default {
270269

271270
// Some are just functions that don't reference anything dynamic.
272271
functionisFunctionWithoutCapturedValues(resolved){
273-
if(!Array.isArray(resolved.defs)){
272+
if(!isArray(resolved.defs)){
274273
returnfalse;
275274
}
276275
constdef=resolved.defs[0];

‎packages/jest-react/src/JestReact.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import{REACT_ELEMENT_TYPE,REACT_FRAGMENT_TYPE}from'shared/ReactSymbols';
99

1010
importinvariantfrom'shared/invariant';
11+
importisArrayfrom'shared/isArray';
1112

1213
functioncaptureAssertion(fn){
1314
// Trick to use a Jest matcher inside another Jest matcher. `fn` contains an
@@ -42,7 +43,7 @@ export function unstable_toMatchRenderedOutput(root, expectedJSX) {
4243
letactualJSX;
4344
if(actualJSON===null||typeofactualJSON==='string'){
4445
actualJSX=actualJSON;
45-
}elseif(Array.isArray(actualJSON)){
46+
}elseif(isArray(actualJSON)){
4647
if(actualJSON.length===0){
4748
actualJSX=null;
4849
}elseif(actualJSON.length===1){

‎packages/react-devtools-shared/src/backend/renderer.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import type {
105105
ElementType,
106106
}from'react-devtools-shared/src/types';
107107
importisfrom'shared/objectIs';
108+
importisArrayfrom'shared/isArray';
108109

109110
typegetDisplayNameForFiberType=(fiber: Fiber)=>string|null;
110111
typegetTypeSymbolType=(type: any)=>Symbol|number;
@@ -1137,7 +1138,7 @@ export function attach(
11371138
memoizedState.hasOwnProperty('create')&&
11381139
memoizedState.hasOwnProperty('destroy')&&
11391140
memoizedState.hasOwnProperty('deps')&&
1140-
(memoizedState.deps===null||Array.isArray(memoizedState.deps))&&
1141+
(memoizedState.deps===null||isArray(memoizedState.deps))&&
11411142
memoizedState.hasOwnProperty('next')
11421143
);
11431144
}

‎packages/react-devtools-shared/src/backend/utils.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import{copy}from'clipboard-js';
1111
import{dehydrate}from'../hydration';
12+
importisArrayfrom'shared/isArray';
1213

1314
importtype{DehydratedData}from'react-devtools-shared/src/devtools/views/Components/types';
1415

@@ -61,9 +62,9 @@ export function copyWithDelete(
6162
index: number=0,
6263
): Object|Array<any>{
6364
const key=path[index];
64-
constupdated=Array.isArray(obj) ? obj.slice() : {...obj};
65+
constupdated=isArray(obj) ? obj.slice() : {...obj};
6566
if(index+1===path.length){
66-
if(Array.isArray(updated)){
67+
if(isArray(updated)){
6768
updated.splice(((key: any): number),1);
6869
}else{
6970
deleteupdated[key];
@@ -84,12 +85,12 @@ export function copyWithRename(
8485
index: number=0,
8586
): Object|Array<any>{
8687
const oldKey =oldPath[index];
87-
constupdated=Array.isArray(obj) ? obj.slice() : {...obj};
88+
constupdated=isArray(obj) ? obj.slice() : {...obj};
8889
if(index+1===oldPath.length){
8990
constnewKey=newPath[index];
9091
// $FlowFixMe number or string is fine here
9192
updated[newKey]=updated[oldKey];
92-
if(Array.isArray(updated)){
93+
if(isArray(updated)){
9394
updated.splice(((oldKey: any): number),1);
9495
}else{
9596
deleteupdated[oldKey];
@@ -111,7 +112,7 @@ export function copyWithSet(
111112
returnvalue;
112113
}
113114
const key =path[index];
114-
constupdated=Array.isArray(obj) ? obj.slice() : {...obj};
115+
constupdated=isArray(obj) ? obj.slice() : {...obj};
115116
// $FlowFixMe number or string is fine here
116117
updated[key]=copyWithSet(obj[key],path,value,index+1);
117118
returnupdated;

‎packages/react-dom/src/client/ReactDOMSelect.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {getCurrentFiberOwnerNameInDevOrNull} from 'react-reconciler/src/ReactCur
1212

1313
import{checkControlledValueProps}from'../shared/ReactControlledValuePropTypes';
1414
import{getToStringValue,toString}from'./ToStringValue';
15+
importisArrayfrom'shared/isArray';
1516

1617
letdidWarnValueDefaultValue;
1718

@@ -45,15 +46,15 @@ function checkSelectPropTypes(props) {
4546
if(props[propName]==null){
4647
continue;
4748
}
48-
constisArray=Array.isArray(props[propName]);
49-
if(props.multiple&&!isArray){
49+
constpropNameIsArray=isArray(props[propName]);
50+
if(props.multiple&&!propNameIsArray){
5051
console.error(
5152
'The `%s` prop supplied to <select> must be an array if '+
5253
'`multiple` is true.%s',
5354
propName,
5455
getDeclarationErrorAddendum(),
5556
);
56-
}elseif(!props.multiple&&isArray){
57+
}elseif(!props.multiple&&propNameIsArray){
5758
console.error(
5859
'The `%s` prop supplied to <select> must be a scalar '+
5960
'value if `multiple` is false.%s',

‎packages/react-dom/src/client/ReactDOMTextarea.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
*/
99

1010
importinvariantfrom'shared/invariant';
11+
importisArrayfrom'shared/isArray';
1112

1213
import{checkControlledValueProps}from'../shared/ReactControlledValuePropTypes';
1314
import{getCurrentFiberOwnerNameInDevOrNull}from'react-reconciler/src/ReactCurrentFiber';
1415
import{getToStringValue,toString}from'./ToStringValue';
1516
importtype{ToStringValue}from'./ToStringValue';
16-
1717
import{disableTextareaChildren}from'shared/ReactFeatureFlags';
1818

1919
letdidWarnValDefaultVal=false;
@@ -100,7 +100,7 @@ export function initWrapperState(element: Element, props: Object) {
100100
defaultValue==null,
101101
'If you supply `defaultValue` on a <textarea>, do not pass children.',
102102
);
103-
if(Array.isArray(children)){
103+
if(isArray(children)){
104104
invariant(
105105
children.length<=1,
106106
'<textarea> can only have at most one child.',

‎packages/react-dom/src/server/ReactDOMServerFormatConfig.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ import hyphenateStyleName from '../shared/hyphenateStyleName';
4646
importinvariantfrom'shared/invariant';
4747
importhasOwnPropertyfrom'shared/hasOwnProperty';
4848
importsanitizeURLfrom'../shared/sanitizeURL';
49-
50-
constisArray=Array.isArray;
49+
importisArrayfrom'shared/isArray';
5150

5251
// Per response, global state that is not contextual to the rendering subtree.
5352
exporttypeResponseState={

‎packages/react-dom/src/server/ReactPartialRenderer.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ReactProvider, ReactContext} from 'shared/ReactTypes';
1414

1515
import*asReactfrom'react';
1616
importinvariantfrom'shared/invariant';
17+
importisArrayfrom'shared/isArray';
1718
importgetComponentNameFromTypefrom'shared/getComponentNameFromType';
1819
import{describeUnknownElementTypeFrameInDEV}from'shared/ReactComponentStackFrame';
1920
importReactSharedInternalsfrom'shared/ReactSharedInternals';
@@ -1438,7 +1439,7 @@ class ReactDOMServerRenderer {
14381439
defaultValue==null,
14391440
'If you supply `defaultValue` on a <textarea>, do not pass children.',
14401441
);
1441-
if(Array.isArray(textareaChildren)){
1442+
if(isArray(textareaChildren)){
14421443
invariant(
14431444
textareaChildren.length<=1,
14441445
'<textarea> can only have at most one child.',
@@ -1467,14 +1468,14 @@ class ReactDOMServerRenderer {
14671468
if(props[propName]==null){
14681469
continue;
14691470
}
1470-
constisArray=Array.isArray(props[propName]);
1471-
if(props.multiple&&!isArray){
1471+
constpropNameIsArray=isArray(props[propName]);
1472+
if(props.multiple&&!propNameIsArray){
14721473
console.error(
14731474
'The `%s` prop supplied to <select> must be an array if '+
14741475
'`multiple` is true.',
14751476
propName,
14761477
);
1477-
}elseif(!props.multiple&&isArray){
1478+
}elseif(!props.multiple&&propNameIsArray){
14781479
console.error(
14791480
'The `%s` prop supplied to <select> must be a scalar '+
14801481
'value if `multiple` is false.',
@@ -1515,7 +1516,7 @@ class ReactDOMServerRenderer {
15151516
value=optionChildren;
15161517
}
15171518
selected=false;
1518-
if(Array.isArray(selectValue)){
1519+
if(isArray(selectValue)){
15191520
// multiple
15201521
for(letj=0;j<selectValue.length;j++){
15211522
if(''+selectValue[j]===value){

‎packages/react-dom/src/test-utils/ReactTestUtils.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
rethrowCaughtError,
2525
invokeGuardedCallbackAndCatchFirstError,
2626
}from'shared/ReactErrorUtils';
27+
importisArrayfrom'shared/isArray';
2728

2829
// Keep in sync with ReactDOM.js, and ReactTestUtilsAct.js:
2930
constEventInternals=
@@ -97,7 +98,7 @@ function validateClassInstance(inst, methodName) {
9798
}
9899
letreceived;
99100
conststringified=''+inst;
100-
if(Array.isArray(inst)){
101+
if(isArray(inst)){
101102
received='an array';
102103
}elseif(inst&&inst.nodeType===ELEMENT_NODE&&inst.tagName){
103104
received='a DOM node';
@@ -197,7 +198,7 @@ function scryRenderedDOMComponentsWithClass(root, classNames) {
197198
}
198199
constclassList=className.split(/\s+/);
199200

200-
if(!Array.isArray(classNames)){
201+
if(!isArray(classNames)){
201202
invariant(
202203
classNames!==undefined,
203204
'TestUtils.scryRenderedDOMComponentsWithClass expects a '+
@@ -365,7 +366,7 @@ function executeDispatch(event, listener, inst) {
365366
functionexecuteDispatchesInOrder(event){
366367
constdispatchListeners=event._dispatchListeners;
367368
constdispatchInstances=event._dispatchInstances;
368-
if(Array.isArray(dispatchListeners)){
369+
if(isArray(dispatchListeners)){
369370
for(leti=0;i<dispatchListeners.length;i++){
370371
if(event.isPropagationStopped()){
371372
break;

‎packages/react-native-renderer/src/ReactNativeAttributePayload.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
deepDiffer,
1313
flattenStyle,
1414
}from'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
15+
importisArrayfrom'shared/isArray';
1516

1617
importtype{AttributeConfiguration}from'./ReactNativeTypes';
1718

@@ -51,7 +52,7 @@ function restoreDeletedValuesInNestedArray(
5152
node: NestedNode,
5253
validAttributes: AttributeConfiguration,
5354
){
54-
if(Array.isArray(node)){
55+
if(isArray(node)){
5556
leti=node.length;
5657
while(i--&&removedKeyCount>0){
5758
restoreDeletedValuesInNestedArray(
@@ -163,12 +164,12 @@ function diffNestedProperty(
163164
returnupdatePayload;
164165
}
165166

166-
if(!Array.isArray(prevProp)&&!Array.isArray(nextProp)){
167+
if(!isArray(prevProp)&&!isArray(nextProp)){
167168
// Both are leaves, we can diff the leaves.
168169
returndiffProperties(updatePayload,prevProp,nextProp,validAttributes);
169170
}
170171

171-
if(Array.isArray(prevProp)&&Array.isArray(nextProp)){
172+
if(isArray(prevProp)&&isArray(nextProp)){
172173
// Both are arrays, we can diff the arrays.
173174
returndiffNestedArrayProperty(
174175
updatePayload,
@@ -178,7 +179,7 @@ function diffNestedProperty(
178179
);
179180
}
180181

181-
if(Array.isArray(prevProp)){
182+
if(isArray(prevProp)){
182183
returndiffProperties(
183184
updatePayload,
184185
// $FlowFixMe - We know that this is always an object when the input is.
@@ -212,7 +213,7 @@ function addNestedProperty(
212213
returnupdatePayload;
213214
}
214215

215-
if(!Array.isArray(nextProp)){
216+
if(!isArray(nextProp)){
216217
// Add each property of the leaf.
217218
returnaddProperties(updatePayload,nextProp,validAttributes);
218219
}
@@ -242,7 +243,7 @@ function clearNestedProperty(
242243
returnupdatePayload;
243244
}
244245

245-
if(!Array.isArray(prevProp)){
246+
if(!isArray(prevProp)){
246247
// Add each property of the leaf.
247248
returnclearProperties(updatePayload,prevProp,validAttributes);
248249
}

0 commit comments

Comments
 (0)