Skip to content

Commit caa8334

Browse files
committed
convert ? (flow) to Maybe (TS)
1 parent e3cd073 commit caa8334

File tree

12 files changed

+34
-24
lines changed

12 files changed

+34
-24
lines changed

src/__testUtils__/inspectStr.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Maybe } from '../jsutils/Maybe';
2+
13
/**
24
* Special inspect function to produce readable string literal for error messages in tests
35
*/
4-
export default function inspectStr(str: ?string): string {
6+
export default function inspectStr(str: Maybe<string>): string {
57
if (str == null) {
68
return 'null';
79
}

src/error/GraphQLError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class GraphQLError extends Error {
6868
/**
6969
* The original error thrown from a field resolver during execution.
7070
*/
71-
readonly originalError: ?Error;
71+
readonly originalError: Maybe<Error>;
7272

7373
/**
7474
* Extension fields to add to the formatted error.

src/execution/values.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { isInputType, isNonNullType } from '../type/definition';
2121
import { typeFromAST } from '../utilities/typeFromAST';
2222
import { valueFromAST } from '../utilities/valueFromAST';
2323
import { coerceInputValue } from '../utilities/coerceInputValue';
24+
import { Maybe } from '../jsutils/Maybe';
2425

2526
type CoercedVariableValues =
2627
| { errors: ReadonlyArray<GraphQLError> }
@@ -245,7 +246,7 @@ export function getArgumentValues(
245246
export function getDirectiveValues(
246247
directiveDef: GraphQLDirective,
247248
node: { readonly directives?: ReadonlyArray<DirectiveNode> },
248-
variableValues?: ?ObjMap<unknown>,
249+
variableValues?: Maybe<ObjMap<unknown>>,
249250
): void | { [argument: string]: unknown } {
250251
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
251252
const directiveNode = node.directives?.find(

src/jsutils/Path.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Maybe } from './Maybe';
2+
13
export type Path = {
24
readonly prev: Path | void;
35
readonly key: string | number;
@@ -18,7 +20,7 @@ export function addPath(
1820
/**
1921
* Given a Path, return an Array of the path keys.
2022
*/
21-
export function pathToArray(path: ?Readonly<Path>): Array<string | number> {
23+
export function pathToArray(path: Maybe<Readonly<Path>>): Array<string | number> {
2224
const flattened = [];
2325
let curr = path;
2426
while (curr) {

src/language/experimentalOnlineParser/grammar.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Maybe } from "../../jsutils/Maybe";
2+
13
export type GraphQLGrammarType = {
24
[name: string]: GraphQLGrammarRule,
35
};
@@ -16,8 +18,8 @@ export type GraphQLGrammarRule =
1618
| GraphQLGrammarConstraintsSet;
1719
export interface GraphQLGrammarBaseRuleConstraint {
1820
butNot?:
19-
| ?GraphQLGrammarTokenConstraint
20-
| ?Array<GraphQLGrammarTokenConstraint>;
21+
| Maybe<GraphQLGrammarTokenConstraint>
22+
| Maybe<Array<GraphQLGrammarTokenConstraint>>;
2123
optional?: boolean;
2224
eatNextOnFail?: boolean;
2325
}
@@ -44,8 +46,8 @@ export interface GraphQLGrammarTokenConstraint
4446
| 'String'
4547
| 'BlockString'
4648
| 'Comment';
47-
ofValue?: ?string;
48-
oneOf?: ?Array<string>;
49+
ofValue?: Maybe<string>;
50+
oneOf?: Maybe<Array<string>>;
4951
tokenName?: string;
5052
definitionName?: boolean;
5153
typeName?: boolean;

src/language/experimentalOnlineParser/onlineParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type OnlineParserConfig = {
101101
};
102102

103103
type OnlineParserConfigOption = {
104-
tabSize: ?number,
104+
tabSize: Maybe<number>,
105105
};
106106

107107
export class OnlineParser {
@@ -453,7 +453,7 @@ export class OnlineParser {
453453
return this.state.rules[this.state.rules.length - 1] || null;
454454
}
455455

456-
_popMatchedRule(token: ?Token) {
456+
_popMatchedRule(token: Maybe<Token>) {
457457
const rule = this.state.rules.pop();
458458
if (!rule) {
459459
return;

src/language/printer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ASTNode } from './ast';
22

33
import { visit } from './visitor';
44
import { printBlockString } from './blockString';
5+
import { Maybe } from '../jsutils/Maybe';
56

67
/**
78
* Converts an AST into a string, using one set of reasonable
@@ -257,22 +258,22 @@ function addDescription(cb) {
257258
* Given maybeArray, print an empty string if it is null or empty, otherwise
258259
* print all items together separated by separator if provided
259260
*/
260-
function join(maybeArray: ?Array<string>, separator = ''): string {
261+
function join(maybeArray: Maybe<Array<string>>, separator = ''): string {
261262
return maybeArray?.filter((x) => x).join(separator) ?? '';
262263
}
263264

264265
/**
265266
* Given array, print each item on its own line, wrapped in an
266267
* indented "{ }" block.
267268
*/
268-
function block(array: ?Array<string>): string {
269+
function block(array: Maybe<Array<string>>): string {
269270
return wrap('{\n', indent(join(array, '\n')), '\n}');
270271
}
271272

272273
/**
273274
* If maybeString is not null or empty, then wrap with start and end, otherwise print an empty string.
274275
*/
275-
function wrap(start: string, maybeString: ?string, end: string = ''): string {
276+
function wrap(start: string, maybeString: Maybe<string>, end: string = ''): string {
276277
return maybeString != null && maybeString !== ''
277278
? start + maybeString + end
278279
: '';
@@ -286,6 +287,6 @@ function isMultiline(str: string): boolean {
286287
return str.indexOf('\n') !== -1;
287288
}
288289

289-
function hasMultilineItems(maybeArray: ?Array<string>): boolean {
290+
function hasMultilineItems(maybeArray: Maybe<Array<string>>): boolean {
290291
return maybeArray != null && maybeArray.some(isMultiline);
291292
}

src/language/visitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect from '../jsutils/inspect';
2+
import { Maybe } from '../jsutils/Maybe';
23

34
import type { ASTNode, ASTKindToNode } from './ast';
45
import { isNode } from './ast';
@@ -405,7 +406,7 @@ export function getVisitFn(
405406
visitor: Visitor<any>,
406407
kind: string,
407408
isLeaving: boolean,
408-
): ?VisitFn<any> {
409+
): Maybe<VisitFn<any>> {
409410
const kindVisitor = visitor[kind];
410411
if (kindVisitor) {
411412
if (!isLeaving && typeof kindVisitor === 'function') {

src/type/validate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,15 +652,15 @@ function getAllImplementsInterfaceNodes(
652652
function getUnionMemberTypeNodes(
653653
union: GraphQLUnionType,
654654
typeName: string,
655-
): ?ReadonlyArray<NamedTypeNode> {
655+
): Maybe<ReadonlyArray<NamedTypeNode>> {
656656
return getAllSubNodes(union, (unionNode) => unionNode.types).filter(
657657
(typeNode) => typeNode.name.value === typeName,
658658
);
659659
}
660660

661661
function getDeprecatedDirectiveNode(
662-
definitionNode: ?{ readonly directives?: ReadonlyArray<DirectiveNode> },
663-
): ?DirectiveNode {
662+
definitionNode: Maybe<{ readonly directives?: ReadonlyArray<DirectiveNode> }>,
663+
): Maybe<DirectiveNode> {
664664
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
665665
return definitionNode?.directives?.find(
666666
(node) => node.name.value === GraphQLDeprecatedDirective.name,

src/utilities/extendSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ function getDeprecationReason(
697697
| EnumValueDefinitionNode
698698
| FieldDefinitionNode
699699
| InputValueDefinitionNode,
700-
): ?string {
700+
): Maybe<string> {
701701
const deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node);
702702
return (deprecated?.reason as any);
703703
}
@@ -707,7 +707,7 @@ function getDeprecationReason(
707707
*/
708708
function getSpecifiedByUrl(
709709
node: ScalarTypeDefinitionNode | ScalarTypeExtensionNode,
710-
): ?string {
710+
): Maybe<string> {
711711
const specifiedBy = getDirectiveValues(GraphQLSpecifiedByDirective, node);
712712
return (specifiedBy?.url as any);
713713
}

src/utilities/lexicographicSortSchema.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
isEnumType,
3333
isInputObjectType,
3434
} from '../type/definition';
35+
import { Maybe } from '../jsutils/Maybe';
3536

3637
/**
3738
* Sort GraphQLSchema.
@@ -69,8 +70,8 @@ export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema {
6970
function replaceNamedType<T extends GraphQLNamedType>(type: T): T {
7071
return (typeMap[type.name]as T);
7172
}
72-
73-
function replaceMaybeType<T extends ?GraphQLNamedType>(maybeType: T): T {
73+
74+
function replaceMaybeType<T extends Maybe<GraphQLNamedType>>(maybeType: T): T {
7475
return maybeType && replaceNamedType(maybeType);
7576
}
7677

src/utilities/printSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function printFilteredSchema(
7272
);
7373
}
7474

75-
function printSchemaDefinition(schema: GraphQLSchema): ?string {
75+
function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
7676
if (schema.description == null && isSchemaOfCommonNames(schema)) {
7777
return;
7878
}
@@ -282,7 +282,7 @@ function printDirective(directive: GraphQLDirective): string {
282282
);
283283
}
284284

285-
function printDeprecated(reason: ?string): string {
285+
function printDeprecated(reason: Maybe<string>): string {
286286
if (reason == null) {
287287
return '';
288288
}

0 commit comments

Comments
 (0)