Skip to content

Commit 079167d

Browse files
authored
refactor: treeshakable kind enum (#4270)
closes #4253
1 parent 92d4ed0 commit 079167d

File tree

7 files changed

+188
-115
lines changed

7 files changed

+188
-115
lines changed

integrationTests/ts/package.json

-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
"dependencies": {
99
"graphql": "file:../graphql.tgz",
1010
"graphql-esm": "file:../graphql-esm.tgz",
11-
"typescript-4.4": "npm:typescript@4.4.x",
12-
"typescript-4.5": "npm:typescript@4.5.x",
13-
"typescript-4.6": "npm:typescript@4.6.x",
14-
"typescript-4.7": "npm:typescript@4.7.x",
15-
"typescript-4.8": "npm:typescript@4.8.x",
1611
"typescript-4.9": "npm:typescript@4.9.x",
1712
"typescript-5.0": "npm:typescript@5.0.x",
1813
"typescript-5.1": "npm:typescript@5.1.x",

src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ export type {
206206
} from './type/index.js';
207207

208208
// Parse and operate on GraphQL language source files.
209+
// @see https://github.com/typescript-eslint/typescript-eslint/issues/10313
210+
// eslint-disable-next-line @typescript-eslint/consistent-type-exports
211+
export { Kind } from './language/kinds.js';
209212
export {
210213
Token,
211214
Source,
@@ -230,7 +233,6 @@ export {
230233
visitInParallel,
231234
getEnterLeaveForKind,
232235
BREAK,
233-
Kind,
234236
DirectiveLocation,
235237
// Predicates
236238
isDefinitionNode,

src/language/__tests__/kind-test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable @typescript-eslint/no-unused-expressions */
2+
import { describe, it } from 'mocha';
3+
4+
import { Kind } from '../index.js';
5+
6+
describe('Kind', () => {
7+
it('is a term level namespace with term level enum members', () => {
8+
const a: Kind.NAME = Kind.NAME;
9+
a;
10+
const b: Kind = Kind.NAME;
11+
b;
12+
const c: Kind = Kind.ARGUMENT;
13+
c;
14+
});
15+
16+
it('is a type level namespace with type level enum members', () => {
17+
// @ts-expect-error
18+
const a: Kind.NAME = 'bad';
19+
a;
20+
const b: Kind.NAME = 'Name';
21+
b;
22+
// @ts-expect-error
23+
const c: Kind = 'bad';
24+
c;
25+
const d: Kind = 'Name';
26+
d;
27+
const e: Kind = 'Argument';
28+
e;
29+
});
30+
});

src/language/__tests__/predicates-test.ts

+39-39
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ function filterNodes(predicate: (node: ASTNode) => boolean): Array<string> {
2727
describe('AST node predicates', () => {
2828
it('isDefinitionNode', () => {
2929
expect(filterNodes(isDefinitionNode)).to.deep.equal([
30-
'OperationDefinition',
31-
'FragmentDefinition',
32-
'SchemaDefinition',
33-
'ScalarTypeDefinition',
34-
'ObjectTypeDefinition',
35-
'InterfaceTypeDefinition',
36-
'UnionTypeDefinition',
30+
'DirectiveDefinition',
3731
'EnumTypeDefinition',
32+
'EnumTypeExtension',
33+
'FragmentDefinition',
3834
'InputObjectTypeDefinition',
39-
'DirectiveDefinition',
40-
'SchemaExtension',
41-
'ScalarTypeExtension',
42-
'ObjectTypeExtension',
35+
'InputObjectTypeExtension',
36+
'InterfaceTypeDefinition',
4337
'InterfaceTypeExtension',
38+
'ObjectTypeDefinition',
39+
'ObjectTypeExtension',
40+
'OperationDefinition',
41+
'ScalarTypeDefinition',
42+
'ScalarTypeExtension',
43+
'SchemaDefinition',
44+
'SchemaExtension',
45+
'UnionTypeDefinition',
4446
'UnionTypeExtension',
45-
'EnumTypeExtension',
46-
'InputObjectTypeExtension',
4747
]);
4848
});
4949

5050
it('isExecutableDefinitionNode', () => {
5151
expect(filterNodes(isExecutableDefinitionNode)).to.deep.equal([
52-
'OperationDefinition',
5352
'FragmentDefinition',
53+
'OperationDefinition',
5454
]);
5555
});
5656

@@ -64,15 +64,15 @@ describe('AST node predicates', () => {
6464

6565
it('isValueNode', () => {
6666
expect(filterNodes(isValueNode)).to.deep.equal([
67-
'Variable',
68-
'IntValue',
69-
'FloatValue',
70-
'StringValue',
7167
'BooleanValue',
72-
'NullValue',
7368
'EnumValue',
69+
'FloatValue',
70+
'IntValue',
7471
'ListValue',
72+
'NullValue',
7573
'ObjectValue',
74+
'StringValue',
75+
'Variable',
7676
]);
7777
});
7878

@@ -89,56 +89,56 @@ describe('AST node predicates', () => {
8989

9090
it('isTypeNode', () => {
9191
expect(filterNodes(isTypeNode)).to.deep.equal([
92-
'NamedType',
9392
'ListType',
93+
'NamedType',
9494
'NonNullType',
9595
]);
9696
});
9797

9898
it('isTypeSystemDefinitionNode', () => {
9999
expect(filterNodes(isTypeSystemDefinitionNode)).to.deep.equal([
100-
'SchemaDefinition',
101-
'ScalarTypeDefinition',
102-
'ObjectTypeDefinition',
103-
'InterfaceTypeDefinition',
104-
'UnionTypeDefinition',
100+
'DirectiveDefinition',
105101
'EnumTypeDefinition',
106102
'InputObjectTypeDefinition',
107-
'DirectiveDefinition',
103+
'InterfaceTypeDefinition',
104+
'ObjectTypeDefinition',
105+
'ScalarTypeDefinition',
106+
'SchemaDefinition',
107+
'UnionTypeDefinition',
108108
]);
109109
});
110110

111111
it('isTypeDefinitionNode', () => {
112112
expect(filterNodes(isTypeDefinitionNode)).to.deep.equal([
113-
'ScalarTypeDefinition',
114-
'ObjectTypeDefinition',
115-
'InterfaceTypeDefinition',
116-
'UnionTypeDefinition',
117113
'EnumTypeDefinition',
118114
'InputObjectTypeDefinition',
115+
'InterfaceTypeDefinition',
116+
'ObjectTypeDefinition',
117+
'ScalarTypeDefinition',
118+
'UnionTypeDefinition',
119119
]);
120120
});
121121

122122
it('isTypeSystemExtensionNode', () => {
123123
expect(filterNodes(isTypeSystemExtensionNode)).to.deep.equal([
124-
'SchemaExtension',
125-
'ScalarTypeExtension',
126-
'ObjectTypeExtension',
127-
'InterfaceTypeExtension',
128-
'UnionTypeExtension',
129124
'EnumTypeExtension',
130125
'InputObjectTypeExtension',
126+
'InterfaceTypeExtension',
127+
'ObjectTypeExtension',
128+
'ScalarTypeExtension',
129+
'SchemaExtension',
130+
'UnionTypeExtension',
131131
]);
132132
});
133133

134134
it('isTypeExtensionNode', () => {
135135
expect(filterNodes(isTypeExtensionNode)).to.deep.equal([
136-
'ScalarTypeExtension',
137-
'ObjectTypeExtension',
138-
'InterfaceTypeExtension',
139-
'UnionTypeExtension',
140136
'EnumTypeExtension',
141137
'InputObjectTypeExtension',
138+
'InterfaceTypeExtension',
139+
'ObjectTypeExtension',
140+
'ScalarTypeExtension',
141+
'UnionTypeExtension',
142142
]);
143143
});
144144
});

src/language/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export type { SourceLocation } from './location.js';
55

66
export { printLocation, printSourceLocation } from './printLocation.js';
77

8+
// @see https://github.com/typescript-eslint/typescript-eslint/issues/10313
9+
// eslint-disable-next-line @typescript-eslint/consistent-type-exports
810
export { Kind } from './kinds.js';
911

1012
export { TokenKind } from './tokenKind.js';

src/language/kinds.ts

+4-70
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,6 @@
1-
/**
2-
* The set of allowed kind values for AST nodes.
3-
*/
4-
export const Kind = {
5-
/** Name */
6-
NAME: 'Name' as const,
1+
/* eslint-disable import/no-namespace */
2+
import type * as Kind_ from './kinds_.js';
73

8-
/** Document */
9-
DOCUMENT: 'Document' as const,
10-
OPERATION_DEFINITION: 'OperationDefinition' as const,
11-
VARIABLE_DEFINITION: 'VariableDefinition' as const,
12-
SELECTION_SET: 'SelectionSet' as const,
13-
FIELD: 'Field' as const,
14-
ARGUMENT: 'Argument' as const,
15-
FRAGMENT_ARGUMENT: 'FragmentArgument' as const,
4+
export * as Kind from './kinds_.js';
165

17-
/** Fragments */
18-
FRAGMENT_SPREAD: 'FragmentSpread' as const,
19-
INLINE_FRAGMENT: 'InlineFragment' as const,
20-
FRAGMENT_DEFINITION: 'FragmentDefinition' as const,
21-
22-
/** Values */
23-
VARIABLE: 'Variable' as const,
24-
INT: 'IntValue' as const,
25-
FLOAT: 'FloatValue' as const,
26-
STRING: 'StringValue' as const,
27-
BOOLEAN: 'BooleanValue' as const,
28-
NULL: 'NullValue' as const,
29-
ENUM: 'EnumValue' as const,
30-
LIST: 'ListValue' as const,
31-
OBJECT: 'ObjectValue' as const,
32-
OBJECT_FIELD: 'ObjectField' as const,
33-
34-
/** Directives */
35-
DIRECTIVE: 'Directive' as const,
36-
37-
/** Types */
38-
NAMED_TYPE: 'NamedType' as const,
39-
LIST_TYPE: 'ListType' as const,
40-
NON_NULL_TYPE: 'NonNullType' as const,
41-
42-
/** Type System Definitions */
43-
SCHEMA_DEFINITION: 'SchemaDefinition' as const,
44-
OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition' as const,
45-
46-
/** Type Definitions */
47-
SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition' as const,
48-
OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition' as const,
49-
FIELD_DEFINITION: 'FieldDefinition' as const,
50-
INPUT_VALUE_DEFINITION: 'InputValueDefinition' as const,
51-
INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition' as const,
52-
UNION_TYPE_DEFINITION: 'UnionTypeDefinition' as const,
53-
ENUM_TYPE_DEFINITION: 'EnumTypeDefinition' as const,
54-
ENUM_VALUE_DEFINITION: 'EnumValueDefinition' as const,
55-
INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition' as const,
56-
57-
/** Directive Definitions */
58-
DIRECTIVE_DEFINITION: 'DirectiveDefinition' as const,
59-
60-
/** Type System Extensions */
61-
SCHEMA_EXTENSION: 'SchemaExtension' as const,
62-
63-
/** Type Extensions */
64-
SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension' as const,
65-
OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension' as const,
66-
INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension' as const,
67-
UNION_TYPE_EXTENSION: 'UnionTypeExtension' as const,
68-
ENUM_TYPE_EXTENSION: 'EnumTypeExtension' as const,
69-
INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension' as const,
70-
};
71-
// eslint-disable-next-line @typescript-eslint/no-redeclare
72-
export type Kind = (typeof Kind)[keyof typeof Kind];
6+
export type Kind = (typeof Kind_)[keyof typeof Kind_];

0 commit comments

Comments
 (0)