From 698ccc988f0f27b847a293d2f25f949684dc10c9 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Tue, 15 Jun 2021 18:25:35 +0300 Subject: [PATCH] Switch tests & internal funcs to accept ReadonlyArrays I extracted all breaking changes into separate commit so this one contains only non-breaking changes, mostly tests and internal funcs --- .../__tests__/genFuzzStrings-test.ts | 2 +- src/__testUtils__/genFuzzStrings.ts | 2 +- src/__tests__/starWarsData.ts | 12 ++++++------ src/execution/__tests__/lists-test.ts | 2 +- src/execution/__tests__/union-interface-test.ts | 12 ++++++------ src/execution/execute.ts | 8 ++++---- src/jsutils/__tests__/suggestionList-test.ts | 2 +- src/jsutils/inspect.ts | 16 +++++++++++----- src/language/__tests__/blockString-test.ts | 2 +- src/type/__tests__/validation-test.ts | 8 ++++---- src/utilities/findBreakingChanges.ts | 6 +++--- src/utilities/separateOperations.ts | 2 +- 12 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/__testUtils__/__tests__/genFuzzStrings-test.ts b/src/__testUtils__/__tests__/genFuzzStrings-test.ts index c4961625e0..516ed00fe7 100644 --- a/src/__testUtils__/__tests__/genFuzzStrings-test.ts +++ b/src/__testUtils__/__tests__/genFuzzStrings-test.ts @@ -4,7 +4,7 @@ import { describe, it } from 'mocha'; import { genFuzzStrings } from '../genFuzzStrings'; function expectFuzzStrings(options: { - allowedChars: Array; + allowedChars: ReadonlyArray; maxLength: number; }) { return expect([...genFuzzStrings(options)]); diff --git a/src/__testUtils__/genFuzzStrings.ts b/src/__testUtils__/genFuzzStrings.ts index 9a9ffeac99..f29e1bb860 100644 --- a/src/__testUtils__/genFuzzStrings.ts +++ b/src/__testUtils__/genFuzzStrings.ts @@ -2,7 +2,7 @@ * Generator that produces all possible combinations of allowed characters. */ export function* genFuzzStrings(options: { - allowedChars: Array; + allowedChars: ReadonlyArray; maxLength: number; }): Generator { const { allowedChars, maxLength } = options; diff --git a/src/__tests__/starWarsData.ts b/src/__tests__/starWarsData.ts index 04cd648546..60c4331bb6 100644 --- a/src/__tests__/starWarsData.ts +++ b/src/__tests__/starWarsData.ts @@ -5,16 +5,16 @@ export interface Character { id: string; name: string; - friends: Array; - appearsIn: Array; + friends: ReadonlyArray; + appearsIn: ReadonlyArray; } export interface Human { type: 'Human'; id: string; name: string; - friends: Array; - appearsIn: Array; + friends: ReadonlyArray; + appearsIn: ReadonlyArray; homePlanet?: string; } @@ -22,8 +22,8 @@ export interface Droid { type: 'Droid'; id: string; name: string; - friends: Array; - appearsIn: Array; + friends: ReadonlyArray; + appearsIn: ReadonlyArray; primaryFunction: string; } diff --git a/src/execution/__tests__/lists-test.ts b/src/execution/__tests__/lists-test.ts index e5efd74db5..53314ecba8 100644 --- a/src/execution/__tests__/lists-test.ts +++ b/src/execution/__tests__/lists-test.ts @@ -37,7 +37,7 @@ describe('Execute: Accepts any iterable as list value', () => { }); it('Accepts function arguments as a List value', () => { - function getArgs(..._args: Array) { + function getArgs(..._args: ReadonlyArray) { return arguments; } const listField = getArgs('one', 'two'); diff --git a/src/execution/__tests__/union-interface-test.ts b/src/execution/__tests__/union-interface-test.ts index 7ff9bd72bb..987f35ddec 100644 --- a/src/execution/__tests__/union-interface-test.ts +++ b/src/execution/__tests__/union-interface-test.ts @@ -19,7 +19,7 @@ class Dog { barks: boolean; mother?: Dog; father?: Dog; - progeny: Array; + progeny: ReadonlyArray; constructor(name: string, barks: boolean) { this.name = name; @@ -33,7 +33,7 @@ class Cat { meows: boolean; mother?: Cat; father?: Cat; - progeny: Array; + progeny: ReadonlyArray; constructor(name: string, meows: boolean) { this.name = name; @@ -44,13 +44,13 @@ class Cat { class Person { name: string; - pets?: Array; - friends?: Array; + pets?: ReadonlyArray; + friends?: ReadonlyArray; constructor( name: string, - pets?: Array, - friends?: Array, + pets?: ReadonlyArray, + friends?: ReadonlyArray, ) { this.name = name; this.pets = pets; diff --git a/src/execution/execute.ts b/src/execution/execute.ts index 4b30409f98..985d3173ed 100644 --- a/src/execution/execute.ts +++ b/src/execution/execute.ts @@ -375,7 +375,7 @@ function executeFieldsSerially( parentType: GraphQLObjectType, sourceValue: unknown, path: Path | undefined, - fields: Map>, + fields: Map>, ): PromiseOrValue> { return promiseReduce( fields.entries(), @@ -413,7 +413,7 @@ function executeFields( parentType: GraphQLObjectType, sourceValue: unknown, path: Path | undefined, - fields: Map>, + fields: Map>, ): PromiseOrValue> { const results = Object.create(null); let containsPromise = false; @@ -463,7 +463,7 @@ export function collectFields( selectionSet: SelectionSetNode, fields: Map>, visitedFragmentNames: Set, -): Map> { +): Map> { for (const selection of selectionSet.selections) { switch (selection.kind) { case Kind.FIELD: { @@ -1075,7 +1075,7 @@ function _collectSubfields( exeContext: ExecutionContext, returnType: GraphQLObjectType, fieldNodes: ReadonlyArray, -): Map> { +): Map> { let subFieldNodes = new Map(); const visitedFragmentNames = new Set(); for (const node of fieldNodes) { diff --git a/src/jsutils/__tests__/suggestionList-test.ts b/src/jsutils/__tests__/suggestionList-test.ts index 3e2037950d..2b90524885 100644 --- a/src/jsutils/__tests__/suggestionList-test.ts +++ b/src/jsutils/__tests__/suggestionList-test.ts @@ -3,7 +3,7 @@ import { describe, it } from 'mocha'; import { suggestionList } from '../suggestionList'; -function expectSuggestions(input: string, options: Array) { +function expectSuggestions(input: string, options: ReadonlyArray) { return expect(suggestionList(input, options)); } diff --git a/src/jsutils/inspect.ts b/src/jsutils/inspect.ts index 9076490de0..514cbaad39 100644 --- a/src/jsutils/inspect.ts +++ b/src/jsutils/inspect.ts @@ -8,7 +8,10 @@ export function inspect(value: unknown): string { return formatValue(value, []); } -function formatValue(value: unknown, seenValues: Array): string { +function formatValue( + value: unknown, + seenValues: ReadonlyArray, +): string { switch (typeof value) { case 'string': return JSON.stringify(value); @@ -23,7 +26,7 @@ function formatValue(value: unknown, seenValues: Array): string { function formatObjectValue( value: object | null, - previouslySeenValues: Array, + previouslySeenValues: ReadonlyArray, ): string { if (value === null) { return 'null'; @@ -55,7 +58,10 @@ function isJSONable(value: any): value is { toJSON: () => unknown } { return typeof value.toJSON === 'function'; } -function formatObject(object: object, seenValues: Array): string { +function formatObject( + object: object, + seenValues: ReadonlyArray, +): string { const entries = Object.entries(object); if (entries.length === 0) { return '{}'; @@ -72,8 +78,8 @@ function formatObject(object: object, seenValues: Array): string { } function formatArray( - array: Array, - seenValues: Array, + array: ReadonlyArray, + seenValues: ReadonlyArray, ): string { if (array.length === 0) { return '[]'; diff --git a/src/language/__tests__/blockString-test.ts b/src/language/__tests__/blockString-test.ts index 3211dae0fb..9f0022fe70 100644 --- a/src/language/__tests__/blockString-test.ts +++ b/src/language/__tests__/blockString-test.ts @@ -7,7 +7,7 @@ import { printBlockString, } from '../blockString'; -function joinLines(...args: Array) { +function joinLines(...args: ReadonlyArray) { return args.join('\n'); } diff --git a/src/type/__tests__/validation-test.ts b/src/type/__tests__/validation-test.ts index 6d8ef8f789..b36021055b 100644 --- a/src/type/__tests__/validation-test.ts +++ b/src/type/__tests__/validation-test.ts @@ -79,7 +79,7 @@ function withModifiers( ]; } -const outputTypes: Array = [ +const outputTypes: ReadonlyArray = [ ...withModifiers(GraphQLString), ...withModifiers(SomeScalarType), ...withModifiers(SomeEnumType), @@ -88,18 +88,18 @@ const outputTypes: Array = [ ...withModifiers(SomeInterfaceType), ]; -const notOutputTypes: Array = [ +const notOutputTypes: ReadonlyArray = [ ...withModifiers(SomeInputObjectType), ]; -const inputTypes: Array = [ +const inputTypes: ReadonlyArray = [ ...withModifiers(GraphQLString), ...withModifiers(SomeScalarType), ...withModifiers(SomeEnumType), ...withModifiers(SomeInputObjectType), ]; -const notInputTypes: Array = [ +const notInputTypes: ReadonlyArray = [ ...withModifiers(SomeObjectType), ...withModifiers(SomeUnionType), ...withModifiers(SomeInterfaceType), diff --git a/src/utilities/findBreakingChanges.ts b/src/utilities/findBreakingChanges.ts index 0da4cceaa7..7aa0760481 100644 --- a/src/utilities/findBreakingChanges.ts +++ b/src/utilities/findBreakingChanges.ts @@ -557,9 +557,9 @@ function diff( oldArray: ReadonlyArray, newArray: ReadonlyArray, ): { - added: Array; - removed: Array; - persisted: Array<[T, T]>; + added: ReadonlyArray; + removed: ReadonlyArray; + persisted: ReadonlyArray<[T, T]>; } { const added: Array = []; const removed: Array = []; diff --git a/src/utilities/separateOperations.ts b/src/utilities/separateOperations.ts index 18884059e9..ece8c2739d 100644 --- a/src/utilities/separateOperations.ts +++ b/src/utilities/separateOperations.ts @@ -63,7 +63,7 @@ export function separateOperations( return separatedDocumentASTs; } -type DepGraph = ObjMap>; +type DepGraph = ObjMap>; // From a dependency graph, collects a list of transitive dependencies by // recursing through a dependency graph.