Skip to content

Commit 3c977de

Browse files
raphamorimgaearon
authored andcommitted
react: convert var to let/const (#11715)
1 parent 2decfe9 commit 3c977de

21 files changed

+521
-508
lines changed

packages/react/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
var React = require('./src/React');
12+
const React = require('./src/React');
1313

1414
// TODO: decide on the top-level export form.
1515
// This is hacky but makes it work with both Rollup and Jest.

packages/react/src/React.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from './ReactElementValidator';
2626
import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
2727

28-
var React = {
28+
const React = {
2929
Children: {
3030
map,
3131
forEach,

packages/react/src/ReactBaseClasses.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Component.prototype.forceUpdate = function(callback) {
8585
* modern base class. Instead, we define a getter that warns if it's accessed.
8686
*/
8787
if (__DEV__) {
88-
var deprecatedAPIs = {
88+
const deprecatedAPIs = {
8989
isMounted: [
9090
'isMounted',
9191
'Instead, make sure to clean up subscriptions and pending requests in ' +
@@ -97,7 +97,7 @@ if (__DEV__) {
9797
'https://github.com/facebook/react/issues/3236).',
9898
],
9999
};
100-
var defineDeprecationWarning = function(methodName, info) {
100+
const defineDeprecationWarning = function(methodName, info) {
101101
Object.defineProperty(Component.prototype, methodName, {
102102
get: function() {
103103
lowPriorityWarning(
@@ -110,7 +110,7 @@ if (__DEV__) {
110110
},
111111
});
112112
};
113-
for (var fnName in deprecatedAPIs) {
113+
for (const fnName in deprecatedAPIs) {
114114
if (deprecatedAPIs.hasOwnProperty(fnName)) {
115115
defineDeprecationWarning(fnName, deprecatedAPIs[fnName]);
116116
}
@@ -132,7 +132,7 @@ function PureComponent(props, context, updater) {
132132

133133
function ComponentDummy() {}
134134
ComponentDummy.prototype = Component.prototype;
135-
var pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());
135+
const pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());
136136
pureComponentPrototype.constructor = PureComponent;
137137
// Avoid an extra prototype jump for these methods.
138138
Object.assign(pureComponentPrototype, Component.prototype);
@@ -148,7 +148,7 @@ function AsyncComponent(props, context, updater) {
148148
this.updater = updater || ReactNoopUpdateQueue;
149149
}
150150

151-
var asyncComponentPrototype = (AsyncComponent.prototype = new ComponentDummy());
151+
const asyncComponentPrototype = (AsyncComponent.prototype = new ComponentDummy());
152152
asyncComponentPrototype.constructor = AsyncComponent;
153153
// Avoid an extra prototype jump for these methods.
154154
Object.assign(asyncComponentPrototype, Component.prototype);

packages/react/src/ReactChildren.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import {
1919
import {isValidElement, cloneAndReplaceKey} from './ReactElement';
2020
import ReactDebugCurrentFrame from './ReactDebugCurrentFrame';
2121

22-
var SEPARATOR = '.';
23-
var SUBSEPARATOR = ':';
22+
const SEPARATOR = '.';
23+
const SUBSEPARATOR = ':';
2424

2525
/**
2626
* Escape and wrap key so it is safe to use as a reactid
@@ -29,12 +29,12 @@ var SUBSEPARATOR = ':';
2929
* @return {string} the escaped key.
3030
*/
3131
function escape(key) {
32-
var escapeRegex = /[=:]/g;
33-
var escaperLookup = {
32+
const escapeRegex = /[=:]/g;
33+
const escaperLookup = {
3434
'=': '=0',
3535
':': '=2',
3636
};
37-
var escapedString = ('' + key).replace(escapeRegex, function(match) {
37+
const escapedString = ('' + key).replace(escapeRegex, function(match) {
3838
return escaperLookup[match];
3939
});
4040

@@ -46,23 +46,23 @@ function escape(key) {
4646
* pattern.
4747
*/
4848

49-
var didWarnAboutMaps = false;
49+
let didWarnAboutMaps = false;
5050

51-
var userProvidedKeyEscapeRegex = /\/+/g;
51+
const userProvidedKeyEscapeRegex = /\/+/g;
5252
function escapeUserProvidedKey(text) {
5353
return ('' + text).replace(userProvidedKeyEscapeRegex, '$&/');
5454
}
5555

56-
var POOL_SIZE = 10;
57-
var traverseContextPool = [];
56+
const POOL_SIZE = 10;
57+
const traverseContextPool = [];
5858
function getPooledTraverseContext(
5959
mapResult,
6060
keyPrefix,
6161
mapFunction,
6262
mapContext,
6363
) {
6464
if (traverseContextPool.length) {
65-
var traverseContext = traverseContextPool.pop();
65+
const traverseContext = traverseContextPool.pop();
6666
traverseContext.result = mapResult;
6767
traverseContext.keyPrefix = keyPrefix;
6868
traverseContext.func = mapFunction;
@@ -105,7 +105,7 @@ function traverseAllChildrenImpl(
105105
callback,
106106
traverseContext,
107107
) {
108-
var type = typeof children;
108+
const type = typeof children;
109109

110110
if (type === 'undefined' || type === 'boolean') {
111111
// All of the above are perceived as null.
@@ -144,13 +144,14 @@ function traverseAllChildrenImpl(
144144
return 1;
145145
}
146146

147-
var child;
148-
var nextName;
149-
var subtreeCount = 0; // Count of children found in the current subtree.
150-
var nextNamePrefix = nameSoFar === '' ? SEPARATOR : nameSoFar + SUBSEPARATOR;
147+
let child;
148+
let nextName;
149+
let subtreeCount = 0; // Count of children found in the current subtree.
150+
const nextNamePrefix =
151+
nameSoFar === '' ? SEPARATOR : nameSoFar + SUBSEPARATOR;
151152

152153
if (Array.isArray(children)) {
153-
for (var i = 0; i < children.length; i++) {
154+
for (let i = 0; i < children.length; i++) {
154155
child = children[i];
155156
nextName = nextNamePrefix + getComponentKey(child, i);
156157
subtreeCount += traverseAllChildrenImpl(
@@ -161,7 +162,7 @@ function traverseAllChildrenImpl(
161162
);
162163
}
163164
} else {
164-
var iteratorFn = getIteratorFn(children);
165+
const iteratorFn = getIteratorFn(children);
165166
if (typeof iteratorFn === 'function') {
166167
if (__DEV__) {
167168
// Warn about using Maps as children
@@ -177,9 +178,9 @@ function traverseAllChildrenImpl(
177178
}
178179
}
179180

180-
var iterator = iteratorFn.call(children);
181-
var step;
182-
var ii = 0;
181+
const iterator = iteratorFn.call(children);
182+
let step;
183+
let ii = 0;
183184
while (!(step = iterator.next()).done) {
184185
child = step.value;
185186
nextName = nextNamePrefix + getComponentKey(child, ii++);
@@ -191,14 +192,14 @@ function traverseAllChildrenImpl(
191192
);
192193
}
193194
} else if (type === 'object') {
194-
var addendum = '';
195+
let addendum = '';
195196
if (__DEV__) {
196197
addendum =
197198
' If you meant to render a collection of children, use an array ' +
198199
'instead.' +
199200
ReactDebugCurrentFrame.getStackAddendum();
200201
}
201-
var childrenString = '' + children;
202+
const childrenString = '' + children;
202203
invariant(
203204
false,
204205
'Objects are not valid as a React child (found: %s).%s',
@@ -260,7 +261,7 @@ function getComponentKey(component, index) {
260261
}
261262

262263
function forEachSingleChild(bookKeeping, child, name) {
263-
var {func, context} = bookKeeping;
264+
const {func, context} = bookKeeping;
264265
func.call(context, child, bookKeeping.count++);
265266
}
266267

@@ -280,7 +281,7 @@ function forEachChildren(children, forEachFunc, forEachContext) {
280281
if (children == null) {
281282
return children;
282283
}
283-
var traverseContext = getPooledTraverseContext(
284+
const traverseContext = getPooledTraverseContext(
284285
null,
285286
null,
286287
forEachFunc,
@@ -291,9 +292,9 @@ function forEachChildren(children, forEachFunc, forEachContext) {
291292
}
292293

293294
function mapSingleChildIntoContext(bookKeeping, child, childKey) {
294-
var {result, keyPrefix, func, context} = bookKeeping;
295+
const {result, keyPrefix, func, context} = bookKeeping;
295296

296-
var mappedChild = func.call(context, child, bookKeeping.count++);
297+
let mappedChild = func.call(context, child, bookKeeping.count++);
297298
if (Array.isArray(mappedChild)) {
298299
mapIntoWithKeyPrefixInternal(
299300
mappedChild,
@@ -319,11 +320,11 @@ function mapSingleChildIntoContext(bookKeeping, child, childKey) {
319320
}
320321

321322
function mapIntoWithKeyPrefixInternal(children, array, prefix, func, context) {
322-
var escapedPrefix = '';
323+
let escapedPrefix = '';
323324
if (prefix != null) {
324325
escapedPrefix = escapeUserProvidedKey(prefix) + '/';
325326
}
326-
var traverseContext = getPooledTraverseContext(
327+
const traverseContext = getPooledTraverseContext(
327328
array,
328329
escapedPrefix,
329330
func,
@@ -350,7 +351,7 @@ function mapChildren(children, func, context) {
350351
if (children == null) {
351352
return children;
352353
}
353-
var result = [];
354+
const result = [];
354355
mapIntoWithKeyPrefixInternal(children, result, null, func, context);
355356
return result;
356357
}
@@ -375,7 +376,7 @@ function countChildren(children, context) {
375376
* See https://reactjs.org/docs/react-api.html#react.children.toarray
376377
*/
377378
function toArray(children) {
378-
var result = [];
379+
const result = [];
379380
mapIntoWithKeyPrefixInternal(
380381
children,
381382
result,

packages/react/src/ReactCurrentOwner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {Fiber} from 'react-reconciler/src/ReactFiber';
1515
* The current owner is the component who should own any components that are
1616
* currently being constructed.
1717
*/
18-
var ReactCurrentOwner = {
18+
const ReactCurrentOwner = {
1919
/**
2020
* @internal
2121
* @type {ReactComponent}

0 commit comments

Comments
 (0)