Skip to content

Commit

Permalink
Cleanup Enums (#1201)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
IvanGoncharov authored and leebyron committed Jan 18, 2018
1 parent b3787bb commit 17a0bfd
Show file tree
Hide file tree
Showing 28 changed files with 247 additions and 332 deletions.
2 changes: 1 addition & 1 deletion src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { ObjMap } from '../jsutils/ObjMap';
import type { MaybePromise } from '../jsutils/MaybePromise';

import { typeFromAST } from '../utilities/typeFromAST';
import * as Kind from '../language/kinds';
import { Kind } from '../language/kinds';
import {
getVariableValues,
getArgumentValues,
Expand Down
2 changes: 1 addition & 1 deletion src/execution/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import keyMap from '../jsutils/keyMap';
import { coerceValue } from '../utilities/coerceValue';
import { typeFromAST } from '../utilities/typeFromAST';
import { valueFromAST } from '../utilities/valueFromAST';
import * as Kind from '../language/kinds';
import { Kind } from '../language/kinds';
import { print } from '../language/printer';
import { isInputType, isNonNullType } from '../type/definition';
import type { ObjMap } from '../jsutils/ObjMap';
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ export type {
TypeExtensionNode,
ObjectTypeExtensionNode,
DirectiveDefinitionNode,
KindEnum,
TokenKindEnum,
DirectiveLocationEnum,
} from './language';

Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import * as Kind from '../kinds';
import { Kind } from '../kinds';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse, parseValue, parseType } from '../parser';
Expand Down
2 changes: 1 addition & 1 deletion src/language/__tests__/visitor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { join } from 'path';
import { TypeInfo } from '../../utilities/TypeInfo';
import { testSchema } from '../../validation/__tests__/harness';
import { getNamedType, isCompositeType } from '../../type';
import * as Kind from '../kinds';
import { Kind } from '../kinds';

function getNodeByPath(ast, path) {
let result = ast;
Expand Down
32 changes: 2 additions & 30 deletions src/language/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import type { Source } from './source';
import type { TokenKindEnum } from './lexer';

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

/**
* Represents the different kinds of tokens in a GraphQL document.
* This type is not inlined in `Token` to fix syntax highlighting on GitHub
* *only*.
*/
type TokenKind =
| '<SOF>'
| '<EOF>'
| '!'
| '$'
| '&'
| '('
| ')'
| '...'
| ':'
| '='
| '@'
| '['
| ']'
| '{'
| '|'
| '}'
| 'Name'
| 'Int'
| 'Float'
| 'String'
| 'BlockString'
| 'Comment';

/**
* Represents a range of characters represented by a lexical token
* within a Source.
Expand All @@ -77,7 +49,7 @@ export type Token = {
/**
* The kind of Token.
*/
+kind: TokenKind,
+kind: TokenKindEnum,

/**
* The character offset at which this Node begins.
Expand Down
6 changes: 3 additions & 3 deletions src/language/directiveLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* The set of allowed directive location values.
*/
export const DirectiveLocation = {
export const DirectiveLocation = Object.freeze({
// Request Definitions
QUERY: 'QUERY',
MUTATION: 'MUTATION',
Expand All @@ -31,9 +31,9 @@ export const DirectiveLocation = {
ENUM_VALUE: 'ENUM_VALUE',
INPUT_OBJECT: 'INPUT_OBJECT',
INPUT_FIELD_DEFINITION: 'INPUT_FIELD_DEFINITION',
};
});

/**
* The enum type representing the directive location values.
*/
export type DirectiveLocationEnum = $Keys<typeof DirectiveLocation>;
export type DirectiveLocationEnum = $Values<typeof DirectiveLocation>;
6 changes: 3 additions & 3 deletions src/language/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

export { getLocation } from './location';
export type { SourceLocation } from './location';
import * as Kind from './kinds';
export { Kind };
export { Kind } from './kinds';
export type { KindEnum } from './kinds';
export { createLexer, TokenKind } from './lexer';
export { parse, parseValue, parseType } from './parser';
export { print } from './printer';
Expand All @@ -24,7 +24,7 @@ export {
} from './visitor';
export type { ASTVisitor, Visitor, VisitFn, VisitorKeyMap } from './visitor';

export type { Lexer } from './lexer';
export type { Lexer, TokenKindEnum } from './lexer';
export type { ParseOptions } from './parser';

export type {
Expand Down
140 changes: 70 additions & 70 deletions src/language/kinds.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,74 @@
* @flow
*/

// Name

export const NAME = 'Name';

// Document

export const DOCUMENT = 'Document';
export const OPERATION_DEFINITION = 'OperationDefinition';
export const VARIABLE_DEFINITION = 'VariableDefinition';
export const VARIABLE = 'Variable';
export const SELECTION_SET = 'SelectionSet';
export const FIELD = 'Field';
export const ARGUMENT = 'Argument';

// Fragments

export const FRAGMENT_SPREAD = 'FragmentSpread';
export const INLINE_FRAGMENT = 'InlineFragment';
export const FRAGMENT_DEFINITION = 'FragmentDefinition';

// Values

export const INT = 'IntValue';
export const FLOAT = 'FloatValue';
export const STRING = 'StringValue';
export const BOOLEAN = 'BooleanValue';
export const NULL = 'NullValue';
export const ENUM = 'EnumValue';
export const LIST = 'ListValue';
export const OBJECT = 'ObjectValue';
export const OBJECT_FIELD = 'ObjectField';

// Directives

export const DIRECTIVE = 'Directive';

// Types

export const NAMED_TYPE = 'NamedType';
export const LIST_TYPE = 'ListType';
export const NON_NULL_TYPE = 'NonNullType';

// Type System Definitions

export const SCHEMA_DEFINITION = 'SchemaDefinition';
export const OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition';

// Type Definitions

export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
export const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition';
export const FIELD_DEFINITION = 'FieldDefinition';
export const INPUT_VALUE_DEFINITION = 'InputValueDefinition';
export const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition';
export const UNION_TYPE_DEFINITION = 'UnionTypeDefinition';
export const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
export const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
export const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition';

// Type Extensions

export const SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension';
export const OBJECT_TYPE_EXTENSION = 'ObjectTypeExtension';
export const INTERFACE_TYPE_EXTENSION = 'InterfaceTypeExtension';
export const UNION_TYPE_EXTENSION = 'UnionTypeExtension';
export const ENUM_TYPE_EXTENSION = 'EnumTypeExtension';
export const INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension';

// Directive Definitions
/**
* The set of allowed kind values for AST nodes.
*/
export const Kind = Object.freeze({
// Name
NAME: 'Name',

// Document
DOCUMENT: 'Document',
OPERATION_DEFINITION: 'OperationDefinition',
VARIABLE_DEFINITION: 'VariableDefinition',
VARIABLE: 'Variable',
SELECTION_SET: 'SelectionSet',
FIELD: 'Field',
ARGUMENT: 'Argument',

// Fragments
FRAGMENT_SPREAD: 'FragmentSpread',
INLINE_FRAGMENT: 'InlineFragment',
FRAGMENT_DEFINITION: 'FragmentDefinition',

// Values
INT: 'IntValue',
FLOAT: 'FloatValue',
STRING: 'StringValue',
BOOLEAN: 'BooleanValue',
NULL: 'NullValue',
ENUM: 'EnumValue',
LIST: 'ListValue',
OBJECT: 'ObjectValue',
OBJECT_FIELD: 'ObjectField',

// Directives
DIRECTIVE: 'Directive',

// Types
NAMED_TYPE: 'NamedType',
LIST_TYPE: 'ListType',
NON_NULL_TYPE: 'NonNullType',

// Type System Definitions
SCHEMA_DEFINITION: 'SchemaDefinition',
OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition',

// Type Definitions
SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition',
OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition',
FIELD_DEFINITION: 'FieldDefinition',
INPUT_VALUE_DEFINITION: 'InputValueDefinition',
INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition',
UNION_TYPE_DEFINITION: 'UnionTypeDefinition',
ENUM_TYPE_DEFINITION: 'EnumTypeDefinition',
ENUM_VALUE_DEFINITION: 'EnumValueDefinition',
INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition',

// Type Extensions
SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension',
OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension',
INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension',
UNION_TYPE_EXTENSION: 'UnionTypeExtension',
ENUM_TYPE_EXTENSION: 'EnumTypeExtension',
INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension',

// Directive Definitions
DIRECTIVE_DEFINITION: 'DirectiveDefinition',
});

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

0 comments on commit 17a0bfd

Please sign in to comment.