Skip to content

Commit

Permalink
Replace all 'Array.forEach' with 'for of' cycle (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored and mjmahone committed Jul 17, 2018
1 parent 4124b61 commit e6c36e0
Show file tree
Hide file tree
Showing 23 changed files with 229 additions and 226 deletions.
8 changes: 4 additions & 4 deletions src/error/printError.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { GraphQLError } from './GraphQLError';
export function printError(error: GraphQLError): string {
const printedLocations = [];
if (error.nodes) {
error.nodes.forEach(node => {
for (const node of error.nodes) {
if (node.loc) {
printedLocations.push(
highlightSourceAtLocation(
Expand All @@ -28,12 +28,12 @@ export function printError(error: GraphQLError): string {
),
);
}
});
}
} else if (error.source && error.locations) {
const source = error.source;
error.locations.forEach(location => {
for (const location of error.locations) {
printedLocations.push(highlightSourceAtLocation(source, location));
});
}
}
return printedLocations.length === 0
? error.message
Expand Down
4 changes: 2 additions & 2 deletions src/execution/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ export function getVariableValues(
const coerced = coerceValue(value, varType, varDefNode);
const coercionErrors = coerced.errors;
if (coercionErrors) {
coercionErrors.forEach(error => {
for (const error of coercionErrors) {
error.message =
`Variable "$${varName}" got invalid ` +
`value ${inspect(value)}; ${error.message}`;
});
}
errors.push(...coercionErrors);
} else {
coercedValues[varName] = coerced.value;
Expand Down
4 changes: 2 additions & 2 deletions src/language/__tests__/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('Parser', () => {
'true',
'false',
];
nonKeywords.forEach(keyword => {
for (const keyword of nonKeywords) {
// You can't define or reference a fragment named `on`.
const fragmentName = keyword !== 'on' ? keyword : 'a';
const document = `
Expand All @@ -166,7 +166,7 @@ describe('Parser', () => {
`;

expect(() => parse(document)).to.not.throw();
});
}
});

it('parses anonymous mutation operations', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/subscription/__tests__/eventEmitterAsyncIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export default function eventEmitterAsyncIterator(
if (listening) {
listening = false;
eventEmitter.removeListener(eventName, pushValue);
pullQueue.forEach(resolve => resolve({ value: undefined, done: true }));
for (const resolve of pullQueue) {
resolve({ value: undefined, done: true });
}
pullQueue.length = 0;
pushQueue.length = 0;
}
Expand Down
24 changes: 12 additions & 12 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,11 @@ describe('Type System: Example', () => {
[EnumType, true],
[InputObjectType, true],
];
expected.forEach(([type, answer]) => {
for (const [type, answer] of expected) {
expect(isInputType(type)).to.equal(answer);
expect(isInputType(GraphQLList(type))).to.equal(answer);
expect(isInputType(GraphQLNonNull(type))).to.equal(answer);
});
}
});

it('identifies output types', () => {
Expand All @@ -394,11 +394,11 @@ describe('Type System: Example', () => {
[EnumType, true],
[InputObjectType, false],
];
expected.forEach(([type, answer]) => {
for (const [type, answer] of expected) {
expect(isOutputType(type)).to.equal(answer);
expect(isOutputType(GraphQLList(type))).to.equal(answer);
expect(isOutputType(GraphQLNonNull(type))).to.equal(answer);
});
}
});

it('prohibits nesting NonNull inside NonNull', () => {
Expand Down Expand Up @@ -1131,19 +1131,19 @@ describe('Type System: List must accept only types', () => {

const notTypes = [{}, String, undefined, null];

types.forEach(type => {
for (const type of types) {
it(`accepts an type as item type of list: ${type}`, () => {
expect(() => GraphQLList(type)).not.to.throw();
});
});
}

notTypes.forEach(type => {
for (const type of notTypes) {
it(`rejects a non-type as item type of list: ${type}`, () => {
expect(() => GraphQLList(type)).to.throw(
`Expected ${inspect(type)} to be a GraphQL type.`,
);
});
});
}
});

describe('Type System: NonNull must only accept non-nullable types', () => {
Expand All @@ -1167,19 +1167,19 @@ describe('Type System: NonNull must only accept non-nullable types', () => {
null,
];

nullableTypes.forEach(type => {
for (const type of nullableTypes) {
it(`accepts an type as nullable type of non-null: ${type}`, () => {
expect(() => GraphQLNonNull(type)).not.to.throw();
});
});
}

notNullableTypes.forEach(type => {
for (const type of notNullableTypes) {
it(`rejects a non-type as nullable type of non-null: ${type}`, () => {
expect(() => GraphQLNonNull(type)).to.throw(
`Expected ${inspect(type)} to be a GraphQL nullable type.`,
);
});
});
}
});

describe('Type System: A Schema must contain uniquely named types', () => {
Expand Down
36 changes: 18 additions & 18 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ describe('Type System: Union types must be valid', () => {
SomeEnumType,
SomeInputObjectType,
];
badUnionMemberTypes.forEach(memberType => {
for (const memberType of badUnionMemberTypes) {
const badSchema = schemaWithFieldType(
new GraphQLUnionType({ name: 'BadUnion', types: [memberType] }),
);
Expand All @@ -652,7 +652,7 @@ describe('Type System: Union types must be valid', () => {
`it cannot include ${memberType}.`,
},
]);
});
}
});
});

Expand Down Expand Up @@ -829,12 +829,12 @@ describe('Type System: Object fields must have output types', () => {
});
}

outputTypes.forEach(type => {
for (const type of outputTypes) {
it(`accepts an output type as an Object field type: ${type}`, () => {
const schema = schemaWithObjectFieldOfType(type);
expect(validateSchema(schema)).to.deep.equal([]);
});
});
}

it('rejects an empty Object field type', () => {
const schema = schemaWithObjectFieldOfType(undefined);
Expand All @@ -846,7 +846,7 @@ describe('Type System: Object fields must have output types', () => {
]);
});

notOutputTypes.forEach(type => {
for (const type of notOutputTypes) {
it(`rejects a non-output type as an Object field type: ${type}`, () => {
const schema = schemaWithObjectFieldOfType(type);
expect(validateSchema(schema)).to.deep.equal([
Expand All @@ -855,7 +855,7 @@ describe('Type System: Object fields must have output types', () => {
},
]);
});
});
}

it('rejects a non-type value as an Object field type', () => {
const schema = schemaWithObjectFieldOfType(Number);
Expand Down Expand Up @@ -1123,12 +1123,12 @@ describe('Type System: Interface fields must have output types', () => {
});
}

outputTypes.forEach(type => {
for (const type of outputTypes) {
it(`accepts an output type as an Interface field type: ${type}`, () => {
const schema = schemaWithInterfaceFieldOfType(type);
expect(validateSchema(schema)).to.deep.equal([]);
});
});
}

it('rejects an empty Interface field type', () => {
const schema = schemaWithInterfaceFieldOfType(undefined);
Expand All @@ -1144,7 +1144,7 @@ describe('Type System: Interface fields must have output types', () => {
]);
});

notOutputTypes.forEach(type => {
for (const type of notOutputTypes) {
it(`rejects a non-output type as an Interface field type: ${type}`, () => {
const schema = schemaWithInterfaceFieldOfType(type);
expect(validateSchema(schema)).to.deep.equal([
Expand All @@ -1156,7 +1156,7 @@ describe('Type System: Interface fields must have output types', () => {
},
]);
});
});
}

it('rejects a non-type value as an Interface field type', () => {
const schema = schemaWithInterfaceFieldOfType(Number);
Expand Down Expand Up @@ -1243,12 +1243,12 @@ describe('Type System: Field arguments must have input types', () => {
});
}

inputTypes.forEach(type => {
for (const type of inputTypes) {
it(`accepts an input type as a field arg type: ${type}`, () => {
const schema = schemaWithArgOfType(type);
expect(validateSchema(schema)).to.deep.equal([]);
});
});
}

it('rejects an empty field arg type', () => {
const schema = schemaWithArgOfType(undefined);
Expand All @@ -1260,7 +1260,7 @@ describe('Type System: Field arguments must have input types', () => {
]);
});

notInputTypes.forEach(type => {
for (const type of notInputTypes) {
it(`rejects a non-input type as a field arg type: ${type}`, () => {
const schema = schemaWithArgOfType(type);
expect(validateSchema(schema)).to.deep.equal([
Expand All @@ -1269,7 +1269,7 @@ describe('Type System: Field arguments must have input types', () => {
},
]);
});
});
}

it('rejects a non-type value as a field arg type', () => {
const schema = schemaWithArgOfType(Number);
Expand Down Expand Up @@ -1327,12 +1327,12 @@ describe('Type System: Input Object fields must have input types', () => {
});
}

inputTypes.forEach(type => {
for (const type of inputTypes) {
it(`accepts an input type as an input field type: ${type}`, () => {
const schema = schemaWithInputFieldOfType(type);
expect(validateSchema(schema)).to.deep.equal([]);
});
});
}

it('rejects an empty input field type', () => {
const schema = schemaWithInputFieldOfType(undefined);
Expand All @@ -1344,7 +1344,7 @@ describe('Type System: Input Object fields must have input types', () => {
]);
});

notInputTypes.forEach(type => {
for (const type of notInputTypes) {
it(`rejects a non-input type as an input field type: ${type}`, () => {
const schema = schemaWithInputFieldOfType(type);
expect(validateSchema(schema)).to.deep.equal([
Expand All @@ -1353,7 +1353,7 @@ describe('Type System: Input Object fields must have input types', () => {
},
]);
});
});
}

it('rejects a non-type value as an input field type', () => {
const schema = schemaWithInputFieldOfType(Number);
Expand Down
8 changes: 4 additions & 4 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ function defineFieldMap<TSource, TContext>(
);

const resultFieldMap = Object.create(null);
Object.keys(fieldMap).forEach(fieldName => {
for (const fieldName of Object.keys(fieldMap)) {
const fieldConfig = fieldMap[fieldName];
invariant(
isPlainObj(fieldConfig),
Expand Down Expand Up @@ -749,7 +749,7 @@ function defineFieldMap<TSource, TContext>(
});
}
resultFieldMap[fieldName] = field;
});
}
return resultFieldMap;
}

Expand Down Expand Up @@ -1241,7 +1241,7 @@ function defineInputFieldMap(
'function which returns such an object.',
);
const resultFieldMap = Object.create(null);
Object.keys(fieldMap).forEach(fieldName => {
for (const fieldName of Object.keys(fieldMap)) {
const field = {
...fieldMap[fieldName],
name: fieldName,
Expand All @@ -1252,7 +1252,7 @@ function defineInputFieldMap(
'Input Types cannot define resolvers.',
);
resultFieldMap[fieldName] = field;
});
}
return resultFieldMap;
}

Expand Down
16 changes: 8 additions & 8 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ export class GraphQLSchema {

// Keep track of all implementations by interface name.
this._implementations = Object.create(null);
Object.keys(this._typeMap).forEach(typeName => {
for (const typeName of Object.keys(this._typeMap)) {
const type = this._typeMap[typeName];
if (isObjectType(type)) {
type.getInterfaces().forEach(iface => {
for (const iface of type.getInterfaces()) {
if (isInterfaceType(iface)) {
const impls = this._implementations[iface.name];
if (impls) {
Expand All @@ -167,11 +167,11 @@ export class GraphQLSchema {
this._implementations[iface.name] = [type];
}
}
});
}
} else if (isAbstractType(type) && !this._implementations[type.name]) {
this._implementations[type.name] = [];
}
});
}
}

getQueryType(): ?GraphQLObjectType {
Expand Down Expand Up @@ -296,19 +296,19 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
}

if (isObjectType(type) || isInterfaceType(type)) {
objectValues(type.getFields()).forEach(field => {
for (const field of objectValues(type.getFields())) {
if (field.args) {
const fieldArgTypes = field.args.map(arg => arg.type);
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
}
reducedMap = typeMapReducer(reducedMap, field.type);
});
}
}

if (isInputObjectType(type)) {
objectValues(type.getFields()).forEach(field => {
for (const field of objectValues(type.getFields())) {
reducedMap = typeMapReducer(reducedMap, field.type);
});
}
}

return reducedMap;
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/astFromValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
}
const fields = objectValues(type.getFields());
const fieldNodes = [];
fields.forEach(field => {
for (const field of fields) {
const fieldValue = astFromValue(value[field.name], field.type);
if (fieldValue) {
fieldNodes.push({
Expand All @@ -95,7 +95,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
value: fieldValue,
});
}
});
}
return { kind: Kind.OBJECT, fields: fieldNodes };
}

Expand Down
Loading

0 comments on commit e6c36e0

Please sign in to comment.