Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace some "for" cycles with "for of" #1910

Merged
merged 1 commit into from May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/jsutils/suggestionList.js
Expand Up @@ -16,13 +16,12 @@ export default function suggestionList(
options: $ReadOnlyArray<string>,
): Array<string> {
const optionsByDistance = Object.create(null);
const oLength = options.length;
const inputThreshold = input.length / 2;
for (let i = 0; i < oLength; i++) {
const distance = lexicalDistance(input, options[i]);
const threshold = Math.max(inputThreshold, options[i].length / 2, 1);
for (const option of options) {
const distance = lexicalDistance(input, option);
const threshold = Math.max(inputThreshold, option.length / 2, 1);
if (distance <= threshold) {
optionsByDistance[options[i]] = distance;
optionsByDistance[option] = distance;
}
}
return Object.keys(optionsByDistance).sort(
Expand Down
3 changes: 1 addition & 2 deletions src/utilities/astFromValue.js
Expand Up @@ -84,9 +84,8 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
if (value === null || typeof value !== 'object') {
return null;
}
const fields = objectValues(type.getFields());
const fieldNodes = [];
for (const field of fields) {
for (const field of objectValues(type.getFields())) {
const fieldValue = astFromValue(value[field.name], field.type);
if (fieldValue) {
fieldNodes.push({
Expand Down
3 changes: 1 addition & 2 deletions src/utilities/getOperationAST.js
Expand Up @@ -23,8 +23,7 @@ export function getOperationAST(
operationName: ?string,
): ?OperationDefinitionNode {
let operation = null;
for (let i = 0; i < documentAST.definitions.length; i++) {
const definition = documentAST.definitions[i];
for (const definition of documentAST.definitions) {
if (definition.kind === Kind.OPERATION_DEFINITION) {
if (!operationName) {
// If no operation name was provided, only return an Operation if there
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/schemaPrinter.js
Expand Up @@ -339,11 +339,11 @@ function printDescription(

function printDescriptionWithComments(lines, indentation, firstInBlock) {
let description = indentation && !firstInBlock ? '\n' : '';
for (let i = 0; i < lines.length; i++) {
if (lines[i] === '') {
for (const line of lines) {
if (line === '') {
description += indentation + '#\n';
} else {
description += indentation + '# ' + lines[i] + '\n';
description += indentation + '# ' + line + '\n';
}
}
return description;
Expand Down
11 changes: 4 additions & 7 deletions src/utilities/valueFromAST.js
Expand Up @@ -86,17 +86,16 @@ export function valueFromAST(
const itemType = type.ofType;
if (valueNode.kind === Kind.LIST) {
const coercedValues = [];
const itemNodes = valueNode.values;
for (let i = 0; i < itemNodes.length; i++) {
if (isMissingVariable(itemNodes[i], variables)) {
for (const itemNode of valueNode.values) {
if (isMissingVariable(itemNode, variables)) {
// If an array contains a missing variable, it is either coerced to
// null or if the item type is non-null, it considered invalid.
if (isNonNullType(itemType)) {
return; // Invalid: intentionally return no value.
}
coercedValues.push(null);
} else {
const itemValue = valueFromAST(itemNodes[i], itemType, variables);
const itemValue = valueFromAST(itemNode, itemType, variables);
if (isInvalid(itemValue)) {
return; // Invalid: intentionally return no value.
}
Expand All @@ -118,9 +117,7 @@ export function valueFromAST(
}
const coercedObj = Object.create(null);
const fieldNodes = keyMap(valueNode.fields, field => field.name.value);
const fields = objectValues(type.getFields());
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
for (const field of objectValues(type.getFields())) {
const fieldNode = fieldNodes[field.name];
if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
if (field.defaultValue !== undefined) {
Expand Down
16 changes: 5 additions & 11 deletions src/validation/ValidationContext.js
Expand Up @@ -97,8 +97,7 @@ export class ASTValidationContext {
const setsToVisit: Array<SelectionSetNode> = [node];
while (setsToVisit.length !== 0) {
const set = setsToVisit.pop();
for (let i = 0; i < set.selections.length; i++) {
const selection = set.selections[i];
for (const selection of set.selections) {
if (selection.kind === Kind.FRAGMENT_SPREAD) {
spreads.push(selection);
} else if (selection.selectionSet) {
Expand All @@ -121,9 +120,8 @@ export class ASTValidationContext {
const nodesToVisit: Array<SelectionSetNode> = [operation.selectionSet];
while (nodesToVisit.length !== 0) {
const node = nodesToVisit.pop();
const spreads = this.getFragmentSpreads(node);
for (let i = 0; i < spreads.length; i++) {
const fragName = spreads[i].name.value;
for (const spread of this.getFragmentSpreads(node)) {
const fragName = spread.name.value;
if (collectedNames[fragName] !== true) {
collectedNames[fragName] = true;
const fragment = this.getFragment(fragName);
Expand Down Expand Up @@ -212,12 +210,8 @@ export class ValidationContext extends ASTValidationContext {
let usages = this._recursiveVariableUsages.get(operation);
if (!usages) {
usages = this.getVariableUsages(operation);
const fragments = this.getRecursivelyReferencedFragments(operation);
for (let i = 0; i < fragments.length; i++) {
Array.prototype.push.apply(
usages,
this.getVariableUsages(fragments[i]),
);
for (const frag of this.getRecursivelyReferencedFragments(operation)) {
usages = usages.concat(this.getVariableUsages(frag));
}
this._recursiveVariableUsages.set(operation, usages);
}
Expand Down
3 changes: 1 addition & 2 deletions src/validation/rules/NoFragmentCycles.js
Expand Up @@ -57,8 +57,7 @@ export function NoFragmentCycles(context: ASTValidationContext): ASTVisitor {

spreadPathIndexByName[fragmentName] = spreadPath.length;

for (let i = 0; i < spreadNodes.length; i++) {
const spreadNode = spreadNodes[i];
for (const spreadNode of spreadNodes) {
const spreadName = spreadNode.name.value;
const cycleIndex = spreadPathIndexByName[spreadName];

Expand Down