Skip to content

Commit

Permalink
refactor: treeshakable kind enum (graphql#4270)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Nov 22, 2024
1 parent 92d4ed0 commit 079167d
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 115 deletions.
5 changes: 0 additions & 5 deletions integrationTests/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
"dependencies": {
"graphql": "file:../graphql.tgz",
"graphql-esm": "file:../graphql-esm.tgz",
"typescript-4.4": "npm:[email protected]",
"typescript-4.5": "npm:[email protected]",
"typescript-4.6": "npm:[email protected]",
"typescript-4.7": "npm:[email protected]",
"typescript-4.8": "npm:[email protected]",
"typescript-4.9": "npm:[email protected]",
"typescript-5.0": "npm:[email protected]",
"typescript-5.1": "npm:[email protected]",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ export type {
} from './type/index.js';

// Parse and operate on GraphQL language source files.
// @see https://github.com/typescript-eslint/typescript-eslint/issues/10313
// eslint-disable-next-line @typescript-eslint/consistent-type-exports
export { Kind } from './language/kinds.js';
export {
Token,
Source,
Expand All @@ -230,7 +233,6 @@ export {
visitInParallel,
getEnterLeaveForKind,
BREAK,
Kind,
DirectiveLocation,
// Predicates
isDefinitionNode,
Expand Down
30 changes: 30 additions & 0 deletions src/language/__tests__/kind-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { describe, it } from 'mocha';

import { Kind } from '../index.js';

describe('Kind', () => {
it('is a term level namespace with term level enum members', () => {
const a: Kind.NAME = Kind.NAME;
a;
const b: Kind = Kind.NAME;
b;
const c: Kind = Kind.ARGUMENT;
c;
});

it('is a type level namespace with type level enum members', () => {
// @ts-expect-error
const a: Kind.NAME = 'bad';
a;
const b: Kind.NAME = 'Name';
b;
// @ts-expect-error
const c: Kind = 'bad';
c;
const d: Kind = 'Name';
d;
const e: Kind = 'Argument';
e;
});
});
78 changes: 39 additions & 39 deletions src/language/__tests__/predicates-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ function filterNodes(predicate: (node: ASTNode) => boolean): Array<string> {
describe('AST node predicates', () => {
it('isDefinitionNode', () => {
expect(filterNodes(isDefinitionNode)).to.deep.equal([
'OperationDefinition',
'FragmentDefinition',
'SchemaDefinition',
'ScalarTypeDefinition',
'ObjectTypeDefinition',
'InterfaceTypeDefinition',
'UnionTypeDefinition',
'DirectiveDefinition',
'EnumTypeDefinition',
'EnumTypeExtension',
'FragmentDefinition',
'InputObjectTypeDefinition',
'DirectiveDefinition',
'SchemaExtension',
'ScalarTypeExtension',
'ObjectTypeExtension',
'InputObjectTypeExtension',
'InterfaceTypeDefinition',
'InterfaceTypeExtension',
'ObjectTypeDefinition',
'ObjectTypeExtension',
'OperationDefinition',
'ScalarTypeDefinition',
'ScalarTypeExtension',
'SchemaDefinition',
'SchemaExtension',
'UnionTypeDefinition',
'UnionTypeExtension',
'EnumTypeExtension',
'InputObjectTypeExtension',
]);
});

it('isExecutableDefinitionNode', () => {
expect(filterNodes(isExecutableDefinitionNode)).to.deep.equal([
'OperationDefinition',
'FragmentDefinition',
'OperationDefinition',
]);
});

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

it('isValueNode', () => {
expect(filterNodes(isValueNode)).to.deep.equal([
'Variable',
'IntValue',
'FloatValue',
'StringValue',
'BooleanValue',
'NullValue',
'EnumValue',
'FloatValue',
'IntValue',
'ListValue',
'NullValue',
'ObjectValue',
'StringValue',
'Variable',
]);
});

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

it('isTypeNode', () => {
expect(filterNodes(isTypeNode)).to.deep.equal([
'NamedType',
'ListType',
'NamedType',
'NonNullType',
]);
});

it('isTypeSystemDefinitionNode', () => {
expect(filterNodes(isTypeSystemDefinitionNode)).to.deep.equal([
'SchemaDefinition',
'ScalarTypeDefinition',
'ObjectTypeDefinition',
'InterfaceTypeDefinition',
'UnionTypeDefinition',
'DirectiveDefinition',
'EnumTypeDefinition',
'InputObjectTypeDefinition',
'DirectiveDefinition',
'InterfaceTypeDefinition',
'ObjectTypeDefinition',
'ScalarTypeDefinition',
'SchemaDefinition',
'UnionTypeDefinition',
]);
});

it('isTypeDefinitionNode', () => {
expect(filterNodes(isTypeDefinitionNode)).to.deep.equal([
'ScalarTypeDefinition',
'ObjectTypeDefinition',
'InterfaceTypeDefinition',
'UnionTypeDefinition',
'EnumTypeDefinition',
'InputObjectTypeDefinition',
'InterfaceTypeDefinition',
'ObjectTypeDefinition',
'ScalarTypeDefinition',
'UnionTypeDefinition',
]);
});

it('isTypeSystemExtensionNode', () => {
expect(filterNodes(isTypeSystemExtensionNode)).to.deep.equal([
'SchemaExtension',
'ScalarTypeExtension',
'ObjectTypeExtension',
'InterfaceTypeExtension',
'UnionTypeExtension',
'EnumTypeExtension',
'InputObjectTypeExtension',
'InterfaceTypeExtension',
'ObjectTypeExtension',
'ScalarTypeExtension',
'SchemaExtension',
'UnionTypeExtension',
]);
});

it('isTypeExtensionNode', () => {
expect(filterNodes(isTypeExtensionNode)).to.deep.equal([
'ScalarTypeExtension',
'ObjectTypeExtension',
'InterfaceTypeExtension',
'UnionTypeExtension',
'EnumTypeExtension',
'InputObjectTypeExtension',
'InterfaceTypeExtension',
'ObjectTypeExtension',
'ScalarTypeExtension',
'UnionTypeExtension',
]);
});
});
2 changes: 2 additions & 0 deletions src/language/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type { SourceLocation } from './location.js';

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

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

export { TokenKind } from './tokenKind.js';
Expand Down
74 changes: 4 additions & 70 deletions src/language/kinds.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,6 @@
/**
* The set of allowed kind values for AST nodes.
*/
export const Kind = {
/** Name */
NAME: 'Name' as const,
/* eslint-disable import/no-namespace */
import type * as Kind_ from './kinds_.js';

/** Document */
DOCUMENT: 'Document' as const,
OPERATION_DEFINITION: 'OperationDefinition' as const,
VARIABLE_DEFINITION: 'VariableDefinition' as const,
SELECTION_SET: 'SelectionSet' as const,
FIELD: 'Field' as const,
ARGUMENT: 'Argument' as const,
FRAGMENT_ARGUMENT: 'FragmentArgument' as const,
export * as Kind from './kinds_.js';

/** Fragments */
FRAGMENT_SPREAD: 'FragmentSpread' as const,
INLINE_FRAGMENT: 'InlineFragment' as const,
FRAGMENT_DEFINITION: 'FragmentDefinition' as const,

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

/** Directives */
DIRECTIVE: 'Directive' as const,

/** Types */
NAMED_TYPE: 'NamedType' as const,
LIST_TYPE: 'ListType' as const,
NON_NULL_TYPE: 'NonNullType' as const,

/** Type System Definitions */
SCHEMA_DEFINITION: 'SchemaDefinition' as const,
OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition' as const,

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

/** Directive Definitions */
DIRECTIVE_DEFINITION: 'DirectiveDefinition' as const,

/** Type System Extensions */
SCHEMA_EXTENSION: 'SchemaExtension' as const,

/** Type Extensions */
SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension' as const,
OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension' as const,
INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension' as const,
UNION_TYPE_EXTENSION: 'UnionTypeExtension' as const,
ENUM_TYPE_EXTENSION: 'EnumTypeExtension' as const,
INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension' as const,
};
// eslint-disable-next-line @typescript-eslint/no-redeclare
export type Kind = (typeof Kind)[keyof typeof Kind];
export type Kind = (typeof Kind_)[keyof typeof Kind_];
Loading

0 comments on commit 079167d

Please sign in to comment.