Skip to content

Commit 1e13dc2

Browse files
committed
fix(Flowtype): more strict type checks
1 parent f4e6029 commit 1e13dc2

33 files changed

+111
-129
lines changed

.eslintrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@
3434
"plugins": [
3535
"flowtype",
3636
"prettier"
37-
]
37+
],
38+
"globals": {
39+
"$Shape": true,
40+
"$FlowFixMe": true,
41+
}
3842
}

flow-typed/npm/jest_v19.x.x.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ type JestExpectType = {
201201
/**
202202
* Use .toMatch to check that a string matches a regular expression.
203203
*/
204-
toMatch(regexp: RegExp): void,
204+
toMatch(regexp: RegExp | string): void,
205205
/**
206206
* Use .toMatchObject to check that a javascript object matches a subset of the properties of an object.
207207
*/

src/ElasticApiParser.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import dox from 'dox';
55
import fs from 'fs';
66
import path from 'path';
7-
import { GraphQLJSON, upperFirst, TypeComposer } from 'graphql-compose';
7+
import { GraphQLJSON, upperFirst, TypeComposer, type ComposeFieldConfigMap } from 'graphql-compose';
88
import {
99
GraphQLString,
1010
GraphQLFloat,
@@ -15,7 +15,6 @@ import {
1515
} from 'graphql-compose/lib/graphql';
1616
import type {
1717
GraphQLArgumentConfig,
18-
GraphQLFieldConfig,
1918
GraphQLFieldConfigMap,
2019
GraphQLFieldConfigArgumentMap,
2120
GraphQLInputType,
@@ -277,10 +276,7 @@ export default class ElasticApiParser {
277276
]);
278277
}
279278

280-
generateFieldConfig(
281-
methodName: string,
282-
methodArgs?: { [paramName: string]: mixed }
283-
): GraphQLFieldConfig<*, *> {
279+
generateFieldConfig(methodName: string, methodArgs?: { [paramName: string]: mixed }) {
284280
if (!methodName) {
285281
throw new Error(`You should provide Elastic search method.`);
286282
}
@@ -299,7 +295,8 @@ export default class ElasticApiParser {
299295
type: GraphQLJSON,
300296
description,
301297
args: argMap,
302-
resolve: (src, args, context) => {
298+
// eslint-disable-next-line no-unused-vars
299+
resolve: (source: any, args: Object, context: any, info: any) => {
303300
const client = (context && context.elasticClient) || this.elasticClient;
304301

305302
if (!client) {
@@ -395,8 +392,7 @@ export default class ElasticApiParser {
395392
} else if (val === 'null') {
396393
result.null_string = { value: 'null' };
397394
} else if (Number.isFinite(val)) {
398-
// $FlowFixMe
399-
result[`number_${val}`] = { value: val };
395+
result[`number_${(val: any)}`] = { value: val };
400396
} else if (typeof val === 'string') {
401397
result[val] = { value: val };
402398
}
@@ -456,7 +452,7 @@ export default class ElasticApiParser {
456452
return result;
457453
}
458454

459-
reassembleNestedFields(fields: GraphQLFieldConfigMap<*, *>): GraphQLFieldConfigMap<*, *> {
455+
reassembleNestedFields(fields: ComposeFieldConfigMap<any, any>): GraphQLFieldConfigMap<*, *> {
460456
const result = {};
461457
Object.keys(fields).forEach(k => {
462458
const names = k.split('.');
@@ -467,8 +463,7 @@ export default class ElasticApiParser {
467463
result[names[0]] = {
468464
type: new GraphQLObjectType({
469465
name: `${this.prefix}_${upperFirst(names[0])}`,
470-
// $FlowFixMe
471-
fields: () => {},
466+
fields: (() => {}: any),
472467
}),
473468
resolve: () => {
474469
return {};

src/__tests__/ElasticApiParser-test.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ElasticApiParser from '../ElasticApiParser';
1717
const apiPartialPath = path.resolve(__dirname, '../__mocks__/apiPartial.js');
1818

1919
describe('ElasticApiParser', () => {
20-
let parser;
20+
let parser: ElasticApiParser;
2121

2222
beforeEach(() => {
2323
parser = new ElasticApiParser({
@@ -44,6 +44,7 @@ describe('ElasticApiParser', () => {
4444
describe('findApiVersionFile()', () => {
4545
it('should find proper version in elasticsearch 12.x', () => {
4646
const { loadApiListFile } = ElasticApiParser;
47+
4748
// $FlowFixMe
4849
ElasticApiParser.loadApiListFile = () =>
4950
`
@@ -60,19 +61,15 @@ describe('ElasticApiParser', () => {
6061
`;
6162

6263
expect(ElasticApiParser.findApiVersionFile('5.0')).toMatch(
63-
// $FlowFixMe
6464
'elasticsearch/src/lib/apis/5_0.js'
6565
);
6666
expect(ElasticApiParser.findApiVersionFile('2.4')).toMatch(
67-
// $FlowFixMe
6867
'elasticsearch/src/lib/apis/2_4.js'
6968
);
7069
expect(ElasticApiParser.findApiVersionFile('1.7')).toMatch(
71-
// $FlowFixMe
7270
'elasticsearch/src/lib/apis/1_7.js'
7371
);
7472
expect(ElasticApiParser.findApiVersionFile('_default')).toMatch(
75-
// $FlowFixMe
7673
'elasticsearch/src/lib/apis/5_0.js'
7774
);
7875

@@ -82,6 +79,7 @@ describe('ElasticApiParser', () => {
8279

8380
it('should find proper version in elasticsearch 13.x', () => {
8481
const { loadApiListFile } = ElasticApiParser;
82+
8583
// $FlowFixMe
8684
ElasticApiParser.loadApiListFile = () =>
8785
`
@@ -100,19 +98,15 @@ describe('ElasticApiParser', () => {
10098
`;
10199

102100
expect(ElasticApiParser.findApiVersionFile('5.0')).toMatch(
103-
// $FlowFixMe
104101
'elasticsearch/src/lib/apis/5_0.js'
105102
);
106103
expect(ElasticApiParser.findApiVersionFile('2.4')).toMatch(
107-
// $FlowFixMe
108104
'elasticsearch/src/lib/apis/2_4.js'
109105
);
110106
expect(ElasticApiParser.findApiVersionFile('1.7')).toMatch(
111-
// $FlowFixMe
112107
'elasticsearch/src/lib/apis/1_7.js'
113108
);
114109
expect(ElasticApiParser.findApiVersionFile('_default')).toMatch(
115-
// $FlowFixMe
116110
'elasticsearch/src/lib/apis/5_3.js'
117111
);
118112

@@ -267,8 +261,7 @@ describe('ElasticApiParser', () => {
267261
ElasticApiParser.parseSource('');
268262
}).toThrowError('Empty source');
269263
expect(() => {
270-
// $FlowFixMe
271-
ElasticApiParser.parseSource(123);
264+
ElasticApiParser.parseSource((123: any));
272265
}).toThrowError('should be non-empty string');
273266
});
274267

@@ -482,23 +475,23 @@ describe('ElasticApiParser', () => {
482475
});
483476

484477
it('should combine nested field in GraphQLObjectType', () => {
485-
const reFields = parser.reassembleNestedFields({
478+
const reFields: any = parser.reassembleNestedFields({
486479
'cat.field1': { type: GraphQLString },
487480
'cat.field2': { type: GraphQLString },
488481
'index.exists': { type: GraphQLBoolean },
489482
});
490483
expect(Object.keys(reFields).length).toEqual(2);
491484
expect(reFields.cat).toBeDefined();
492485
expect(reFields.cat.type).toBeInstanceOf(GraphQLObjectType);
493-
// $FlowFixMe
486+
494487
const tc = TypeComposer.create(reFields.cat.type);
495488
expect(tc.getFieldNames()).toEqual(['field1', 'field2']);
496489
expect(tc.getFieldType('field1')).toEqual(GraphQLString);
497490
expect(tc.getFieldType('field2')).toEqual(GraphQLString);
498491

499492
expect(reFields.index).toBeDefined();
500493
expect(reFields.index.type).toBeInstanceOf(GraphQLObjectType);
501-
// $FlowFixMe
494+
502495
const tc2 = TypeComposer.create(reFields.index.type);
503496
expect(tc2.getFieldNames()).toEqual(['exists']);
504497
expect(tc2.getFieldType('exists')).toEqual(GraphQLBoolean);
@@ -508,8 +501,7 @@ describe('ElasticApiParser', () => {
508501
describe('generateFieldConfig()', () => {
509502
it('should throw error if provided empty method name', () => {
510503
expect(() => {
511-
// $FlowFixMe
512-
parser.generateFieldConfig();
504+
parser.generateFieldConfig((undefined: any));
513505
}).toThrowError('provide Elastic search method');
514506
});
515507

src/__tests__/mappingConverter-test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ const mapping = {
6060
describe('PropertiesConverter', () => {
6161
describe('convertToSourceTC()', () => {
6262
it('should throw error on empty mapping', () => {
63-
// $FlowFixMe
64-
expect(() => convertToSourceTC()).toThrowError('incorrect mapping');
63+
expect(() => {
64+
// $FlowFixMe
65+
convertToSourceTC();
66+
}).toThrowError('incorrect mapping');
6567
});
6668

6769
it('should throw error on empty typeName', () => {

src/elasticApiFieldConfig.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ElasticApiParser from './ElasticApiParser';
1111

1212
const DEFAULT_ELASTIC_API_VERSION = '_default';
1313

14-
export function elasticApiFieldConfig(esClientOrOpts: Object): GraphQLFieldConfig<*, *> {
14+
export function elasticApiFieldConfig(esClientOrOpts: Object): GraphQLFieldConfig<any, any> {
1515
if (!esClientOrOpts || typeof esClientOrOpts !== 'object') {
1616
throw new Error(
1717
'You should provide ElasticClient instance or ElasticClientConfig in first argument.'
@@ -25,7 +25,7 @@ export function elasticApiFieldConfig(esClientOrOpts: Object): GraphQLFieldConfi
2525
}
2626
}
2727

28-
function instanceElasticClient(elasticClient: Object): GraphQLFieldConfig<*, *> {
28+
function instanceElasticClient(elasticClient: Object): GraphQLFieldConfig<any, any> {
2929
const apiVersion = elasticClient.transport._config.apiVersion || DEFAULT_ELASTIC_API_VERSION;
3030
const prefix = `ElasticAPI${apiVersion.replace('.', '')}`;
3131

@@ -44,7 +44,7 @@ function instanceElasticClient(elasticClient: Object): GraphQLFieldConfig<*, *>
4444
};
4545
}
4646

47-
function contextElasticClient(elasticConfig: Object): GraphQLFieldConfig<*, *> {
47+
function contextElasticClient(elasticConfig: Object): GraphQLFieldConfig<any, any> {
4848
if (!elasticConfig.apiVersion) {
4949
elasticConfig.apiVersion = DEFAULT_ELASTIC_API_VERSION;
5050
}

src/elasticDSL/Aggs/Aggs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ export function convertAggsRules(rules: GqlAggRules): ElasticAggsRulesT {
5151
if (key === 'aggs' && rules.aggs) {
5252
result.aggs = convertAggsBlocks(rules.aggs);
5353
} else {
54-
// $FlowFixMe
5554
result[key] = rules[key];
5655
}
5756
});
58-
return result;
57+
return (result: any);
5958
}

src/elasticDSL/Commons/Date.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* @flow */
22
/* eslint-disable no-unused-vars */
33

4-
import { InputTypeComposer } from 'graphql-compose';
4+
import { InputTypeComposer, type ComposeInputFieldConfigAsObject } from 'graphql-compose';
55
import { getTypeName, getOrSetType, desc } from '../../utils';
66

7-
export function getDateRangeITC(opts: mixed = {}): mixed {
7+
export function getDateRangeITC(opts: mixed = {}): InputTypeComposer {
88
const name = getTypeName('DateRange', opts);
99
const description = desc(`Date range where \`from\` value includes and \`to\` value excludes.`);
1010

@@ -20,7 +20,7 @@ export function getDateRangeITC(opts: mixed = {}): mixed {
2020
);
2121
}
2222

23-
export function getDateFormatFC(opts: mixed = {}): mixed {
23+
export function getDateFormatFC(opts: mixed = {}): ComposeInputFieldConfigAsObject {
2424
return {
2525
type: 'String',
2626
description: desc(
@@ -32,7 +32,7 @@ export function getDateFormatFC(opts: mixed = {}): mixed {
3232
};
3333
}
3434

35-
export function getDateIntervalFC(opts: mixed = {}): mixed {
35+
export function getDateIntervalFC(opts: mixed = {}): ComposeInputFieldConfigAsObject {
3636
return {
3737
type: 'String',
3838
description: desc(
@@ -45,7 +45,7 @@ export function getDateIntervalFC(opts: mixed = {}): mixed {
4545
};
4646
}
4747

48-
export function getDateMathFC(opts: mixed = {}): mixed {
48+
export function getDateMathFC(opts: mixed = {}): ComposeInputFieldConfigAsObject {
4949
return {
5050
type: 'String',
5151
description: desc(
@@ -59,7 +59,7 @@ export function getDateMathFC(opts: mixed = {}): mixed {
5959
};
6060
}
6161

62-
export function getDateTimeZoneFC(opts: mixed = {}): mixed {
62+
export function getDateTimeZoneFC(opts: mixed = {}): ComposeInputFieldConfigAsObject {
6363
return {
6464
type: 'String',
6565
description: desc(

src/elasticDSL/Commons/FieldNames.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ export function getFieldConfigMap(
200200

201201
const fcMap = {};
202202
if (addAll) {
203-
// $FlowFixMe
204203
fcMap._all = fc;
205204
}
206205
getFieldNamesByElasticType(opts.fieldMap, types).forEach(fieldName => {

src/elasticDSL/Commons/Float.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { InputTypeComposer } from 'graphql-compose';
55
import { getTypeName, getOrSetType, desc } from '../../utils';
66

7-
export function getFloatRangeITC(opts: mixed = {}): mixed {
7+
export function getFloatRangeITC(opts: mixed = {}): InputTypeComposer {
88
const name = getTypeName('FloatRange', opts);
99
const description = desc(`Float range where \`from\` value includes and \`to\` value excludes.`);
1010

@@ -20,7 +20,7 @@ export function getFloatRangeITC(opts: mixed = {}): mixed {
2020
);
2121
}
2222

23-
export function getFloatRangeKeyedITC(opts: mixed = {}): mixed {
23+
export function getFloatRangeKeyedITC(opts: mixed = {}): InputTypeComposer {
2424
const name = getTypeName('FloatRangeKeyed', opts);
2525
const description = desc(
2626
`

src/elasticDSL/Commons/Geo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22
/* eslint-disable no-unused-vars */
33

4-
import { InputTypeComposer } from 'graphql-compose';
4+
import { InputTypeComposer, type ComposeInputFieldConfigAsObject } from 'graphql-compose';
55
import { GraphQLScalarType, Kind } from 'graphql-compose/lib/graphql';
66
import { getTypeName, getOrSetType, desc } from '../../utils';
77

@@ -45,11 +45,11 @@ export const ElasticGeoPointType = new GraphQLScalarType({
4545
},
4646
});
4747

48-
export function getGeoPointFC(opts: mixed = {}): mixed {
48+
export function getGeoPointFC(opts: mixed = {}): GraphQLScalarType {
4949
return ElasticGeoPointType;
5050
}
5151

52-
export function getDistanceUnitFC(opts: mixed = {}): mixed {
52+
export function getDistanceUnitFC(opts: mixed = {}): ComposeInputFieldConfigAsObject {
5353
return {
5454
type: 'String',
5555
description: desc(
@@ -62,7 +62,7 @@ export function getDistanceUnitFC(opts: mixed = {}): mixed {
6262
};
6363
}
6464

65-
export function getDistanceCalculationModeFC(opts: mixed = {}): mixed {
65+
export function getDistanceCalculationModeFC(opts: mixed = {}): ComposeInputFieldConfigAsObject {
6666
return {
6767
type: 'String',
6868
description: desc(

src/elasticDSL/Commons/Ip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { InputTypeComposer } from 'graphql-compose';
55
import { getTypeName, getOrSetType, desc } from '../../utils';
66

7-
export function getIpRangeTypeITC(opts: mixed = {}): mixed {
7+
export function getIpRangeTypeITC(opts: mixed = {}): InputTypeComposer {
88
const name = getTypeName('IpRangeType', opts);
99
const description = desc(`Ip range where \`from\` value includes and \`to\` value excludes.`);
1010

0 commit comments

Comments
 (0)