Skip to content

Commit 9db9914

Browse files
authored
Fix linter setup (#49)
* fix: activate linting of TypeScript files * chore: ESLint autofixes * refactor: manual lint fixes for QueryComplexity * refactor: manual lint fixes for tests * refactor: manual lint fixes for estimators * refactor: manual lint fixes for index
1 parent a7a5042 commit 9db9914

File tree

14 files changed

+82
-72
lines changed

14 files changed

+82
-72
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/dist

.eslintrc

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,8 @@
1313
"env": {
1414
"browser": true,
1515
"es6": true,
16-
"node": true
17-
},
18-
19-
"globals": {
20-
"describe": true,
21-
"expect": true,
22-
"it": true,
23-
"before": true,
24-
"after": true
16+
"node": true,
17+
"mocha": true
2518
},
2619

2720
"parserOptions": {
@@ -226,4 +219,4 @@
226219
"wrap-regex": 0,
227220
"yoda": [2, "never", {"exceptRange": true}]
228221
}
229-
}
222+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
8-
"lint": "eslint src/**/*.ts",
9-
"lint:fix": "eslint --fix src/**/*.ts",
8+
"lint": "eslint --ext .ts .",
9+
"lint:fix": "npm run lint -- --fix",
1010
"clean": "rimraf dist/*",
1111
"build": "tsc",
1212
"test": "npm run lint && npm run testonly",

