@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
43 lines (40 loc) • 3.44 kB
text/typescript
import { AST } from 'eslint';
import { SourceLocation, Comment } from 'estree';
import { ASTNode, TypeNode, TypeInfo, DocumentNode, DefinitionNode, EnumValueDefinitionNode, EnumTypeDefinitionNode, EnumTypeExtensionNode, InputValueDefinitionNode, DirectiveDefinitionNode, FieldDefinitionNode, InputObjectTypeDefinitionNode, InputObjectTypeExtensionNode, InterfaceTypeDefinitionNode, InterfaceTypeExtensionNode, ObjectTypeDefinitionNode, ObjectTypeExtensionNode, SelectionSetNode, ExecutableDefinitionNode, FieldNode, InlineFragmentNode, SelectionNode, ListTypeNode, NonNullTypeNode, OperationTypeDefinitionNode, VariableDefinitionNode, NameNode, ArgumentNode, FragmentSpreadNode, NamedTypeNode, TypeDefinitionNode, TypeExtensionNode, VariableNode, DirectiveNode } from 'graphql';
type SafeGraphQLType<T extends ASTNode> = T extends {
type: TypeNode;
} ? Omit<T, 'loc' | 'type'> & {
gqlType: T['type'];
} : Omit<T, 'loc'>;
type Writeable<T> = {
-readonly [K in keyof T]: T[K];
};
type TypeInformation = {
argument: ReturnType<TypeInfo['getArgument']>;
defaultValue: ReturnType<TypeInfo['getDefaultValue']>;
directive: ReturnType<TypeInfo['getDirective']>;
enumValue: ReturnType<TypeInfo['getEnumValue']>;
fieldDef: ReturnType<TypeInfo['getFieldDef']>;
inputType: ReturnType<TypeInfo['getInputType']>;
parentInputType: ReturnType<TypeInfo['getParentInputType']>;
parentType: ReturnType<TypeInfo['getParentType']>;
gqlType: ReturnType<TypeInfo['getType']>;
};
type NodeWithName = ArgumentNode | DirectiveDefinitionNode | EnumValueDefinitionNode | ExecutableDefinitionNode | FieldDefinitionNode | FieldNode | FragmentSpreadNode | NamedTypeNode | TypeDefinitionNode | TypeExtensionNode | VariableNode;
type NodeWithType = FieldDefinitionNode | InputValueDefinitionNode | ListTypeNode | NonNullTypeNode | OperationTypeDefinitionNode | VariableDefinitionNode;
type ParentNode<T> = T extends DocumentNode ? AST.Program : T extends DefinitionNode ? DocumentNode : T extends EnumValueDefinitionNode ? EnumTypeDefinitionNode | EnumTypeExtensionNode : T extends InputValueDefinitionNode ? DirectiveDefinitionNode | FieldDefinitionNode | InputObjectTypeDefinitionNode | InputObjectTypeExtensionNode : T extends FieldDefinitionNode ? InterfaceTypeDefinitionNode | InterfaceTypeExtensionNode | ObjectTypeDefinitionNode | ObjectTypeExtensionNode : T extends SelectionSetNode ? ExecutableDefinitionNode | FieldNode | InlineFragmentNode : T extends SelectionNode ? SelectionSetNode : T extends TypeNode ? NodeWithType : T extends NameNode ? NodeWithName : T extends DirectiveNode ? InputObjectTypeDefinitionNode | ObjectTypeDefinitionNode : T extends VariableNode ? VariableDefinitionNode : unknown;
type Node<T extends ASTNode, WithTypeInfo extends boolean> = Writeable<SafeGraphQLType<T>> & {
type: T['kind'];
loc: SourceLocation;
range: AST.Range;
leadingComments: Comment[];
typeInfo: () => WithTypeInfo extends true ? TypeInformation : Record<string, never>;
rawNode: () => T;
parent: GraphQLESTreeNode<ParentNode<T>>;
};
type GraphQLESTreeNode<T, W extends boolean = false> = T extends ASTNode ? {
[K in keyof Node<T, W>]: Node<T, W>[K] extends ReadonlyArray<infer ArrayItem> | undefined ? GraphQLESTreeNode<ArrayItem, W>[] : GraphQLESTreeNode<Node<T, W>[K], W>;
} : T extends AST.Program ? T & {
parent: null;
} : T;
export type { GraphQLESTreeNode, TypeInformation };