Skip to content

Commit e6c36e0

Browse files
IvanGoncharovmjmahone
authored andcommitted
Replace all 'Array.forEach' with 'for of' cycle (#1423)
1 parent 4124b61 commit e6c36e0

23 files changed

+229
-226
lines changed

src/error/printError.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { GraphQLError } from './GraphQLError';
1919
export function printError(error: GraphQLError): string {
2020
const printedLocations = [];
2121
if (error.nodes) {
22-
error.nodes.forEach(node => {
22+
for (const node of error.nodes) {
2323
if (node.loc) {
2424
printedLocations.push(
2525
highlightSourceAtLocation(
@@ -28,12 +28,12 @@ export function printError(error: GraphQLError): string {
2828
),
2929
);
3030
}
31-
});
31+
}
3232
} else if (error.source && error.locations) {
3333
const source = error.source;
34-
error.locations.forEach(location => {
34+
for (const location of error.locations) {
3535
printedLocations.push(highlightSourceAtLocation(source, location));
36-
});
36+
}
3737
}
3838
return printedLocations.length === 0
3939
? error.message

src/execution/values.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ export function getVariableValues(
9696
const coerced = coerceValue(value, varType, varDefNode);
9797
const coercionErrors = coerced.errors;
9898
if (coercionErrors) {
99-
coercionErrors.forEach(error => {
99+
for (const error of coercionErrors) {
100100
error.message =
101101
`Variable "$${varName}" got invalid ` +
102102
`value ${inspect(value)}; ${error.message}`;
103-
});
103+
}
104104
errors.push(...coercionErrors);
105105
} else {
106106
coercedValues[varName] = coerced.value;

src/language/__tests__/parser-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('Parser', () => {
151151
'true',
152152
'false',
153153
];
154-
nonKeywords.forEach(keyword => {
154+
for (const keyword of nonKeywords) {
155155
// You can't define or reference a fragment named `on`.
156156
const fragmentName = keyword !== 'on' ? keyword : 'a';
157157
const document = `
@@ -166,7 +166,7 @@ describe('Parser', () => {
166166
`;
167167

168168
expect(() => parse(document)).to.not.throw();
169-
});
169+
}
170170
});
171171

172172
it('parses anonymous mutation operations', () => {

src/subscription/__tests__/eventEmitterAsyncIterator.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export default function eventEmitterAsyncIterator(
4545
if (listening) {
4646
listening = false;
4747
eventEmitter.removeListener(eventName, pushValue);
48-
pullQueue.forEach(resolve => resolve({ value: undefined, done: true }));
48+
for (const resolve of pullQueue) {
49+
resolve({ value: undefined, done: true });
50+
}
4951
pullQueue.length = 0;
5052
pushQueue.length = 0;
5153
}

src/type/__tests__/definition-test.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,11 @@ describe('Type System: Example', () => {
378378
[EnumType, true],
379379
[InputObjectType, true],
380380
];
381-
expected.forEach(([type, answer]) => {
381+
for (const [type, answer] of expected) {
382382
expect(isInputType(type)).to.equal(answer);
383383
expect(isInputType(GraphQLList(type))).to.equal(answer);
384384
expect(isInputType(GraphQLNonNull(type))).to.equal(answer);
385-
});
385+
}
386386
});
387387

388388
it('identifies output types', () => {
@@ -394,11 +394,11 @@ describe('Type System: Example', () => {
394394
[EnumType, true],
395395
[InputObjectType, false],
396396
];
397-
expected.forEach(([type, answer]) => {
397+
for (const [type, answer] of expected) {
398398
expect(isOutputType(type)).to.equal(answer);
399399
expect(isOutputType(GraphQLList(type))).to.equal(answer);
400400
expect(isOutputType(GraphQLNonNull(type))).to.equal(answer);
401-
});
401+
}
402402
});
403403

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

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

1134-
types.forEach(type => {
1134+
for (const type of types) {
11351135
it(`accepts an type as item type of list: ${type}`, () => {
11361136
expect(() => GraphQLList(type)).not.to.throw();
11371137
});
1138-
});
1138+
}
11391139

1140-
notTypes.forEach(type => {
1140+
for (const type of notTypes) {
11411141
it(`rejects a non-type as item type of list: ${type}`, () => {
11421142
expect(() => GraphQLList(type)).to.throw(
11431143
`Expected ${inspect(type)} to be a GraphQL type.`,
11441144
);
11451145
});
1146-
});
1146+
}
11471147
});
11481148

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

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

1176-
notNullableTypes.forEach(type => {
1176+
for (const type of notNullableTypes) {
11771177
it(`rejects a non-type as nullable type of non-null: ${type}`, () => {
11781178
expect(() => GraphQLNonNull(type)).to.throw(
11791179
`Expected ${inspect(type)} to be a GraphQL nullable type.`,
11801180
);
11811181
});
1182-
});
1182+
}
11831183
});
11841184

11851185
describe('Type System: A Schema must contain uniquely named types', () => {

src/type/__tests__/validation-test.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ describe('Type System: Union types must be valid', () => {
641641
SomeEnumType,
642642
SomeInputObjectType,
643643
];
644-
badUnionMemberTypes.forEach(memberType => {
644+
for (const memberType of badUnionMemberTypes) {
645645
const badSchema = schemaWithFieldType(
646646
new GraphQLUnionType({ name: 'BadUnion', types: [memberType] }),
647647
);
@@ -652,7 +652,7 @@ describe('Type System: Union types must be valid', () => {
652652
`it cannot include ${memberType}.`,
653653
},
654654
]);
655-
});
655+
}
656656
});
657657
});
658658

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

832-
outputTypes.forEach(type => {
832+
for (const type of outputTypes) {
833833
it(`accepts an output type as an Object field type: ${type}`, () => {
834834
const schema = schemaWithObjectFieldOfType(type);
835835
expect(validateSchema(schema)).to.deep.equal([]);
836836
});
837-
});
837+
}
838838

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

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

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

1126-
outputTypes.forEach(type => {
1126+
for (const type of outputTypes) {
11271127
it(`accepts an output type as an Interface field type: ${type}`, () => {
11281128
const schema = schemaWithInterfaceFieldOfType(type);
11291129
expect(validateSchema(schema)).to.deep.equal([]);
11301130
});
1131-
});
1131+
}
11321132

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

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

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

1246-
inputTypes.forEach(type => {
1246+
for (const type of inputTypes) {
12471247
it(`accepts an input type as a field arg type: ${type}`, () => {
12481248
const schema = schemaWithArgOfType(type);
12491249
expect(validateSchema(schema)).to.deep.equal([]);
12501250
});
1251-
});
1251+
}
12521252

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

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

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

1330-
inputTypes.forEach(type => {
1330+
for (const type of inputTypes) {
13311331
it(`accepts an input type as an input field type: ${type}`, () => {
13321332
const schema = schemaWithInputFieldOfType(type);
13331333
expect(validateSchema(schema)).to.deep.equal([]);
13341334
});
1335-
});
1335+
}
13361336

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

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

13581358
it('rejects a non-type value as an input field type', () => {
13591359
const schema = schemaWithInputFieldOfType(Number);

src/type/definition.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ function defineFieldMap<TSource, TContext>(
707707
);
708708

709709
const resultFieldMap = Object.create(null);
710-
Object.keys(fieldMap).forEach(fieldName => {
710+
for (const fieldName of Object.keys(fieldMap)) {
711711
const fieldConfig = fieldMap[fieldName];
712712
invariant(
713713
isPlainObj(fieldConfig),
@@ -749,7 +749,7 @@ function defineFieldMap<TSource, TContext>(
749749
});
750750
}
751751
resultFieldMap[fieldName] = field;
752-
});
752+
}
753753
return resultFieldMap;
754754
}
755755

@@ -1241,7 +1241,7 @@ function defineInputFieldMap(
12411241
'function which returns such an object.',
12421242
);
12431243
const resultFieldMap = Object.create(null);
1244-
Object.keys(fieldMap).forEach(fieldName => {
1244+
for (const fieldName of Object.keys(fieldMap)) {
12451245
const field = {
12461246
...fieldMap[fieldName],
12471247
name: fieldName,
@@ -1252,7 +1252,7 @@ function defineInputFieldMap(
12521252
'Input Types cannot define resolvers.',
12531253
);
12541254
resultFieldMap[fieldName] = field;
1255-
});
1255+
}
12561256
return resultFieldMap;
12571257
}
12581258

src/type/schema.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ export class GraphQLSchema {
155155

156156
// Keep track of all implementations by interface name.
157157
this._implementations = Object.create(null);
158-
Object.keys(this._typeMap).forEach(typeName => {
158+
for (const typeName of Object.keys(this._typeMap)) {
159159
const type = this._typeMap[typeName];
160160
if (isObjectType(type)) {
161-
type.getInterfaces().forEach(iface => {
161+
for (const iface of type.getInterfaces()) {
162162
if (isInterfaceType(iface)) {
163163
const impls = this._implementations[iface.name];
164164
if (impls) {
@@ -167,11 +167,11 @@ export class GraphQLSchema {
167167
this._implementations[iface.name] = [type];
168168
}
169169
}
170-
});
170+
}
171171
} else if (isAbstractType(type) && !this._implementations[type.name]) {
172172
this._implementations[type.name] = [];
173173
}
174-
});
174+
}
175175
}
176176

177177
getQueryType(): ?GraphQLObjectType {
@@ -296,19 +296,19 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
296296
}
297297

298298
if (isObjectType(type) || isInterfaceType(type)) {
299-
objectValues(type.getFields()).forEach(field => {
299+
for (const field of objectValues(type.getFields())) {
300300
if (field.args) {
301301
const fieldArgTypes = field.args.map(arg => arg.type);
302302
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
303303
}
304304
reducedMap = typeMapReducer(reducedMap, field.type);
305-
});
305+
}
306306
}
307307

308308
if (isInputObjectType(type)) {
309-
objectValues(type.getFields()).forEach(field => {
309+
for (const field of objectValues(type.getFields())) {
310310
reducedMap = typeMapReducer(reducedMap, field.type);
311-
});
311+
}
312312
}
313313

314314
return reducedMap;

src/utilities/astFromValue.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
8686
}
8787
const fields = objectValues(type.getFields());
8888
const fieldNodes = [];
89-
fields.forEach(field => {
89+
for (const field of fields) {
9090
const fieldValue = astFromValue(value[field.name], field.type);
9191
if (fieldValue) {
9292
fieldNodes.push({
@@ -95,7 +95,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
9595
value: fieldValue,
9696
});
9797
}
98-
});
98+
}
9999
return { kind: Kind.OBJECT, fields: fieldNodes };
100100
}
101101

0 commit comments

Comments
 (0)