Skip to content

Commit bb104d9

Browse files
Convert more cycles to 'for..of' (#2100)
Since #2099 makes it zero-cost to use 'for..of' it's better to improve code readability and consistency
1 parent 4339864 commit bb104d9

File tree

5 files changed

+14
-22
lines changed

5 files changed

+14
-22
lines changed

src/execution/execute.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ export function buildExecutionContext(
281281
let operation: OperationDefinitionNode | void;
282282
let hasMultipleAssumedOperations = false;
283283
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
284-
for (let i = 0; i < document.definitions.length; i++) {
285-
const definition = document.definitions[i];
284+
for (const definition of document.definitions) {
286285
switch (definition.kind) {
287286
case Kind.OPERATION_DEFINITION:
288287
if (!operationName && operation) {
@@ -434,8 +433,7 @@ function executeFields(
434433
const results = Object.create(null);
435434
let containsPromise = false;
436435

437-
for (let i = 0, keys = Object.keys(fields); i < keys.length; ++i) {
438-
const responseName = keys[i];
436+
for (const responseName of Object.keys(fields)) {
439437
const fieldNodes = fields[responseName];
440438
const fieldPath = addPath(path, responseName);
441439
const result = resolveField(
@@ -480,8 +478,7 @@ export function collectFields(
480478
fields: ObjMap<Array<FieldNode>>,
481479
visitedFragmentNames: ObjMap<boolean>,
482480
): ObjMap<Array<FieldNode>> {
483-
for (let i = 0; i < selectionSet.selections.length; i++) {
484-
const selection = selectionSet.selections[i];
481+
for (const selection of selectionSet.selections) {
485482
switch (selection.kind) {
486483
case Kind.FIELD: {
487484
if (!shouldIncludeNode(exeContext, selection)) {
@@ -1108,13 +1105,12 @@ function _collectSubfields(
11081105
): ObjMap<Array<FieldNode>> {
11091106
let subFieldNodes = Object.create(null);
11101107
const visitedFragmentNames = Object.create(null);
1111-
for (let i = 0; i < fieldNodes.length; i++) {
1112-
const selectionSet = fieldNodes[i].selectionSet;
1113-
if (selectionSet) {
1108+
for (const node of fieldNodes) {
1109+
if (node.selectionSet) {
11141110
subFieldNodes = collectFields(
11151111
exeContext,
11161112
returnType,
1117-
selectionSet,
1113+
node.selectionSet,
11181114
subFieldNodes,
11191115
visitedFragmentNames,
11201116
);

src/jsutils/suggestionList.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ function lexicalDistance(aStr, bStr) {
4545
return 0;
4646
}
4747

48-
let i;
49-
let j;
5048
const d = [];
5149
const a = aStr.toLowerCase();
5250
const b = bStr.toLowerCase();
@@ -58,16 +56,16 @@ function lexicalDistance(aStr, bStr) {
5856
return 1;
5957
}
6058

61-
for (i = 0; i <= aLength; i++) {
59+
for (let i = 0; i <= aLength; i++) {
6260
d[i] = [i];
6361
}
6462

65-
for (j = 1; j <= bLength; j++) {
63+
for (let j = 1; j <= bLength; j++) {
6664
d[0][j] = j;
6765
}
6866

69-
for (i = 1; i <= aLength; i++) {
70-
for (j = 1; j <= bLength; j++) {
67+
for (let i = 1; i <= aLength; i++) {
68+
for (let j = 1; j <= bLength; j++) {
7169
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
7270

7371
d[i][j] = Math.min(

src/polyfills/find.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const find = Array.prototype.find
1212
return Array.prototype.find.call(list, predicate);
1313
}
1414
: function(list, predicate) {
15-
for (let i = 0; i < list.length; i++) {
16-
const value = list[i];
15+
for (const value of list) {
1716
if (predicate(value)) {
1817
return value;
1918
}

src/polyfills/flatMap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const flatMap = Array.prototype.flatMap
1313
}
1414
: function(list, fn) {
1515
let result = [];
16-
for (let i = 0; i < list.length; i++) {
17-
const value = fn(list[i]);
16+
for (const item of list) {
17+
const value = fn(item);
1818
if (Array.isArray(value)) {
1919
result = result.concat(value);
2020
} else {

src/validation/rules/OverlappingFieldsCanBeMerged.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,7 @@ function _collectFieldsAndFragmentNames(
747747
nodeAndDefs,
748748
fragmentNames,
749749
): void {
750-
for (let i = 0; i < selectionSet.selections.length; i++) {
751-
const selection = selectionSet.selections[i];
750+
for (const selection of selectionSet.selections) {
752751
switch (selection.kind) {
753752
case Kind.FIELD: {
754753
const fieldName = selection.name.value;

0 commit comments

Comments
 (0)