Skip to content

Commit 17a0bfd

Browse files
IvanGoncharovleebyron
authored andcommitted
Cleanup Enums (#1201)
* Convert 'Kind' to object and expose 'KindEnum' * Create 'TokenKindEnum' type. * Move 'literalTok' outside of 'readToken' * Avoid using enums as runtime values Enum values are best left as representative opaque values, so we should avoid `value.length`. Minor variable name changes to preserve existing formatting * Which makes literalTok uncalled
1 parent b3787bb commit 17a0bfd

28 files changed

+247
-332
lines changed

src/execution/execute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type { ObjMap } from '../jsutils/ObjMap';
2020
import type { MaybePromise } from '../jsutils/MaybePromise';
2121

2222
import { typeFromAST } from '../utilities/typeFromAST';
23-
import * as Kind from '../language/kinds';
23+
import { Kind } from '../language/kinds';
2424
import {
2525
getVariableValues,
2626
getArgumentValues,

src/execution/values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import keyMap from '../jsutils/keyMap';
1414
import { coerceValue } from '../utilities/coerceValue';
1515
import { typeFromAST } from '../utilities/typeFromAST';
1616
import { valueFromAST } from '../utilities/valueFromAST';
17-
import * as Kind from '../language/kinds';
17+
import { Kind } from '../language/kinds';
1818
import { print } from '../language/printer';
1919
import { isInputType, isNonNullType } from '../type/definition';
2020
import type { ObjMap } from '../jsutils/ObjMap';

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ export type {
247247
TypeExtensionNode,
248248
ObjectTypeExtensionNode,
249249
DirectiveDefinitionNode,
250+
KindEnum,
251+
TokenKindEnum,
250252
DirectiveLocationEnum,
251253
} from './language';
252254

src/language/__tests__/parser-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import * as Kind from '../kinds';
8+
import { Kind } from '../kinds';
99
import { expect } from 'chai';
1010
import { describe, it } from 'mocha';
1111
import { parse, parseValue, parseType } from '../parser';

src/language/__tests__/visitor-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { join } from 'path';
1515
import { TypeInfo } from '../../utilities/TypeInfo';
1616
import { testSchema } from '../../validation/__tests__/harness';
1717
import { getNamedType, isCompositeType } from '../../type';
18-
import * as Kind from '../kinds';
18+
import { Kind } from '../kinds';
1919

2020
function getNodeByPath(ast, path) {
2121
let result = ast;

src/language/ast.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import type { Source } from './source';
11+
import type { TokenKindEnum } from './lexer';
1112

1213
/**
1314
* Contains a range of UTF-8 character offsets and token references that
@@ -40,35 +41,6 @@ export type Location = {
4041
+source: Source,
4142
};
4243

43-
/**
44-
* Represents the different kinds of tokens in a GraphQL document.
45-
* This type is not inlined in `Token` to fix syntax highlighting on GitHub
46-
* *only*.
47-
*/
48-
type TokenKind =
49-
| '<SOF>'
50-
| '<EOF>'
51-
| '!'
52-
| '$'
53-
| '&'
54-
| '('
55-
| ')'
56-
| '...'
57-
| ':'
58-
| '='
59-
| '@'
60-
| '['
61-
| ']'
62-
| '{'
63-
| '|'
64-
| '}'
65-
| 'Name'
66-
| 'Int'
67-
| 'Float'
68-
| 'String'
69-
| 'BlockString'
70-
| 'Comment';
71-
7244
/**
7345
* Represents a range of characters represented by a lexical token
7446
* within a Source.
@@ -77,7 +49,7 @@ export type Token = {
7749
/**
7850
* The kind of Token.
7951
*/
80-
+kind: TokenKind,
52+
+kind: TokenKindEnum,
8153

8254
/**
8355
* The character offset at which this Node begins.

src/language/directiveLocation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* The set of allowed directive location values.
1212
*/
13-
export const DirectiveLocation = {
13+
export const DirectiveLocation = Object.freeze({
1414
// Request Definitions
1515
QUERY: 'QUERY',
1616
MUTATION: 'MUTATION',
@@ -31,9 +31,9 @@ export const DirectiveLocation = {
3131
ENUM_VALUE: 'ENUM_VALUE',
3232
INPUT_OBJECT: 'INPUT_OBJECT',
3333
INPUT_FIELD_DEFINITION: 'INPUT_FIELD_DEFINITION',
34-
};
34+
});
3535

3636
/**
3737
* The enum type representing the directive location values.
3838
*/
39-
export type DirectiveLocationEnum = $Keys<typeof DirectiveLocation>;
39+
export type DirectiveLocationEnum = $Values<typeof DirectiveLocation>;

src/language/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
export { getLocation } from './location';
1111
export type { SourceLocation } from './location';
12-
import * as Kind from './kinds';
13-
export { Kind };
12+
export { Kind } from './kinds';
13+
export type { KindEnum } from './kinds';
1414
export { createLexer, TokenKind } from './lexer';
1515
export { parse, parseValue, parseType } from './parser';
1616
export { print } from './printer';
@@ -24,7 +24,7 @@ export {
2424
} from './visitor';
2525
export type { ASTVisitor, Visitor, VisitFn, VisitorKeyMap } from './visitor';
2626

27-
export type { Lexer } from './lexer';
27+
export type { Lexer, TokenKindEnum } from './lexer';
2828
export type { ParseOptions } from './parser';
2929

3030
export type {

src/language/kinds.js

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,74 @@
77
* @flow
88
*/
99

10-
// Name
11-
12-
export const NAME = 'Name';
13-
14-
// Document
15-
16-
export const DOCUMENT = 'Document';
17-
export const OPERATION_DEFINITION = 'OperationDefinition';
18-
export const VARIABLE_DEFINITION = 'VariableDefinition';
19-
export const VARIABLE = 'Variable';
20-
export const SELECTION_SET = 'SelectionSet';
21-
export const FIELD = 'Field';
22-
export const ARGUMENT = 'Argument';
23-
24-
// Fragments
25-
26-
export const FRAGMENT_SPREAD = 'FragmentSpread';
27-
export const INLINE_FRAGMENT = 'InlineFragment';
28-
export const FRAGMENT_DEFINITION = 'FragmentDefinition';
29-
30-
// Values
31-
32-
export const INT = 'IntValue';
33-
export const FLOAT = 'FloatValue';
34-
export const STRING = 'StringValue';
35-
export const BOOLEAN = 'BooleanValue';
36-
export const NULL = 'NullValue';
37-
export const ENUM = 'EnumValue';
38-
export const LIST = 'ListValue';
39-
export const OBJECT = 'ObjectValue';
40-
export const OBJECT_FIELD = 'ObjectField';
41-
42-
// Directives
43-
44-
export const DIRECTIVE = 'Directive';
45-
46-
// Types
47-
48-
export const NAMED_TYPE = 'NamedType';
49-
export const LIST_TYPE = 'ListType';
50-
export const NON_NULL_TYPE = 'NonNullType';
51-
52-
// Type System Definitions
53-
54-
export const SCHEMA_DEFINITION = 'SchemaDefinition';
55-
export const OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition';
56-
57-
// Type Definitions
58-
59-
export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
60-
export const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition';
61-
export const FIELD_DEFINITION = 'FieldDefinition';
62-
export const INPUT_VALUE_DEFINITION = 'InputValueDefinition';
63-
export const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition';
64-
export const UNION_TYPE_DEFINITION = 'UnionTypeDefinition';
65-
export const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
66-
export const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
67-
export const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition';
68-
69-
// Type Extensions
70-
71-
export const SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension';
72-
export const OBJECT_TYPE_EXTENSION = 'ObjectTypeExtension';
73-
export const INTERFACE_TYPE_EXTENSION = 'InterfaceTypeExtension';
74-
export const UNION_TYPE_EXTENSION = 'UnionTypeExtension';
75-
export const ENUM_TYPE_EXTENSION = 'EnumTypeExtension';
76-
export const INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension';
77-
78-
// Directive Definitions
10+
/**
11+
* The set of allowed kind values for AST nodes.
12+
*/
13+
export const Kind = Object.freeze({
14+
// Name
15+
NAME: 'Name',
16+
17+
// Document
18+
DOCUMENT: 'Document',
19+
OPERATION_DEFINITION: 'OperationDefinition',
20+
VARIABLE_DEFINITION: 'VariableDefinition',
21+
VARIABLE: 'Variable',
22+
SELECTION_SET: 'SelectionSet',
23+
FIELD: 'Field',
24+
ARGUMENT: 'Argument',
25+
26+
// Fragments
27+
FRAGMENT_SPREAD: 'FragmentSpread',
28+
INLINE_FRAGMENT: 'InlineFragment',
29+
FRAGMENT_DEFINITION: 'FragmentDefinition',
30+
31+
// Values
32+
INT: 'IntValue',
33+
FLOAT: 'FloatValue',
34+
STRING: 'StringValue',
35+
BOOLEAN: 'BooleanValue',
36+
NULL: 'NullValue',
37+
ENUM: 'EnumValue',
38+
LIST: 'ListValue',
39+
OBJECT: 'ObjectValue',
40+
OBJECT_FIELD: 'ObjectField',
41+
42+
// Directives
43+
DIRECTIVE: 'Directive',
44+
45+
// Types
46+
NAMED_TYPE: 'NamedType',
47+
LIST_TYPE: 'ListType',
48+
NON_NULL_TYPE: 'NonNullType',
49+
50+
// Type System Definitions
51+
SCHEMA_DEFINITION: 'SchemaDefinition',
52+
OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition',
53+
54+
// Type Definitions
55+
SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition',
56+
OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition',
57+
FIELD_DEFINITION: 'FieldDefinition',
58+
INPUT_VALUE_DEFINITION: 'InputValueDefinition',
59+
INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition',
60+
UNION_TYPE_DEFINITION: 'UnionTypeDefinition',
61+
ENUM_TYPE_DEFINITION: 'EnumTypeDefinition',
62+
ENUM_VALUE_DEFINITION: 'EnumValueDefinition',
63+
INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition',
64+
65+
// Type Extensions
66+
SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension',
67+
OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension',
68+
INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension',
69+
UNION_TYPE_EXTENSION: 'UnionTypeExtension',
70+
ENUM_TYPE_EXTENSION: 'EnumTypeExtension',
71+
INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension',
72+
73+
// Directive Definitions
74+
DIRECTIVE_DEFINITION: 'DirectiveDefinition',
75+
});
7976

80-
export const DIRECTIVE_DEFINITION = 'DirectiveDefinition';
77+
/**
78+
* The enum type representing the possible kind values of AST nodes.
79+
*/
80+
export type KindEnum = $Values<typeof Kind>;

0 commit comments

Comments
 (0)