Skip to content

Commit

Permalink
Convert more cycles to 'for..of' (#2100)
Browse files Browse the repository at this point in the history
Since #2099 makes it zero-cost to use 'for..of' it's better to improve
code readability and consistency
  • Loading branch information
IvanGoncharov authored Aug 20, 2019
1 parent 4339864 commit bb104d9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
16 changes: 6 additions & 10 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ export function buildExecutionContext(
let operation: OperationDefinitionNode | void;
let hasMultipleAssumedOperations = false;
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
for (let i = 0; i < document.definitions.length; i++) {
const definition = document.definitions[i];
for (const definition of document.definitions) {
switch (definition.kind) {
case Kind.OPERATION_DEFINITION:
if (!operationName && operation) {
Expand Down Expand Up @@ -434,8 +433,7 @@ function executeFields(
const results = Object.create(null);
let containsPromise = false;

for (let i = 0, keys = Object.keys(fields); i < keys.length; ++i) {
const responseName = keys[i];
for (const responseName of Object.keys(fields)) {
const fieldNodes = fields[responseName];
const fieldPath = addPath(path, responseName);
const result = resolveField(
Expand Down Expand Up @@ -480,8 +478,7 @@ export function collectFields(
fields: ObjMap<Array<FieldNode>>,
visitedFragmentNames: ObjMap<boolean>,
): ObjMap<Array<FieldNode>> {
for (let i = 0; i < selectionSet.selections.length; i++) {
const selection = selectionSet.selections[i];
for (const selection of selectionSet.selections) {
switch (selection.kind) {
case Kind.FIELD: {
if (!shouldIncludeNode(exeContext, selection)) {
Expand Down Expand Up @@ -1108,13 +1105,12 @@ function _collectSubfields(
): ObjMap<Array<FieldNode>> {
let subFieldNodes = Object.create(null);
const visitedFragmentNames = Object.create(null);
for (let i = 0; i < fieldNodes.length; i++) {
const selectionSet = fieldNodes[i].selectionSet;
if (selectionSet) {
for (const node of fieldNodes) {
if (node.selectionSet) {
subFieldNodes = collectFields(
exeContext,
returnType,
selectionSet,
node.selectionSet,
subFieldNodes,
visitedFragmentNames,
);
Expand Down
10 changes: 4 additions & 6 deletions src/jsutils/suggestionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ function lexicalDistance(aStr, bStr) {
return 0;
}

let i;
let j;
const d = [];
const a = aStr.toLowerCase();
const b = bStr.toLowerCase();
Expand All @@ -58,16 +56,16 @@ function lexicalDistance(aStr, bStr) {
return 1;
}

for (i = 0; i <= aLength; i++) {
for (let i = 0; i <= aLength; i++) {
d[i] = [i];
}

for (j = 1; j <= bLength; j++) {
for (let j = 1; j <= bLength; j++) {
d[0][j] = j;
}

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

d[i][j] = Math.min(
Expand Down
3 changes: 1 addition & 2 deletions src/polyfills/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const find = Array.prototype.find
return Array.prototype.find.call(list, predicate);
}
: function(list, predicate) {
for (let i = 0; i < list.length; i++) {
const value = list[i];
for (const value of list) {
if (predicate(value)) {
return value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/polyfills/flatMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const flatMap = Array.prototype.flatMap
}
: function(list, fn) {
let result = [];
for (let i = 0; i < list.length; i++) {
const value = fn(list[i]);
for (const item of list) {
const value = fn(item);
if (Array.isArray(value)) {
result = result.concat(value);
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/validation/rules/OverlappingFieldsCanBeMerged.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,7 @@ function _collectFieldsAndFragmentNames(
nodeAndDefs,
fragmentNames,
): void {
for (let i = 0; i < selectionSet.selections.length; i++) {
const selection = selectionSet.selections[i];
for (const selection of selectionSet.selections) {
switch (selection.kind) {
case Kind.FIELD: {
const fieldName = selection.name.value;
Expand Down

0 comments on commit bb104d9

Please sign in to comment.