Skip to content

Commit

Permalink
Enable '@typescript-eslint/switch-exhaustiveness-check' rule
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Nov 29, 2021
1 parent 37ec050 commit cd78917
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Expand Up @@ -538,7 +538,7 @@ overrides:
'@typescript-eslint/restrict-template-expressions': off #TODO temporarily disabled
'@typescript-eslint/sort-type-union-intersection-members': off # TODO consider
'@typescript-eslint/strict-boolean-expressions': off # TODO consider
'@typescript-eslint/switch-exhaustiveness-check': off #TODO temporarily disabled
'@typescript-eslint/switch-exhaustiveness-check': error
'@typescript-eslint/triple-slash-reference': error
'@typescript-eslint/typedef': off
'@typescript-eslint/unbound-method': off # TODO consider
Expand Down
5 changes: 0 additions & 5 deletions src/__tests__/starWarsSchema.ts
@@ -1,5 +1,3 @@
import { invariant } from '../jsutils/invariant';

import { GraphQLSchema } from '../type/schema';
import { GraphQLString } from '../type/scalars';
import {
Expand Down Expand Up @@ -137,9 +135,6 @@ const characterInterface: GraphQLInterfaceType = new GraphQLInterfaceType({
case 'Droid':
return droidType.name;
}

// istanbul ignore next (Not reachable. All possible types have been considered)
invariant(false);
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/execution/execute.ts
Expand Up @@ -309,6 +309,8 @@ export function buildExecutionContext(
case Kind.FRAGMENT_DEFINITION:
fragments[definition.name.value] = definition;
break;
default:
// ignore non-executable definitions
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/language/parser.ts
Expand Up @@ -619,8 +619,9 @@ export class Parser {
}
}
return this.parseVariable();
default:
throw this.unexpected();
}
throw this.unexpected();
}

parseConstValueLiteral(): ConstValueNode {
Expand Down
8 changes: 4 additions & 4 deletions src/type/schema.ts
Expand Up @@ -9,10 +9,10 @@ import type { Maybe } from '../jsutils/Maybe';
import type { GraphQLError } from '../error/GraphQLError';

import type {
OperationTypeNode,
SchemaDefinitionNode,
SchemaExtensionNode,
} from '../language/ast';
import { OperationTypeNode } from '../language/ast';

import type {
GraphQLType,
Expand Down Expand Up @@ -282,11 +282,11 @@ export class GraphQLSchema {

getRootType(operation: OperationTypeNode): Maybe<GraphQLObjectType> {
switch (operation) {
case 'query':
case OperationTypeNode.QUERY:
return this.getQueryType();
case 'mutation':
case OperationTypeNode.MUTATION:
return this.getMutationType();
case 'subscription':
case OperationTypeNode.SUBSCRIPTION:
return this.getSubscriptionType();
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/utilities/TypeInfo.ts
Expand Up @@ -247,6 +247,8 @@ export class TypeInfo {
this._enumValue = enumValue;
break;
}
default:
// Ignore other nodes
}
}

Expand Down Expand Up @@ -283,6 +285,8 @@ export class TypeInfo {
case Kind.ENUM:
this._enumValue = null;
break;
default:
// Ignore other nodes
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/utilities/__tests__/separateOperations-test.ts
Expand Up @@ -201,6 +201,34 @@ describe('separateOperations', () => {
});
});

it('ignores type definitions', () => {
const ast = parse(`
query Foo {
...Bar
}
fragment Bar on T {
baz
}
scalar Foo
type Bar
`);

const separatedASTs = mapValue(separateOperations(ast), print);
expect(separatedASTs).to.deep.equal({
Foo: dedent`
query Foo {
...Bar
}
fragment Bar on T {
baz
}
`,
});
});

it('handles unknown fragments', () => {
const ast = parse(`
{
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/buildClientSchema.ts
Expand Up @@ -173,6 +173,8 @@ export function buildClientSchema(
function buildType(type: IntrospectionType): GraphQLNamedType {
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
if (type != null && type.name != null && type.kind != null) {
// FIXME: Properly type IntrospectionType, it's a breaking change so fix in v17
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (type.kind) {
case TypeKind.SCALAR:
return buildScalarDef(type);
Expand Down
3 changes: 0 additions & 3 deletions src/utilities/extendSchema.ts
Expand Up @@ -649,9 +649,6 @@ export function extendSchemaImpl(
});
}
}

// istanbul ignore next (Not reachable. All possible type definition nodes have been considered)
invariant(false, 'Unexpected type definition node: ' + inspect(astNode));
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/utilities/separateOperations.ts
Expand Up @@ -31,6 +31,8 @@ export function separateOperations(
definitionNode.selectionSet,
);
break;
default:
// ignore non-executable definitions
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/utilities/valueFromASTUntyped.ts
Expand Up @@ -51,7 +51,4 @@ export function valueFromASTUntyped(
case Kind.VARIABLE:
return variables?.[valueNode.name.value];
}

// istanbul ignore next (Not reachable. All possible value nodes have been considered)
invariant(false, 'Unexpected value node: ' + inspect(valueNode));
}
6 changes: 3 additions & 3 deletions src/validation/rules/KnownDirectivesRule.ts
Expand Up @@ -120,6 +120,9 @@ function getDirectiveLocationForASTPath(
? DirectiveLocation.INPUT_FIELD_DEFINITION
: DirectiveLocation.ARGUMENT_DEFINITION;
}
// istanbul ignore next (Not reachable. All possible types have been considered)
default:
invariant(false, 'Unexpected kind: ' + inspect(appliedTo.kind));
}
}

Expand All @@ -134,7 +137,4 @@ function getDirectiveLocationForOperation(
case OperationTypeNode.SUBSCRIPTION:
return DirectiveLocation.SUBSCRIPTION;
}

// istanbul ignore next (Not reachable. All possible types have been considered)
invariant(false, 'Unexpected operation: ' + inspect(operation));
}
6 changes: 3 additions & 3 deletions src/validation/rules/PossibleTypeExtensionsRule.ts
Expand Up @@ -137,8 +137,8 @@ function extensionKindToTypeName(kind: Kind): string {
return 'enum';
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
return 'input object';
// istanbul ignore next (Not reachable. All possible types have been considered)
default:
invariant(false, 'Unexpected kind: ' + inspect(kind));
}

// istanbul ignore next (Not reachable. All possible types have been considered)
invariant(false, 'Unexpected kind: ' + inspect(kind));
}

0 comments on commit cd78917

Please sign in to comment.