Skip to content

Commit

Permalink
replace %checks (flow) to typeguards (ts)
Browse files Browse the repository at this point in the history
from flow to ts conversion
  • Loading branch information
saihaj committed Nov 5, 2020
1 parent 5f54a57 commit e45a056
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 80 deletions.
5 changes: 1 addition & 4 deletions src/jsutils/isAsyncIterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
* Returns true if the provided object implements the AsyncIterator protocol via
* either implementing a `Symbol.asyncIterator` or `"@@asyncIterator"` method.
*/
declare function isAsyncIterable(value: unknown): boolean; // FIXME: TS_CONVERTION %check

// eslint-disable-next-line no-redeclare
export default function isAsyncIterable(maybeAsyncIterable) {
export default function isAsyncIterable(maybeAsyncIterable:unknown): maybeAsyncIterable is AsyncIterable<unknown> {
if (maybeAsyncIterable == null || typeof maybeAsyncIterable !== 'object') {
return false;
}
Expand Down
5 changes: 1 addition & 4 deletions src/jsutils/isCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
* An Object value which might implement the Iterable or Array-like protocols.
* @return {boolean} true if Iterable or Array-like Object.
*/
declare function isCollection(value: unknown): boolean; // FIXME: TS_CONVERTION %check

// eslint-disable-next-line no-redeclare
export default function isCollection(obj) {
export default function isCollection(obj: unknown): obj is object| Symbol {
if (obj == null || typeof obj !== 'object') {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/jsutils/isObjectLike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* Return true if `value` is object-like. A value is object-like if it's not
* `null` and has a `typeof` result of "object".
*/
export default function isObjectLike(value: unknown): boolean { // FIXME: TS_CONVERTION %check
export default function isObjectLike(value: unknown): value is object {
return typeof value == 'object' && value !== null;
}
5 changes: 1 addition & 4 deletions src/jsutils/isPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
* Returns true if the value acts like a Promise, i.e. has a "then" function,
* otherwise returns false.
*/
declare function isPromise(value: unknown): boolean; // FIXME: TS_CONVERTION %check

// eslint-disable-next-line no-redeclare
export default function isPromise(value) {
export default function isPromise(value: unknown): value is Promise<unknown> {
return typeof value?.then === 'function';
}
2 changes: 1 addition & 1 deletion src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class Token {
/**
* @internal
*/
export function isNode(maybeNode: unknown): boolean { // FIXME: TS_CONVERTION %check
export function isNode(maybeNode: unknown): maybeNode is ASTNode {
return maybeNode != null && typeof maybeNode.kind === 'string';
}

Expand Down
2 changes: 1 addition & 1 deletion src/language/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Lexer {
/**
* @internal
*/
export function isPunctuatorTokenKind(kind: TokenKindEnum): boolean {
export function isPunctuatorTokenKind(kind: TokenKindEnum): kind is typeof TokenKind {
return (
kind === TokenKind.BANG ||
kind === TokenKind.DOLLAR ||
Expand Down
31 changes: 21 additions & 10 deletions src/language/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import type { ASTNode } from './ast';
import {
ASTNode,
DefinitionNode,
ExecutableDefinitionNode,
SelectionNode,
ValueNode,
TypeNode,
TypeSystemDefinitionNode,
TypeDefinitionNode,
TypeSystemExtensionNode,
TypeExtensionNode,
} from './ast';
import { Kind } from './kinds';

export function isDefinitionNode(node: ASTNode): boolean {
export function isDefinitionNode(node: ASTNode): node is DefinitionNode {
return (
isExecutableDefinitionNode(node) ||
isTypeSystemDefinitionNode(node) ||
isTypeSystemExtensionNode(node)
);
}

export function isExecutableDefinitionNode(node: ASTNode): boolean {
export function isExecutableDefinitionNode(node: ASTNode): node is ExecutableDefinitionNode {
return (
node.kind === Kind.OPERATION_DEFINITION ||
node.kind === Kind.FRAGMENT_DEFINITION
);
}

export function isSelectionNode(node: ASTNode): boolean {
export function isSelectionNode(node: ASTNode): node is SelectionNode {
return (
node.kind === Kind.FIELD ||
node.kind === Kind.FRAGMENT_SPREAD ||
node.kind === Kind.INLINE_FRAGMENT
);
}

export function isValueNode(node: ASTNode): boolean {
export function isValueNode(node: ASTNode): node is ValueNode {
return (
node.kind === Kind.VARIABLE ||
node.kind === Kind.INT ||
Expand All @@ -38,23 +49,23 @@ export function isValueNode(node: ASTNode): boolean {
);
}

export function isTypeNode(node: ASTNode): boolean {
export function isTypeNode(node: ASTNode): node is TypeNode {
return (
node.kind === Kind.NAMED_TYPE ||
node.kind === Kind.LIST_TYPE ||
node.kind === Kind.NON_NULL_TYPE
);
}

export function isTypeSystemDefinitionNode(node: ASTNode): boolean {
export function isTypeSystemDefinitionNode(node: ASTNode): node is TypeSystemDefinitionNode {
return (
node.kind === Kind.SCHEMA_DEFINITION ||
isTypeDefinitionNode(node) ||
node.kind === Kind.DIRECTIVE_DEFINITION
);
}

export function isTypeDefinitionNode(node: ASTNode): boolean {
export function isTypeDefinitionNode(node: ASTNode): node is TypeDefinitionNode {
return (
node.kind === Kind.SCALAR_TYPE_DEFINITION ||
node.kind === Kind.OBJECT_TYPE_DEFINITION ||
Expand All @@ -65,11 +76,11 @@ export function isTypeDefinitionNode(node: ASTNode): boolean {
);
}

export function isTypeSystemExtensionNode(node: ASTNode): boolean {
export function isTypeSystemExtensionNode(node: ASTNode): node is TypeSystemExtensionNode {
return node.kind === Kind.SCHEMA_EXTENSION || isTypeExtensionNode(node);
}

export function isTypeExtensionNode(node: ASTNode): boolean {
export function isTypeExtensionNode(node: ASTNode): node is TypeExtensionNode {
return (
node.kind === Kind.SCALAR_TYPE_EXTENSION ||
node.kind === Kind.OBJECT_TYPE_EXTENSION ||
Expand Down
4 changes: 1 addition & 3 deletions src/language/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export class Source {
*
* @internal
*/
declare function isSource(source: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isSource(source) {
export function isSource(source: unknown): source is Source {
return instanceOf(source, Source);
}
66 changes: 23 additions & 43 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type GraphQLType =
| GraphQLList<any>
| GraphQLNonNull<any>;

export function isType(type: unknown): boolean {
export function isType(type: unknown): type is GraphQLType {
return (
isScalarType(type) ||
isObjectType(type) ||
Expand All @@ -88,10 +88,7 @@ export function assertType(type: unknown): GraphQLType {
/**
* There are predicates for each kind of GraphQL type.
*/

declare function isScalarType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isScalarType(type) {
export function isScalarType(type: unknown): type is GraphQLScalarType {
return instanceOf(type, GraphQLScalarType);
}

Expand All @@ -102,9 +99,7 @@ export function assertScalarType(type: unknown): GraphQLScalarType {
return type;
}

declare function isObjectType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isObjectType(type) {
export function isObjectType(type: unknown): type is GraphQLObjectType {
return instanceOf(type, GraphQLObjectType);
}

Expand All @@ -115,9 +110,7 @@ export function assertObjectType(type: unknown): GraphQLObjectType {
return type;
}

declare function isInterfaceType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isInterfaceType(type) {
export function isInterfaceType(type: unknown): type is GraphQLInterfaceType {
return instanceOf(type, GraphQLInterfaceType);
}

Expand All @@ -130,9 +123,7 @@ export function assertInterfaceType(type: unknown): GraphQLInterfaceType {
return type;
}

declare function isUnionType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isUnionType(type) {
export function isUnionType(type: unknown): type is GraphQLUnionType {
return instanceOf(type, GraphQLUnionType);
}

Expand All @@ -143,9 +134,7 @@ export function assertUnionType(type: unknown): GraphQLUnionType {
return type;
}

declare function isEnumType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isEnumType(type) {
export function isEnumType(type: unknown): type is GraphQLEnumType {
return instanceOf(type, GraphQLEnumType);
}

Expand All @@ -156,9 +145,7 @@ export function assertEnumType(type: unknown): GraphQLEnumType {
return type;
}

declare function isInputObjectType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isInputObjectType(type) {
export function isInputObjectType(type: unknown): type is GraphQLInputObjectType {
return instanceOf(type, GraphQLInputObjectType);
}

Expand All @@ -171,9 +158,7 @@ export function assertInputObjectType(type: unknown): GraphQLInputObjectType {
return type;
}

declare function isListType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isListType(type) {
export function isListType(type: unknown): type is GraphQLList<any> {
return instanceOf(type, GraphQLList);
}

Expand All @@ -184,9 +169,7 @@ export function assertListType(type: unknown): GraphQLList<any> {
return type;
}

declare function isNonNullType(type: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isNonNullType(type) {
export function isNonNullType(type: unknown): type is GraphQLNonNull<any> {
return instanceOf(type, GraphQLNonNull);
}

Expand All @@ -212,7 +195,7 @@ export type GraphQLInputType =
| GraphQLList<GraphQLInputType>,
>;

export function isInputType(type: unknown): boolean {
export function isInputType(type: unknown): type is GraphQLInputType {
return (
isScalarType(type) ||
isEnumType(type) ||
Expand Down Expand Up @@ -247,7 +230,7 @@ export type GraphQLOutputType =
| GraphQLList<GraphQLOutputType>,
>;

export function isOutputType(type: unknown): boolean {
export function isOutputType(type: unknown): type is GraphQLOutputType {
return (
isScalarType(type) ||
isObjectType(type) ||
Expand All @@ -270,7 +253,7 @@ export function assertOutputType(type: unknown): GraphQLOutputType {
*/
export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType;

export function isLeafType(type: unknown): boolean {
export function isLeafType(type: unknown): type is GraphQLLeafType {
return isScalarType(type) || isEnumType(type);
}

Expand All @@ -289,7 +272,7 @@ export type GraphQLCompositeType =
| GraphQLInterfaceType
| GraphQLUnionType;

export function isCompositeType(type: unknown): boolean {
export function isCompositeType(type: unknown): type is GraphQLCompositeType {
return isObjectType(type) || isInterfaceType(type) || isUnionType(type);
}

Expand All @@ -307,7 +290,7 @@ export function assertCompositeType(type: unknown): GraphQLCompositeType {
*/
export type GraphQLAbstractType = GraphQLInterfaceType | GraphQLUnionType;

export function isAbstractType(type: unknown): boolean {
export function isAbstractType(type: unknown): type is GraphQLAbstractType {
return isInterfaceType(type) || isUnionType(type);
}

Expand Down Expand Up @@ -414,7 +397,7 @@ export class GraphQLNonNull<T extends GraphQLNullableType> {

export type GraphQLWrappingType = GraphQLList<any> | GraphQLNonNull<any>;

export function isWrappingType(type: unknown): boolean {
export function isWrappingType(type: unknown): type is GraphQLWrappingType {
return isListType(type) || isNonNullType(type);
}

Expand All @@ -437,7 +420,7 @@ export type GraphQLNullableType =
| GraphQLInputObjectType
| GraphQLList<any>;

export function isNullableType(type: unknown): boolean {
export function isNullableType(type: unknown): type is GraphQLNullableType {
return isType(type) && !isNonNullType(type);
}

Expand All @@ -448,12 +431,11 @@ export function assertNullableType(type: unknown): GraphQLNullableType {
return type;
}

/* eslint-disable no-redeclare */
declare function getNullableType(type: void | null): void;
declare function getNullableType<T extends GraphQLNullableType>(type: T): T;
declare function getNullableType<T>(type: GraphQLNonNull<T>): T;

export function getNullableType(type: void | null): void;
export function getNullableType<T extends GraphQLNullableType>(type: T): T;
export function getNullableType<T extends GraphQLNullableType>(type: GraphQLNonNull<T>): T;
export function getNullableType(type) {
/* eslint-enable no-redeclare */
if (type) {
return isNonNullType(type) ? type.ofType : type;
}
Expand All @@ -470,7 +452,7 @@ export type GraphQLNamedType =
| GraphQLEnumType
| GraphQLInputObjectType;

export function isNamedType(type: unknown): boolean {
export function isNamedType(type: unknown): type is GraphQLNamedType {
return (
isScalarType(type) ||
isObjectType(type) ||
Expand All @@ -488,11 +470,9 @@ export function assertNamedType(type: unknown): GraphQLNamedType {
return type;
}

/* eslint-disable no-redeclare */
declare function getNamedType(type: void | null): void;
declare function getNamedType(type: GraphQLType): GraphQLNamedType;
export function getNamedType(type: void | null): void;
export function getNamedType(type: GraphQLType): GraphQLNamedType;
export function getNamedType(type) {
/* eslint-enable no-redeclare */
if (type) {
let unwrappedType = type;
while (isWrappingType(unwrappedType)) {
Expand Down
6 changes: 1 addition & 5 deletions src/type/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import { Maybe } from '../jsutils/Maybe';
/**
* Test if the given value is a GraphQL directive.
*/
declare function isDirective(
directive: unknown,
): boolean;
// eslint-disable-next-line no-redeclare
export function isDirective(directive) {
export function isDirective(directive: unknown): directive is GraphQLDirective {
return instanceOf(directive, GraphQLDirective);
}

Expand Down
2 changes: 1 addition & 1 deletion src/type/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,6 @@ export const introspectionTypes = Object.freeze([
__TypeKind,
]);

export function isIntrospectionType(type: GraphQLNamedType): boolean { // FIXME: TS_CONVERTION %check
export function isIntrospectionType(type: GraphQLNamedType): boolean {
return introspectionTypes.some(({ name }) => type.name === name);
}
4 changes: 1 addition & 3 deletions src/type/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ import { Maybe } from '../jsutils/Maybe';
/**
* Test if the given value is a GraphQL schema.
*/
declare function isSchema(schema: unknown): boolean;
// eslint-disable-next-line no-redeclare
export function isSchema(schema) {
export function isSchema(schema: unknown): schema is GraphQLSchema {
return instanceOf(schema, GraphQLSchema);
}

Expand Down

0 comments on commit e45a056

Please sign in to comment.