Skip to content

Commit

Permalink
convert ? (flow) to Maybe (TS)
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Nov 6, 2020
1 parent e3cd073 commit caa8334
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/__testUtils__/inspectStr.ts
@@ -1,7 +1,9 @@
import { Maybe } from '../jsutils/Maybe';

/**
* Special inspect function to produce readable string literal for error messages in tests
*/
export default function inspectStr(str: ?string): string {
export default function inspectStr(str: Maybe<string>): string {
if (str == null) {
return 'null';
}
Expand Down
2 changes: 1 addition & 1 deletion src/error/GraphQLError.ts
Expand Up @@ -68,7 +68,7 @@ export class GraphQLError extends Error {
/**
* The original error thrown from a field resolver during execution.
*/
readonly originalError: ?Error;
readonly originalError: Maybe<Error>;

/**
* Extension fields to add to the formatted error.
Expand Down
3 changes: 2 additions & 1 deletion src/execution/values.ts
Expand Up @@ -21,6 +21,7 @@ import { isInputType, isNonNullType } from '../type/definition';
import { typeFromAST } from '../utilities/typeFromAST';
import { valueFromAST } from '../utilities/valueFromAST';
import { coerceInputValue } from '../utilities/coerceInputValue';
import { Maybe } from '../jsutils/Maybe';

type CoercedVariableValues =
| { errors: ReadonlyArray<GraphQLError> }
Expand Down Expand Up @@ -245,7 +246,7 @@ export function getArgumentValues(
export function getDirectiveValues(
directiveDef: GraphQLDirective,
node: { readonly directives?: ReadonlyArray<DirectiveNode> },
variableValues?: ?ObjMap<unknown>,
variableValues?: Maybe<ObjMap<unknown>>,
): void | { [argument: string]: unknown } {
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
const directiveNode = node.directives?.find(
Expand Down
4 changes: 3 additions & 1 deletion src/jsutils/Path.ts
@@ -1,3 +1,5 @@
import { Maybe } from './Maybe';

export type Path = {
readonly prev: Path | void;
readonly key: string | number;
Expand All @@ -18,7 +20,7 @@ export function addPath(
/**
* Given a Path, return an Array of the path keys.
*/
export function pathToArray(path: ?Readonly<Path>): Array<string | number> {
export function pathToArray(path: Maybe<Readonly<Path>>): Array<string | number> {
const flattened = [];
let curr = path;
while (curr) {
Expand Down
10 changes: 6 additions & 4 deletions src/language/experimentalOnlineParser/grammar.ts
@@ -1,3 +1,5 @@
import { Maybe } from "../../jsutils/Maybe";

export type GraphQLGrammarType = {
[name: string]: GraphQLGrammarRule,
};
Expand All @@ -16,8 +18,8 @@ export type GraphQLGrammarRule =
| GraphQLGrammarConstraintsSet;
export interface GraphQLGrammarBaseRuleConstraint {
butNot?:
| ?GraphQLGrammarTokenConstraint
| ?Array<GraphQLGrammarTokenConstraint>;
| Maybe<GraphQLGrammarTokenConstraint>
| Maybe<Array<GraphQLGrammarTokenConstraint>>;
optional?: boolean;
eatNextOnFail?: boolean;
}
Expand All @@ -44,8 +46,8 @@ export interface GraphQLGrammarTokenConstraint
| 'String'
| 'BlockString'
| 'Comment';
ofValue?: ?string;
oneOf?: ?Array<string>;
ofValue?: Maybe<string>;
oneOf?: Maybe<Array<string>>;
tokenName?: string;
definitionName?: boolean;
typeName?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/language/experimentalOnlineParser/onlineParser.ts
Expand Up @@ -101,7 +101,7 @@ type OnlineParserConfig = {
};

type OnlineParserConfigOption = {
tabSize: ?number,
tabSize: Maybe<number>,
};

export class OnlineParser {
Expand Down Expand Up @@ -453,7 +453,7 @@ export class OnlineParser {
return this.state.rules[this.state.rules.length - 1] || null;
}

_popMatchedRule(token: ?Token) {
_popMatchedRule(token: Maybe<Token>) {
const rule = this.state.rules.pop();
if (!rule) {
return;
Expand Down
9 changes: 5 additions & 4 deletions src/language/printer.ts
Expand Up @@ -2,6 +2,7 @@ import type { ASTNode } from './ast';

import { visit } from './visitor';
import { printBlockString } from './blockString';
import { Maybe } from '../jsutils/Maybe';

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

/**
* Given array, print each item on its own line, wrapped in an
* indented "{ }" block.
*/
function block(array: ?Array<string>): string {
function block(array: Maybe<Array<string>>): string {
return wrap('{\n', indent(join(array, '\n')), '\n}');
}

/**
* If maybeString is not null or empty, then wrap with start and end, otherwise print an empty string.
*/
function wrap(start: string, maybeString: ?string, end: string = ''): string {
function wrap(start: string, maybeString: Maybe<string>, end: string = ''): string {
return maybeString != null && maybeString !== ''
? start + maybeString + end
: '';
Expand All @@ -286,6 +287,6 @@ function isMultiline(str: string): boolean {
return str.indexOf('\n') !== -1;
}

function hasMultilineItems(maybeArray: ?Array<string>): boolean {
function hasMultilineItems(maybeArray: Maybe<Array<string>>): boolean {
return maybeArray != null && maybeArray.some(isMultiline);
}
3 changes: 2 additions & 1 deletion src/language/visitor.ts
@@ -1,4 +1,5 @@
import inspect from '../jsutils/inspect';
import { Maybe } from '../jsutils/Maybe';

import type { ASTNode, ASTKindToNode } from './ast';
import { isNode } from './ast';
Expand Down Expand Up @@ -405,7 +406,7 @@ export function getVisitFn(
visitor: Visitor<any>,
kind: string,
isLeaving: boolean,
): ?VisitFn<any> {
): Maybe<VisitFn<any>> {
const kindVisitor = visitor[kind];
if (kindVisitor) {
if (!isLeaving && typeof kindVisitor === 'function') {
Expand Down
6 changes: 3 additions & 3 deletions src/type/validate.ts
Expand Up @@ -652,15 +652,15 @@ function getAllImplementsInterfaceNodes(
function getUnionMemberTypeNodes(
union: GraphQLUnionType,
typeName: string,
): ?ReadonlyArray<NamedTypeNode> {
): Maybe<ReadonlyArray<NamedTypeNode>> {
return getAllSubNodes(union, (unionNode) => unionNode.types).filter(
(typeNode) => typeNode.name.value === typeName,
);
}

function getDeprecatedDirectiveNode(
definitionNode: ?{ readonly directives?: ReadonlyArray<DirectiveNode> },
): ?DirectiveNode {
definitionNode: Maybe<{ readonly directives?: ReadonlyArray<DirectiveNode> }>,
): Maybe<DirectiveNode> {
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
return definitionNode?.directives?.find(
(node) => node.name.value === GraphQLDeprecatedDirective.name,
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/extendSchema.ts
Expand Up @@ -697,7 +697,7 @@ function getDeprecationReason(
| EnumValueDefinitionNode
| FieldDefinitionNode
| InputValueDefinitionNode,
): ?string {
): Maybe<string> {
const deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node);
return (deprecated?.reason as any);
}
Expand All @@ -707,7 +707,7 @@ function getDeprecationReason(
*/
function getSpecifiedByUrl(
node: ScalarTypeDefinitionNode | ScalarTypeExtensionNode,
): ?string {
): Maybe<string> {
const specifiedBy = getDirectiveValues(GraphQLSpecifiedByDirective, node);
return (specifiedBy?.url as any);
}
5 changes: 3 additions & 2 deletions src/utilities/lexicographicSortSchema.ts
Expand Up @@ -32,6 +32,7 @@ import {
isEnumType,
isInputObjectType,
} from '../type/definition';
import { Maybe } from '../jsutils/Maybe';

/**
* Sort GraphQLSchema.
Expand Down Expand Up @@ -69,8 +70,8 @@ export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema {
function replaceNamedType<T extends GraphQLNamedType>(type: T): T {
return (typeMap[type.name]as T);
}

function replaceMaybeType<T extends ?GraphQLNamedType>(maybeType: T): T {
function replaceMaybeType<T extends Maybe<GraphQLNamedType>>(maybeType: T): T {
return maybeType && replaceNamedType(maybeType);
}

Expand Down
4 changes: 2 additions & 2 deletions src/utilities/printSchema.ts
Expand Up @@ -72,7 +72,7 @@ function printFilteredSchema(
);
}

function printSchemaDefinition(schema: GraphQLSchema): ?string {
function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
if (schema.description == null && isSchemaOfCommonNames(schema)) {
return;
}
Expand Down Expand Up @@ -282,7 +282,7 @@ function printDirective(directive: GraphQLDirective): string {
);
}

function printDeprecated(reason: ?string): string {
function printDeprecated(reason: Maybe<string>): string {
if (reason == null) {
return '';
}
Expand Down

0 comments on commit caa8334

Please sign in to comment.