Skip to content

Commit

Permalink
Remove superficial .values() and .entries() calls (#3689)
Browse files Browse the repository at this point in the history
Motivation: Spotted during investigation of unrelated issue.
Maps and sets are iteratable, so no need to call these methods.
  • Loading branch information
IvanGoncharov authored Aug 9, 2022
1 parent 62a08fa commit 7f5fe4d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function executeFieldsSerially(
fields: Map<string, ReadonlyArray<FieldNode>>,
): PromiseOrValue<ObjMap<unknown>> {
return promiseReduce(
fields.entries(),
fields,
(results, [responseName, fieldNodes]) => {
const fieldPath = addPath(path, responseName, parentType.name);
const result = executeField(
Expand Down Expand Up @@ -431,7 +431,7 @@ function executeFields(
const results = Object.create(null);
let containsPromise = false;

for (const [responseName, fieldNodes] of fields.entries()) {
for (const [responseName, fieldNodes] of fields) {
const fieldPath = addPath(path, responseName, parentType.name);
const result = executeField(
exeContext,
Expand Down Expand Up @@ -1227,7 +1227,9 @@ function executeSubscription(
rootType,
operation.selectionSet,
);
const [responseName, fieldNodes] = [...rootFields.entries()][0];

const firstRootField = rootFields.entries().next().value;
const [responseName, fieldNodes] = firstRootField;
const fieldName = fieldNodes[0].name.value;
const fieldDef = schema.getField(rootType, fieldName);

Expand Down
2 changes: 1 addition & 1 deletion src/jsutils/__tests__/AccumulatorMap-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it } from 'mocha';
import { AccumulatorMap } from '../AccumulatorMap';

function expectMap<K, V>(map: Map<K, V>) {
return expect(Object.fromEntries(map.entries()));
return expect(Object.fromEntries(map));
}

describe('AccumulatorMap', () => {
Expand Down
9 changes: 6 additions & 3 deletions src/jsutils/promiseForObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import type { ObjMap } from './ObjMap';
export function promiseForObject<T>(
object: ObjMap<Promise<T>>,
): Promise<ObjMap<T>> {
return Promise.all(Object.values(object)).then((resolvedValues) => {
const keys = Object.keys(object);
const values = Object.values(object);

return Promise.all(values).then((resolvedValues) => {
const resolvedObject = Object.create(null);
for (const [i, key] of Object.keys(object).entries()) {
resolvedObject[key] = resolvedValues[i];
for (let i = 0; i < keys.length; ++i) {
resolvedObject[keys[i]] = resolvedValues[i];
}
return resolvedObject;
});
Expand Down
2 changes: 1 addition & 1 deletion src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function validateRootTypes(context: SchemaValidationContext): void {
}
}

for (const [rootType, operationTypes] of rootTypesMap.entries()) {
for (const [rootType, operationTypes] of rootTypesMap) {
if (operationTypes.length > 1) {
const operationList = andList(operationTypes);
context.reportError(
Expand Down
3 changes: 1 addition & 2 deletions src/validation/rules/SingleFieldSubscriptionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export function SingleFieldSubscriptionsRule(
);
}
for (const fieldNodes of fields.values()) {
const field = fieldNodes[0];
const fieldName = field.name.value;
const fieldName = fieldNodes[0].name.value;
if (fieldName.startsWith('__')) {
context.reportError(
new GraphQLError(
Expand Down

0 comments on commit 7f5fe4d

Please sign in to comment.