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