import type { Node } from 'web-tree-sitter'; import type { ListTypeNode } from './list_type.js'; import type { NamedTypeNode } from './named_type.js'; import type { NonNullTypeNode } from './non_null_type.js'; const TYPE = 'type' as const; // ================================================================ // ================================================================ // // TypeNode // // GraphQL type node // // ================================================================ // ================================================================ /** * Represents a type in the GraphQL AST. * * Children: * {@link ListTypeNode}, {@link NamedTypeNode}, {@link NonNullTypeNode} * */ export interface TypeNode extends Node { type: typeof TYPE; } // ================================================================ // ================================================================ // // Type Guard // // ================================================================ // ================================================================ /** * Type guard to check if a node is a {@link TypeNode}. * * @param node - The node to check * @returns True if the node is a {@link TypeNode} * * @example * ```typescript * if (isTypeNode(node)) { * // TypeScript now knows node is TypeNode * console.log(node.type); // 'type' * } * ``` */ export function isTypeNode(node: unknown): node is TypeNode { return (node as any)?.type === TYPE; } // ================================================================ // ================================================================ // // Constructor // // ================================================================ // ================================================================ /** * Creates a new {@link TypeNode} with the specified properties. * * @param props - The node properties * @param props.listtype - {@link ListTypeNode} * @param props.namedtype - {@link NamedTypeNode} * @param props.nonnulltype - {@link NonNullTypeNode} * @returns A new {@link TypeNode} * * @example * ```typescript * const node = TypeNode({ * // properties... * }); * ``` */ export function TypeNode(props: Omit): TypeNode { return { type: TYPE, ...props }; }