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