-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
no-unreachable-types.ts
85 lines (77 loc) · 2.37 KB
/
no-unreachable-types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Kind } from 'graphql';
import { GraphQLESLintRule } from '../types';
import { getLocation, requireReachableTypesFromContext } from '../utils';
const UNREACHABLE_TYPE = 'UNREACHABLE_TYPE';
const RULE_NAME = 'no-unreachable-types';
const rule: GraphQLESLintRule = {
meta: {
messages: {
[UNREACHABLE_TYPE]: `Type "{{ typeName }}" is unreachable`,
},
docs: {
description: `Requires all types to be reachable at some level by root level fields.`,
category: 'Best Practices',
url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/${RULE_NAME}.md`,
requiresSchema: true,
examples: [
{
title: 'Incorrect',
code: /* GraphQL */ `
type User {
id: ID!
name: String
}
type Query {
me: String
}
`,
},
{
title: 'Correct',
code: /* GraphQL */ `
type User {
id: ID!
name: String
}
type Query {
me: User
}
`,
},
],
},
fixable: 'code',
type: 'suggestion',
schema: [],
},
create(context) {
const reachableTypes = requireReachableTypesFromContext(RULE_NAME, context);
function ensureReachability(node): void {
const typeName = node.name.value;
if (!reachableTypes.has(typeName)) {
context.report({
loc: getLocation(node.name.loc, typeName, { offsetStart: node.kind === Kind.DIRECTIVE_DEFINITION ? 2 : 1 }),
messageId: UNREACHABLE_TYPE,
data: { typeName },
fix: fixer => fixer.remove(node),
});
}
}
return {
DirectiveDefinition: ensureReachability,
ObjectTypeDefinition: ensureReachability,
ObjectTypeExtension: ensureReachability,
InterfaceTypeDefinition: ensureReachability,
InterfaceTypeExtension: ensureReachability,
ScalarTypeDefinition: ensureReachability,
ScalarTypeExtension: ensureReachability,
InputObjectTypeDefinition: ensureReachability,
InputObjectTypeExtension: ensureReachability,
UnionTypeDefinition: ensureReachability,
UnionTypeExtension: ensureReachability,
EnumTypeDefinition: ensureReachability,
EnumTypeExtension: ensureReachability,
};
},
};
export default rule;