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