Skip to content

Commit

Permalink
Allow for tree-shaking the Kind enum
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 30, 2024
1 parent 0ffc1e1 commit f45208a
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 149 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export type {
} from './type/index.js';

// Parse and operate on GraphQL language source files.
// eslint-disable-next-line @typescript-eslint/consistent-type-exports
export {
Token,
Source,
Expand Down
165 changes: 86 additions & 79 deletions src/language/__tests__/predicates-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,55 @@ 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',
'EnumTypeDefinition',
'InputObjectTypeDefinition',
'DirectiveDefinition',
'SchemaExtension',
'ScalarTypeExtension',
'ObjectTypeExtension',
'InterfaceTypeExtension',
'UnionTypeExtension',
'EnumTypeExtension',
'InputObjectTypeExtension',
]);
expect(filterNodes(isDefinitionNode)).to.deep.equal(
[
'OperationDefinition',
'FragmentDefinition',
'SchemaDefinition',
'ScalarTypeDefinition',
'ObjectTypeDefinition',
'InterfaceTypeDefinition',
'UnionTypeDefinition',
'EnumTypeDefinition',
'InputObjectTypeDefinition',
'DirectiveDefinition',
'SchemaExtension',
'ScalarTypeExtension',
'ObjectTypeExtension',
'InterfaceTypeExtension',
'UnionTypeExtension',
'EnumTypeExtension',
'InputObjectTypeExtension',
].sort(),
);
});

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

it('isSelectionNode', () => {
expect(filterNodes(isSelectionNode)).to.deep.equal([
'Field',
'FragmentSpread',
'InlineFragment',
]);
expect(filterNodes(isSelectionNode)).to.deep.equal(
['Field', 'FragmentSpread', 'InlineFragment'].sort(),
);
});

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

it('isConstValueNode', () => {
Expand All @@ -88,57 +89,63 @@ describe('AST node predicates', () => {
});

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

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

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

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

it('isTypeExtensionNode', () => {
expect(filterNodes(isTypeExtensionNode)).to.deep.equal([
'ScalarTypeExtension',
'ObjectTypeExtension',
'InterfaceTypeExtension',
'UnionTypeExtension',
'EnumTypeExtension',
'InputObjectTypeExtension',
]);
expect(filterNodes(isTypeExtensionNode)).to.deep.equal(
[
'ScalarTypeExtension',
'ObjectTypeExtension',
'InterfaceTypeExtension',
'UnionTypeExtension',
'EnumTypeExtension',
'InputObjectTypeExtension',
].sort(),
);
});
});
65 changes: 65 additions & 0 deletions src/language/_kinds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/** Name */
export const NAME = 'Name';

/** Document */
export const DOCUMENT = 'Document';
export const OPERATION_DEFINITION = 'OperationDefinition';
export const VARIABLE_DEFINITION = 'VariableDefinition';
export const SELECTION_SET = 'SelectionSet';
export const FIELD = 'Field';
export const ARGUMENT = 'Argument';
export const FRAGMENT_ARGUMENT = 'FragmentArgument';

/** Fragments */
export const FRAGMENT_SPREAD = 'FragmentSpread';
export const INLINE_FRAGMENT = 'InlineFragment';
export const FRAGMENT_DEFINITION = 'FragmentDefinition';

/** Values */
export const VARIABLE = 'Variable';
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';

/** Directive Definitions */
export const DIRECTIVE_DEFINITION = 'DirectiveDefinition';

/** Type System Extensions */
export const SCHEMA_EXTENSION = 'SchemaExtension';

/** 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';
2 changes: 1 addition & 1 deletion src/language/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type { SourceLocation } from './location.js';

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

export { Kind } from './kinds.js';
export * from './kinds.js';

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

Expand Down
Loading

0 comments on commit f45208a

Please sign in to comment.