Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow: Replace force type conversion with explicit $FlowFixMe comments #3081

Merged
merged 1 commit into from May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/error/GraphQLError.js
Expand Up @@ -133,7 +133,8 @@ export class GraphQLError extends Error {
}
}

Object.defineProperties((this: any), {
// $FlowFixMe[cannot-write] FIXME
Object.defineProperties(this, {
name: { value: 'GraphQLError' },
message: {
value: message,
Expand Down
22 changes: 14 additions & 8 deletions src/error/__tests__/locatedError-test.js
Expand Up @@ -17,20 +17,26 @@ describe('locatedError', () => {
});

it('passes GraphQLError-ish through', () => {
const e = new Error('I have a different prototype chain');
(e: any).locations = [];
(e: any).path = [];
(e: any).nodes = [];
(e: any).source = null;
(e: any).positions = [];
(e: any).name = 'GraphQLError';
const e = new Error();
// $FlowExpectedError[prop-missing]
e.locations = [];
// $FlowExpectedError[prop-missing]
e.path = [];
// $FlowExpectedError[prop-missing]
e.nodes = [];
// $FlowExpectedError[prop-missing]
e.source = null;
// $FlowExpectedError[prop-missing]
e.positions = [];
e.name = 'GraphQLError';

expect(locatedError(e, [], [])).to.deep.equal(e);
});

it('does not pass through elasticsearch-like errors', () => {
const e = new Error('I am from elasticsearch');
(e: any).path = '/something/feed/_search';
// $FlowExpectedError[prop-missing]
e.path = '/something/feed/_search';

expect(locatedError(e, [], [])).to.not.deep.equal(e);
});
Expand Down
12 changes: 8 additions & 4 deletions src/error/locatedError.js
Expand Up @@ -22,14 +22,18 @@ export function locatedError(

// Note: this uses a brand-check to support GraphQL errors originating from other contexts.
if (Array.isArray(originalError.path)) {
return (originalError: any);
// $FlowExpectedError[incompatible-return]
return originalError;
}

return new GraphQLError(
originalError.message,
(originalError: any).nodes ?? nodes,
(originalError: any).source,
(originalError: any).positions,
// $FlowFixMe[prop-missing] FIXME
originalError.nodes ?? nodes,
// $FlowFixMe[prop-missing] FIXME
originalError.source,
// $FlowFixMe[prop-missing] FIXME
originalError.positions,
path,
originalError,
);
Expand Down
8 changes: 6 additions & 2 deletions src/execution/__tests__/abstract-test.js
Expand Up @@ -6,6 +6,7 @@ import { parse } from '../../language/parser';
import { GraphQLSchema } from '../../type/schema';
import { GraphQLString, GraphQLBoolean } from '../../type/scalars';
import {
assertInterfaceType,
GraphQLList,
GraphQLObjectType,
GraphQLInterfaceType,
Expand Down Expand Up @@ -569,13 +570,16 @@ describe('Execute: Handles execution of abstract types', () => {
);

// FIXME: workaround since we can't inject resolveType into SDL
(schema.getType('Pet'): any).resolveType = () => [];
// $FlowExpectedError[incompatible-type]
assertInterfaceType(schema.getType('Pet')).resolveType = () => [];
expectError({ forTypeName: undefined }).toEqual(
'Abstract type "Pet" must resolve to an Object type at runtime for field "Query.pet" with value { __typename: undefined }, received "[]".',
);

// FIXME: workaround since we can't inject resolveType into SDL
(schema.getType('Pet'): any).resolveType = () => schema.getType('Cat');
assertInterfaceType(schema.getType('Pet')).resolveType =
// $FlowExpectedError[incompatible-type]
() => schema.getType('Cat');
expectError({ forTypeName: undefined }).toEqual(
'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.',
);
Expand Down
3 changes: 2 additions & 1 deletion src/execution/__tests__/executor-test.js
Expand Up @@ -489,7 +489,8 @@ describe('Execute: Handles basic execution tasks', () => {
},
asyncReturnErrorWithExtensions() {
const error = new Error('Error getting asyncReturnErrorWithExtensions');
(error: any).extensions = { foo: 'bar' };
// $FlowExpectedError[prop-missing]
error.extensions = { foo: 'bar' };

return Promise.resolve(error);
},
Expand Down
3 changes: 2 additions & 1 deletion src/jsutils/__tests__/inspect-test.js
Expand Up @@ -165,7 +165,8 @@ describe('inspect', () => {

expect(inspect([[new Foo()]])).to.equal('[[[Foo]]]');

(Foo.prototype: any)[Symbol.toStringTag] = 'Bar';
// $FlowExpectedError[prop-missing]
Foo.prototype[Symbol.toStringTag] = 'Bar';
expect(inspect([[new Foo()]])).to.equal('[[[Bar]]]');

// eslint-disable-next-line func-names
Expand Down
11 changes: 5 additions & 6 deletions src/language/__tests__/predicates-test.js
Expand Up @@ -17,12 +17,11 @@ import {
isTypeExtensionNode,
} from '../predicates';

const allASTNodes: Array<ASTNode> = Object.values(Kind).map(
(kind) => ({ kind }: any),
);

function filterNodes(predicate: (node: ASTNode) => boolean): Array<string> {
return allASTNodes.filter(predicate).map(({ kind }) => kind);
function filterNodes(predicate: (ASTNode) => boolean): Array<string> {
return Object.values(Kind).filter(
// $FlowExpectedError[speculation-ambiguous] create node only with kind
(kind) => predicate({ kind }),
);
}

describe('AST node predicates', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/language/lexer.js
Expand Up @@ -64,8 +64,8 @@ export class Lexer {
let token = this.token;
if (token.kind !== TokenKind.EOF) {
do {
// Note: next is only mutable during parsing, so we cast to allow this.
token = token.next ?? ((token: any).next = readToken(this, token));
// $FlowFixMe[cannot-write] next is only mutable during parsing, so we cast to allow this.
token = token.next ?? (token.next = readToken(this, token));
} while (token.kind === TokenKind.COMMENT);
}
return token;
Expand Down
24 changes: 16 additions & 8 deletions src/language/parser.js
Expand Up @@ -184,7 +184,8 @@ export class Parser {
const token = this.expectToken(TokenKind.NAME);
return this.node(token, {
kind: Kind.NAME,
value: ((token.value: any): string),
// $FlowFixMe[incompatible-return] FIXME
value: token.value,
});
}

Expand Down Expand Up @@ -413,7 +414,8 @@ export class Parser {
}

parseConstArgument(): ConstArgumentNode {
return (this.parseArgument(true): any);
// $FlowFixMe[incompatible-return] FIXME during TS conversion
return this.parseArgument(true);
}

// Implements the parsing rules in the Fragments section.
Expand Down Expand Up @@ -517,13 +519,15 @@ export class Parser {
this._lexer.advance();
return this.node(token, {
kind: Kind.INT,
value: ((token.value: any): string),
// $FlowFixMe[incompatible-return] FIXME
value: token.value,
});
case TokenKind.FLOAT:
this._lexer.advance();
return this.node(token, {
kind: Kind.FLOAT,
value: ((token.value: any): string),
// $FlowFixMe[incompatible-return] FIXME
value: token.value,
});
case TokenKind.STRING:
case TokenKind.BLOCK_STRING:
Expand All @@ -540,7 +544,8 @@ export class Parser {
default:
return this.node(token, {
kind: Kind.ENUM,
value: ((token.value: any): string),
// $FlowFixMe[incompatible-return] FIXME
value: token.value,
});
}
case TokenKind.DOLLAR:
Expand All @@ -563,15 +568,17 @@ export class Parser {
}

parseConstValueLiteral(): ConstValueNode {
return (this.parseValueLiteral(true): any);
// $FlowFixMe[incompatible-return] FIXME during TS conversion
return this.parseValueLiteral(true);
}

parseStringLiteral(): StringValueNode {
const token = this._lexer.token;
this._lexer.advance();
return this.node(token, {
kind: Kind.STRING,
value: ((token.value: any): string),
// $FlowFixMe[incompatible-return] FIXME
value: token.value,
block: token.kind === TokenKind.BLOCK_STRING,
});
}
Expand Down Expand Up @@ -631,7 +638,8 @@ export class Parser {
}

parseConstDirectives(): Array<ConstDirectiveNode> {
return (this.parseDirectives(true): any);
// $FlowFixMe[incompatible-return] FIXME during TS conversion
return this.parseDirectives(true);
}

/**
Expand Down