src/QueryComplexity.ts

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
/* eslint-disable @typescript-eslint/no-use-before-define */
13
/**
24
* Created by Ivo Meißner on 28.07.17.
35
*/
@@ -11,11 +13,9 @@ import {
1113
ValidationContext,
1214
FragmentDefinitionNode,
1315
OperationDefinitionNode,
14-
DirectiveNode,
1516
FieldNode,
1617
FragmentSpreadNode,
1718
InlineFragmentNode,
18-
assertCompositeType,
1919
GraphQLField, isCompositeType, GraphQLCompositeType, GraphQLFieldMap,
2020
GraphQLSchema, DocumentNode, TypeInfo,
2121
visit, visitWithTypeInfo,
@@ -31,11 +31,11 @@ import {
3131
} from 'graphql';
3232

3333
export type ComplexityEstimatorArgs = {
34-
type: GraphQLCompositeType,
35-
field: GraphQLField<any, any>,
36-
node: FieldNode,
37-
args: {[key: string]: any},
38-
childComplexity: number
34+
type: GraphQLCompositeType;
35+
field: GraphQLField<any, any>;
36+
node: FieldNode;
37+
args: {[key: string]: any};
38+
childComplexity: number;
3939
}
4040

4141
export type ComplexityEstimator = (options: ComplexityEstimatorArgs) => number | void;
@@ -45,27 +45,27 @@ export type Complexity = any;
4545

4646
// Map of complexities for possible types (of Union, Interface types)
4747
type ComplexityMap = {
48-
[typeName: string]: number,
48+
[typeName: string]: number;
4949
}
5050

5151
export interface QueryComplexityOptions {
5252
// The maximum allowed query complexity, queries above this threshold will be rejected
53-
maximumComplexity: number,
53+
maximumComplexity: number;
5454

5555
// The query variables. This is needed because the variables are not available
5656
// in the visitor of the graphql-js library
57-
variables?: Object,
57+
variables?: Record<string, any>;
5858

5959
// specify operation name only when pass multi-operation documents
60-
operationName?: string,
60+
operationName?: string;
6161

6262
// Optional callback function to retrieve the determined query complexity
6363
// Will be invoked whether the query is rejected or not
6464
// This can be used for logging or to implement rate limiting
65-
onComplete?: (complexity: number) => void,
65+
onComplete?: (complexity: number) => void;
6666

6767
// Optional function to create a custom error
68-
createError?: (max: number, actual: number) => GraphQLError,
68+
createError?: (max: number, actual: number) => GraphQLError;
6969

7070
// An array of complexity estimators to use for estimating the complexity
7171
estimators: Array<ComplexityEstimator>;
@@ -79,11 +79,11 @@ function queryComplexityMessage(max: number, actual: number): string {
7979
}
8080

8181
export function getComplexity(options: {
82-
estimators: ComplexityEstimator[],
83-
schema: GraphQLSchema,
84-
query: DocumentNode,
85-
variables?: Object,
86-
operationName?: string
82+
estimators: ComplexityEstimator[];
83+
schema: GraphQLSchema;
84+
query: DocumentNode;
85+
variables?: Record<string, any>;
86+
operationName?: string;
8787
}): number {
8888
const typeInfo = new TypeInfo(options.schema);
8989

@@ -104,7 +104,7 @@ export default class QueryComplexity {
104104
context: ValidationContext;
105105
complexity: number;
106106
options: QueryComplexityOptions;
107-
OperationDefinition: Object;
107+
OperationDefinition: Record<string, any>;
108108
estimators: Array<ComplexityEstimator>;
109109
includeDirectiveDef: GraphQLDirective;
110110
skipDirectiveDef: GraphQLDirective;
@@ -123,15 +123,15 @@ export default class QueryComplexity {
123123

124124
this.includeDirectiveDef = this.context.getSchema().getDirective('include');
125125
this.skipDirectiveDef = this.context.getSchema().getDirective('skip');
126-
this.estimators = options.estimators
126+
this.estimators = options.estimators;
127127

128128
this.OperationDefinition = {
129129
enter: this.onOperationDefinitionEnter,
130130
leave: this.onOperationDefinitionLeave
131131
};
132132
}
133133

134-
onOperationDefinitionEnter(operation: OperationDefinitionNode) {
134+
onOperationDefinitionEnter(operation: OperationDefinitionNode): void {
135135
if (typeof this.options.operationName === 'string' && this.options.operationName !== operation.name.value) {
136136
return;
137137
}
@@ -181,7 +181,7 @@ export default class QueryComplexity {
181181
typeDef: GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType,
182182
): number {
183183
if (node.selectionSet) {
184-
let fields:GraphQLFieldMap<any, any> = {};
184+
let fields: GraphQLFieldMap<any, any> = {};
185185
if (typeDef instanceof GraphQLObjectType || typeDef instanceof GraphQLInterfaceType) {
186186
fields = typeDef.getFields();
187187
}
@@ -191,18 +191,19 @@ export default class QueryComplexity {
191191
if (isAbstractType(typeDef)) {
192192
possibleTypeNames = this.context.getSchema().getPossibleTypes(typeDef).map(t => t.name);
193193
} else {
194-
possibleTypeNames = [typeDef.name];
194+
possibleTypeNames = [ typeDef.name ];
195195
}
196196

197197
// Collect complexities for all possible types individually
198198
const selectionSetComplexities: ComplexityMap = node.selectionSet.selections.reduce(
199199
(complexities: ComplexityMap, childNode: FieldNode | FragmentSpreadNode | InlineFragmentNode) => {
200200
// let nodeComplexity = 0;
201+
let innerComplexities = complexities;
201202

202203
let includeNode = true;
203204
let skipNode = false;
204205

205-
childNode.directives?.forEach((directive: DirectiveNode) => {
206+
for (const directive of childNode.directives ?? []) {
206207
const directiveName = directive.name.value;
207208
switch (directiveName) {
208209
case 'include': {
@@ -216,7 +217,7 @@ export default class QueryComplexity {
216217
break;
217218
}
218219
}
219-
});
220+
}
220221

221222
if (!includeNode || skipNode) {
222223
return complexities;
@@ -258,7 +259,7 @@ export default class QueryComplexity {
258259
const tmpComplexity = estimator(estimatorArgs);
259260

260261
if (typeof tmpComplexity === 'number' && !isNaN(tmpComplexity)) {
261-
complexities = addComplexities(
262+
innerComplexities = addComplexities(
262263
tmpComplexity,
263264
complexities,
264265
possibleTypeNames,
@@ -292,17 +293,17 @@ export default class QueryComplexity {
292293
const nodeComplexity = this.nodeComplexity(fragment, fragmentType);
293294
if (isAbstractType(fragmentType)) {
294295
// Add fragment complexity for all possible types
295-
complexities = addComplexities(
296+
innerComplexities = addComplexities(
296297
nodeComplexity,
297298
complexities,
298299
this.context.getSchema().getPossibleTypes(fragmentType).map(t => t.name),
299300
);
300301
} else {
301302
// Add complexity for object type
302-
complexities = addComplexities(
303+
innerComplexities = addComplexities(
303304
nodeComplexity,
304305
complexities,
305-
[fragmentType.name],
306+
[ fragmentType.name ],
306307
);
307308
}
308309
break;
@@ -319,23 +320,23 @@ export default class QueryComplexity {
319320
const nodeComplexity = this.nodeComplexity(childNode, inlineFragmentType);
320321
if (isAbstractType(inlineFragmentType)) {
321322
// Add fragment complexity for all possible types
322-
complexities = addComplexities(
323+
innerComplexities = addComplexities(
323324
nodeComplexity,
324325
complexities,
325326
this.context.getSchema().getPossibleTypes(inlineFragmentType).map(t => t.name),
326327
);
327328
} else {
328329
// Add complexity for object type
329-
complexities = addComplexities(
330+
innerComplexities = addComplexities(
330331
nodeComplexity,
331332
complexities,
332-
[inlineFragmentType.name],
333+
[ inlineFragmentType.name ],
333334
);
334335
}
335336
break;
336337
}
337338
default: {
338-
complexities = addComplexities(
339+
innerComplexities = addComplexities(
339340
this.nodeComplexity(childNode, typeDef),
340341
complexities,
341342
possibleTypeNames,
@@ -344,7 +345,7 @@ export default class QueryComplexity {
344345
}
345346
}
346347

347-
return complexities;
348+
return innerComplexities;
348349
}, {});
349350
// Only return max complexity of all possible types
350351
if (!selectionSetComplexities) {
@@ -381,8 +382,8 @@ function addComplexities(
381382
possibleTypes: string[],
382383
): ComplexityMap {
383384
for (const type of possibleTypes) {
384-
if (complexityMap.hasOwnProperty(type)) {
385-
complexityMap[type] = complexityMap[type] + complexity;
385+
if (Object.prototype.hasOwnProperty.call(complexityMap, type)) {
386+
complexityMap[type] += complexity;
386387
} else {
387388
complexityMap[type] = complexity;
388389
}

src/__tests__/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'@typescript-eslint/explicit-function-return-type': "off"
4+
}
5+
}

src/__tests__/fixtures/CompatibleValidationContext.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { GraphQLError, TypeInfo, ValidationContext } from "graphql";
2-
import { GraphQLSchema } from "graphql/type/schema";
3-
import { DocumentNode } from "graphql/language/ast";
1+
import { GraphQLError, TypeInfo, ValidationContext } from 'graphql';
2+
import { GraphQLSchema } from 'graphql/type/schema';
3+
import { DocumentNode } from 'graphql/language/ast';
44

55
/**
66
* This class is used to test that validation errors are raised correctly
@@ -18,11 +18,12 @@ export class CompatibleValidationContext extends ValidationContext {
1818
private errors: GraphQLError[] = []
1919

2020
constructor(schema: GraphQLSchema, ast: DocumentNode, typeInfo: TypeInfo) {
21-
super(schema, ast, typeInfo, err => this.errors.push(err));
21+
super(schema, ast, typeInfo, err => this.errors.push(err));
2222
}
2323

2424
getErrors(): ReadonlyArray<GraphQLError> {
25-
// @ts-ignore
26-
return super.getErrors ? super.getErrors() : this.errors
25+
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
26+
// @ts-ignore
27+
return super.getErrors ? super.getErrors() : this.errors;
2728
}
2829
}

src/__tests__/fixtures/schema.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,6 @@ const NameInterface = new GraphQLInterfaceType({
6363
resolveType: () => Item
6464
});
6565

66-
const UnionInterface = new GraphQLInterfaceType({
67-
name: 'UnionInterface',
68-
fields: () => ({
69-
union: { type: Union }
70-
}),
71-
resolveType: () => Item
72-
});
73-
7466
const SecondItem = new GraphQLObjectType({
7567
name: 'SecondItem',
7668
fields: () => ({
@@ -95,6 +87,14 @@ const Union = new GraphQLUnionType({
9587
resolveType: () => Item
9688
});
9789

90+
const UnionInterface = new GraphQLInterfaceType({
91+
name: 'UnionInterface',
92+
fields: () => ({
93+
union: { type: Union }
94+
}),
95+
resolveType: () => Item
96+
});
97+
9898
const SDLInterface = new GraphQLInterfaceType({
9999
name: 'SDLInterface',
100100
fields: {
@@ -108,7 +108,7 @@ const SDL = new GraphQLObjectType({
108108
fields: {
109109
sdl: { type: GraphQLString }
110110
},
111-
interfaces: () => [SDLInterface],
111+
interfaces: () => [ SDLInterface ],
112112
});
113113

114114
const Query = new GraphQLObjectType({
@@ -161,7 +161,7 @@ const Query = new GraphQLObjectType({
161161
},
162162
_service: {type: SDLInterface},
163163
}),
164-
interfaces: () => [NameInterface, UnionInterface,]
164+
interfaces: () => [ NameInterface, UnionInterface, ]
165165
});
166166

167167
export default new GraphQLSchema({

src/estimators/directive/__tests__/directiveEstimator-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ describe('directiveEstimator analysis', () => {
253253
it('should create complexity directive that can be used to generate directive definition', () => {
254254
const complexityDirective = createComplexityDirective();
255255
const codeFirstSchema = new GraphQLSchema({
256-
directives: [complexityDirective]
256+
directives: [ complexityDirective ]
257257
});
258258

259259
// rebuilding code first schema
@@ -270,7 +270,7 @@ describe('directiveEstimator analysis', () => {
270270
it('should create complexity directive with configured name', () => {
271271
const complexityDirective = createComplexityDirective({name: 'cost'});
272272
const codeFirstSchema = new GraphQLSchema({
273-
directives: [complexityDirective]
273+
directives: [ complexityDirective ]
274274
});
275275

276276
// rebuilding code first schema

src/estimators/directive/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { DirectiveLocation } from 'graphql/language/directiveLocation';
55
import get from 'lodash.get';
66

77
export type ComplexityDirectiveOptions = {
8-
name?: string
8+
name?: string;
99
}
1010

11-
export function createComplexityDirective(options?: ComplexityDirectiveOptions) {
11+
export function createComplexityDirective(options?: ComplexityDirectiveOptions): GraphQLDirective {
1212
const mergedOptions = {
1313
name: 'complexity',
1414
...(options || {})
@@ -35,7 +35,7 @@ export function createComplexityDirective(options?: ComplexityDirectiveOptions)
3535
export default function (options: ComplexityDirectiveOptions = {}): ComplexityEstimator {
3636
const directive = createComplexityDirective(options);
3737

38-
return (args: ComplexityEstimatorArgs) => {
38+
return (args: ComplexityEstimatorArgs): number | void => {
3939
// Ignore if astNode is undefined
4040
if (!args.field.astNode) {
4141
return;

src/estimators/fieldExtensions/__tests__/fixtures/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
12
/**
23
* Created by Ivo Meißner on 28.07.17.
34
*/

0 commit comments

Comments
 (0)