Skip to content

Commit fca503e

Browse files
Enable '@typescript-eslint/switch-exhaustiveness-check' rule (#3391)
1 parent 37ec050 commit fca503e

File tree

13 files changed

+51
-25
lines changed

13 files changed

+51
-25
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ overrides:
538538
'@typescript-eslint/restrict-template-expressions': off #TODO temporarily disabled
539539
'@typescript-eslint/sort-type-union-intersection-members': off # TODO consider
540540
'@typescript-eslint/strict-boolean-expressions': off # TODO consider
541-
'@typescript-eslint/switch-exhaustiveness-check': off #TODO temporarily disabled
541+
'@typescript-eslint/switch-exhaustiveness-check': error
542542
'@typescript-eslint/triple-slash-reference': error
543543
'@typescript-eslint/typedef': off
544544
'@typescript-eslint/unbound-method': off # TODO consider

src/__tests__/starWarsSchema.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { invariant } from '../jsutils/invariant';
2-
31
import { GraphQLSchema } from '../type/schema';
42
import { GraphQLString } from '../type/scalars';
53
import {
@@ -137,9 +135,6 @@ const characterInterface: GraphQLInterfaceType = new GraphQLInterfaceType({
137135
case 'Droid':
138136
return droidType.name;
139137
}
140-
141-
// istanbul ignore next (Not reachable. All possible types have been considered)
142-
invariant(false);
143138
},
144139
});
145140

src/execution/execute.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ export function buildExecutionContext(
309309
case Kind.FRAGMENT_DEFINITION:
310310
fragments[definition.name.value] = definition;
311311
break;
312+
default:
313+
// ignore non-executable definitions
312314
}
313315
}
314316

src/language/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,9 @@ export class Parser {
619619
}
620620
}
621621
return this.parseVariable();
622+
default:
623+
throw this.unexpected();
622624
}
623-
throw this.unexpected();
624625
}
625626

626627
parseConstValueLiteral(): ConstValueNode {

src/type/schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import type { Maybe } from '../jsutils/Maybe';
99
import type { GraphQLError } from '../error/GraphQLError';
1010

1111
import type {
12-
OperationTypeNode,
1312
SchemaDefinitionNode,
1413
SchemaExtensionNode,
1514
} from '../language/ast';
15+
import { OperationTypeNode } from '../language/ast';
1616

1717
import type {
1818
GraphQLType,
@@ -282,11 +282,11 @@ export class GraphQLSchema {
282282

283283
getRootType(operation: OperationTypeNode): Maybe<GraphQLObjectType> {
284284
switch (operation) {
285-
case 'query':
285+
case OperationTypeNode.QUERY:
286286
return this.getQueryType();
287-
case 'mutation':
287+
case OperationTypeNode.MUTATION:
288288
return this.getMutationType();
289-
case 'subscription':
289+
case OperationTypeNode.SUBSCRIPTION:
290290
return this.getSubscriptionType();
291291
}
292292
}

src/utilities/TypeInfo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ export class TypeInfo {
247247
this._enumValue = enumValue;
248248
break;
249249
}
250+
default:
251+
// Ignore other nodes
250252
}
251253
}
252254

@@ -283,6 +285,8 @@ export class TypeInfo {
283285
case Kind.ENUM:
284286
this._enumValue = null;
285287
break;
288+
default:
289+
// Ignore other nodes
286290
}
287291
}
288292
}

src/utilities/__tests__/separateOperations-test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,34 @@ describe('separateOperations', () => {
201201
});
202202
});
203203

204+
it('ignores type definitions', () => {
205+
const ast = parse(`
206+
query Foo {
207+
...Bar
208+
}
209+
210+
fragment Bar on T {
211+
baz
212+
}
213+
214+
scalar Foo
215+
type Bar
216+
`);
217+
218+
const separatedASTs = mapValue(separateOperations(ast), print);
219+
expect(separatedASTs).to.deep.equal({
220+
Foo: dedent`
221+
query Foo {
222+
...Bar
223+
}
224+
225+
fragment Bar on T {
226+
baz
227+
}
228+
`,
229+
});
230+
});
231+
204232
it('handles unknown fragments', () => {
205233
const ast = parse(`
206234
{

src/utilities/buildClientSchema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ export function buildClientSchema(
173173
function buildType(type: IntrospectionType): GraphQLNamedType {
174174
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
175175
if (type != null && type.name != null && type.kind != null) {
176+
// FIXME: Properly type IntrospectionType, it's a breaking change so fix in v17
177+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
176178
switch (type.kind) {
177179
case TypeKind.SCALAR:
178180
return buildScalarDef(type);

src/utilities/extendSchema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,6 @@ export function extendSchemaImpl(
649649
});
650650
}
651651
}
652-
653-
// istanbul ignore next (Not reachable. All possible type definition nodes have been considered)
654-
invariant(false, 'Unexpected type definition node: ' + inspect(astNode));
655652
}
656653
}
657654

src/utilities/separateOperations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export function separateOperations(
3131
definitionNode.selectionSet,
3232
);
3333
break;
34+
default:
35+
// ignore non-executable definitions
3436
}
3537
}
3638

src/utilities/valueFromASTUntyped.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import type { ObjMap } from '../jsutils/ObjMap';
2-
import { inspect } from '../jsutils/inspect';
3-
import { invariant } from '../jsutils/invariant';
42
import { keyValMap } from '../jsutils/keyValMap';
53
import type { Maybe } from '../jsutils/Maybe';
64

@@ -51,7 +49,4 @@ export function valueFromASTUntyped(
5149
case Kind.VARIABLE:
5250
return variables?.[valueNode.name.value];
5351
}
54-
55-
// istanbul ignore next (Not reachable. All possible value nodes have been considered)
56-
invariant(false, 'Unexpected value node: ' + inspect(valueNode));
5752
}

src/validation/rules/KnownDirectivesRule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ function getDirectiveLocationForASTPath(
120120
? DirectiveLocation.INPUT_FIELD_DEFINITION
121121
: DirectiveLocation.ARGUMENT_DEFINITION;
122122
}
123+
// istanbul ignore next (Not reachable. All possible types have been considered)
124+
default:
125+
invariant(false, 'Unexpected kind: ' + inspect(appliedTo.kind));
123126
}
124127
}
125128

@@ -134,7 +137,4 @@ function getDirectiveLocationForOperation(
134137
case OperationTypeNode.SUBSCRIPTION:
135138
return DirectiveLocation.SUBSCRIPTION;
136139
}
137-
138-
// istanbul ignore next (Not reachable. All possible types have been considered)
139-
invariant(false, 'Unexpected operation: ' + inspect(operation));
140140
}

src/validation/rules/PossibleTypeExtensionsRule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ function extensionKindToTypeName(kind: Kind): string {
137137
return 'enum';
138138
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
139139
return 'input object';
140+
// istanbul ignore next (Not reachable. All possible types have been considered)
141+
default:
142+
invariant(false, 'Unexpected kind: ' + inspect(kind));
140143
}
141-
142-
// istanbul ignore next (Not reachable. All possible types have been considered)
143-
invariant(false, 'Unexpected kind: ' + inspect(kind));
144144
}

0 commit comments

Comments
 (0)