Skip to content

Commit

Permalink
Flow: Remove 'any's or improve their locality (graphql#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Feb 6, 2020
1 parent 791be1a commit 95aa338
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 32 deletions.
18 changes: 9 additions & 9 deletions src/error/__tests__/locatedError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ describe('locatedError', () => {
});

it('passes GraphQLError-ish through', () => {
const e: any = new Error('I have a different prototype chain');
e.locations = [];
e.path = [];
e.nodes = [];
e.source = null;
e.positions = [];
e.name = 'GraphQLError';
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';

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

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

expect(locatedError(e, [], [])).to.not.deep.equal(e);
});
Expand Down
6 changes: 2 additions & 4 deletions src/execution/__tests__/executor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,8 @@ describe('Execute: Handles basic execution tasks', () => {
return Promise.resolve(new Error('Error getting asyncReturnError'));
},
asyncReturnErrorWithExtensions() {
const error: any = new Error(
'Error getting asyncReturnErrorWithExtensions',
);
error.extensions = { foo: 'bar' };
const error = new Error('Error getting asyncReturnErrorWithExtensions');
(error: any).extensions = { foo: 'bar' };

return Promise.resolve(error);
},
Expand Down
10 changes: 4 additions & 6 deletions src/language/__tests__/visitor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,8 @@ describe('Visitor', () => {
});

describe('Support for custom AST nodes', () => {
const customAST: any = parse('{ a }');
customAST.definitions[0].selectionSet.selections.push({
const customAST = parse('{ a }');
(customAST: any).definitions[0].selectionSet.selections.push({
kind: 'CustomField',
name: {
kind: 'Name',
Expand Down Expand Up @@ -916,10 +916,8 @@ describe('Visitor', () => {
});

it('does not traverse unknown node kinds', () => {
const customQueryDocumentKeys: any = {
...QueryDocumentKeys,
CustomField: ['name', 'selectionSet'],
};
const customQueryDocumentKeys = { ...QueryDocumentKeys };
(customQueryDocumentKeys: any).CustomField = ['name', 'selectionSet'];

const visited = [];
const visitor = {
Expand Down
3 changes: 1 addition & 2 deletions src/polyfills/flatMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ declare function flatMap<T, U>(
fn: (item: T, index: number) => $ReadOnlyArray<U> | U,
): Array<U>;

// Workaround to make older Flow versions happy
const flatMapMethod = (Array.prototype: any).flatMap;
const flatMapMethod = Array.prototype.flatMap;

/* eslint-disable no-redeclare */
// $FlowFixMe
Expand Down
11 changes: 6 additions & 5 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,7 @@ export function extendSchemaImpl(
mutation: ?GraphQLObjectType,
subscription: ?GraphQLObjectType,
|} {
// Note: While this could make early assertions to get the correctly
// typed values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
const opTypes: any = {};
const opTypes = {};
for (const node of nodes) {
/* istanbul ignore next (See https://github.com/graphql/graphql-js/issues/2203) */
const operationTypesNodes = node.operationTypes ?? [];
Expand All @@ -416,7 +413,11 @@ export function extendSchemaImpl(
opTypes[operationType.operation] = getNamedType(operationType.type);
}
}
return opTypes;

// Note: While this could make early assertions to get the correctly
// typed values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
return (opTypes: any);
}

function getNamedType(node: NamedTypeNode): GraphQLNamedType {
Expand Down
8 changes: 2 additions & 6 deletions src/validation/rules/UniqueDirectivesPerLocationRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { GraphQLError } from '../../error/GraphQLError';

import { Kind } from '../../language/kinds';
import { type DirectiveNode } from '../../language/ast';
import { type ASTVisitor } from '../../language/visitor';

import { specifiedDirectives } from '../../type/directives';
Expand Down Expand Up @@ -44,12 +43,9 @@ export function UniqueDirectivesPerLocationRule(
// them all, just listen for entering any node, and check to see if it
// defines any directives.
enter(node) {
// Flow can't refine that node.directives will only contain directives,
// so we cast so the rest of the code is well typed.
const directives: ?$ReadOnlyArray<DirectiveNode> = (node: any).directives;
if (directives) {
if (node.directives != null) {
const knownDirectives = Object.create(null);
for (const directive of directives) {
for (const directive of node.directives) {
const directiveName = directive.name.value;

if (uniqueDirectiveMap[directiveName]) {
Expand Down

0 comments on commit 95aa338

Please sign in to comment.