graphql
Version:
A Query Language and Runtime which can target any service.
1 lines • 22.1 kB
Source Map (JSON)
{"version":3,"file":"visitor.js","sourceRoot":"","sources":["../../src/language/visitor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,iCAAgC;AACpD,OAAO,EAAE,OAAO,EAAE,+BAA8B;AAGhD,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,kBAAiB;AACrD,OAAO,EAAE,IAAI,EAAE,oBAAmB;AA2FlC,MAAM,CAAC,MAAM,KAAK,GAAY,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAkKhD,MAAM,UAAU,KAAK,CACnB,IAAa,EACb,OAAqC,EACrC,cAAgC,iBAAiB;IAEjD,MAAM,aAAa,GAAG,IAAI,GAAG,EAAoC,CAAC;IAClE,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QACvC,aAAa,CAAC,GAAG,CAAC,IAAI,EAAE,oBAAoB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;IAC/D,CAAC;IAGD,IAAI,KAAK,GAAQ,SAAS,CAAC;IAC3B,IAAI,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,IAAI,IAAI,GAAQ,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC;IACf,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,IAAI,IAAI,GAAQ,IAAI,CAAC;IACrB,IAAI,GAAG,GAAQ,SAAS,CAAC;IACzB,IAAI,MAAM,GAAQ,SAAS,CAAC;IAC5B,MAAM,IAAI,GAAQ,EAAE,CAAC;IACrB,MAAM,SAAS,GAAG,EAAE,CAAC;IAGrB,GAAG,CAAC;QACF,KAAK,EAAE,CAAC;QACR,MAAM,SAAS,GAAG,KAAK,KAAK,IAAI,CAAC,MAAM,CAAC;QACxC,MAAM,QAAQ,GAAG,SAAS,IAA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AAC;AAiBD,MAAM,UAAU,oBAAoB,CAClC,OAAmB,EACnB,IAAU;IAEV,MAAM,WAAW,GAGA,OAAe,CAAC,IAAI,CAAC,CAAC;IAEvC,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE,CAAC;QAEpC,OAAO,WAAW,CAAC;IACrB,CAAC;SAAM,IAAI,OAAO,WAAW,KAAK,UAAU,EAAE,CAAC;QAE7C,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IAClD,CAAC;IAGD,OAAO,EAAE,KAAK,EAAG,OAAe,CAAC,KAAK,EAAE,KAAK,EAAG,OAAe,CAAC,KAAK,EAAE,CAAC;AAC1E,CAAC","sourcesContent":["/** @category Visiting */\n\nimport { devAssert } from '../jsutils/devAssert.ts';\nimport { inspect } from '../jsutils/inspect.ts';\n\nimport type { ASTNode } from './ast.ts';\nimport { isNode, QueryDocumentKeys } from './ast.ts';\nimport { Kind } from './kinds.ts';\n\n/** A visitor defines the callbacks called during AST traversal. */\nexport type ASTVisitor = EnterLeaveVisitor<ASTNode> | KindVisitor;\n\ntype KindVisitor = {\n readonly [NodeT in ASTNode as NodeT['kind']]?:\n | ASTVisitFn<NodeT>\n | EnterLeaveVisitor<NodeT>;\n};\n\ninterface EnterLeaveVisitor<TVisitedNode extends ASTNode> {\n readonly enter?: ASTVisitFn<TVisitedNode> | undefined;\n readonly leave?: ASTVisitFn<TVisitedNode> | undefined;\n}\n\n/**\n * A visitor is composed of visit functions called for each node during traversal.\n * @typeParam TVisitedNode - AST node type handled by this visitor function.\n */\nexport type ASTVisitFn<TVisitedNode extends ASTNode> = (\n /** Current node being visited. */\n node: TVisitedNode,\n /** Index or key for this node within the parent node or array. */\n key: string | number | undefined,\n /** Parent immediately above this node, which may be an array. */\n parent: ASTNode | ReadonlyArray<ASTNode> | undefined,\n /** Key path from the root node to this node. */\n path: ReadonlyArray<string | number>,\n /**\n * All nodes and arrays visited before reaching this node's parent.\n * These correspond to array indices in `path`.\n * Note: ancestors includes arrays that contain the visited node's parent.\n */\n ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>,\n) => any;\n\n/**\n * A reducer is composed of reducer functions that convert AST nodes into another form.\n *\n * @internal\n */\nexport type ASTReducer<R> = {\n readonly [NodeT in ASTNode as NodeT['kind']]?: {\n readonly enter?: ASTVisitFn<NodeT>;\n readonly leave: ASTReducerFn<NodeT, R>;\n };\n};\n\ntype ASTReducerFn<TReducedNode extends ASTNode, R> = (\n /**\n * Current node being visited.\n * @internal\n */\n node: { [K in keyof TReducedNode]: ReducedField<TReducedNode[K], R> },\n /**\n * Index or key for this node within the parent node or array.\n * @internal\n */\n key: string | number | undefined,\n /**\n * Parent immediately above this node, which may be an array.\n * @internal\n */\n parent: ASTNode | ReadonlyArray<ASTNode> | undefined,\n /**\n * Key path from the root node to this node.\n * @internal\n */\n path: ReadonlyArray<string | number>,\n /**\n * All nodes and arrays visited before reaching this node's parent.\n * These correspond to array indices in `path`.\n * Note: ancestors includes arrays that contain the visited node's parent.\n * @internal\n */\n ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>,\n) => R;\n\ntype ReducedField<T, R> = T extends ASTNode\n ? R\n : T extends ReadonlyArray<ASTNode>\n ? ReadonlyArray<R>\n : T;\n\n/** A visitor key map describes the traversable child properties for each node kind. */\nexport type ASTVisitorKeyMap = {\n [NodeT in ASTNode as NodeT['kind']]?: ReadonlyArray<keyof NodeT>;\n};\n\n/** A value that can be returned from a visitor function to stop traversal. */\nexport const BREAK: unknown = Object.freeze({});\n\n/**\n * visit() will walk through an AST using a depth-first traversal, calling\n * the visitor's enter function at each node in the traversal, and calling the\n * leave function after visiting that node and all of its child nodes.\n *\n * By returning different values from the enter and leave functions, the\n * behavior of the visitor can be altered, including skipping over a sub-tree of\n * the AST (by returning false), editing the AST by returning a value or null\n * to remove the value, or to stop the whole traversal by returning BREAK.\n *\n * When using visit() to edit an AST, the original AST will not be modified, and\n * a new version of the AST with the changes applied will be returned from the\n * visit function.\n * @param root - The AST node at which to start traversal.\n * @param visitor - The visitor or reducer functions to call while traversing.\n * @param visitorKeys - Optional map of child keys to visit for each AST node kind.\n * @returns The original AST, an edited AST, or a reduced value depending on the visitor.\n * @typeParam N - The root AST node type returned when visiting without reducing.\n * @example\n * ```ts\n * // Return values control traversal: undefined makes no change, false skips\n * // a subtree, BREAK stops traversal, null removes a node, and any other\n * // value replaces the current node.\n * import { Kind, parse, print, visit } from 'graphql/language';\n *\n * const document = parse('{ hero { name } }');\n * const editedAST = visit(document, {\n * Field: (node) => {\n * if (node.name.value === 'hero') {\n * return {\n * ...node,\n * name: { kind: Kind.NAME, value: 'human' },\n * };\n * }\n * },\n * });\n *\n * print(editedAST); // => '{\\n human {\\n name\\n }\\n}'\n * ```\n * @example\n * ```ts\n * // A named visitor function runs when entering nodes of that kind.\n * import { parse, visit } from 'graphql/language';\n *\n * const document = parse('{ hero { name } }');\n * const fieldNames = [];\n *\n * visit(document, {\n * Field: (node) => {\n * fieldNames.push(node.name.value);\n * },\n * });\n *\n * fieldNames; // => ['hero', 'name']\n * ```\n * @example\n * ```ts\n * // A named visitor object can provide separate enter and leave handlers for\n * // nodes of that kind.\n * import { parse, visit } from 'graphql/language';\n *\n * const document = parse('{ hero { name } }');\n * const events = [];\n *\n * visit(document, {\n * Field: {\n * enter: (node) => {\n * events.push(`enter:${node.name.value}`);\n * },\n * leave: (node) => {\n * events.push(`leave:${node.name.value}`);\n * },\n * },\n * });\n *\n * events; // => ['enter:hero', 'enter:name', 'leave:name', 'leave:hero']\n * ```\n * @example\n * ```ts\n * // Generic enter and leave handlers run for every node.\n * import { parse, visit } from 'graphql/language';\n *\n * const document = parse('{ hero { name } }');\n * let enterCount = 0;\n * let leaveCount = 0;\n *\n * visit(document, {\n * enter: (node) => {\n * enterCount += 1;\n * },\n * leave: (node) => {\n * leaveCount += 1;\n * },\n * });\n *\n * enterCount; // => leaveCount\n * enterCount > 0; // => true\n * ```\n */\nexport function visit<N extends ASTNode>(\n root: N,\n visitor: ASTVisitor,\n visitorKeys?: ASTVisitorKeyMap,\n): N;\n/**\n * Traverses an AST with reducer callbacks and returns the reduced value.\n * @param root - The AST node where traversal starts.\n * @param visitor - Reducer callbacks to invoke during traversal.\n * @param visitorKeys - Optional mapping of child keys for each AST node kind.\n * @returns The value produced by the reducer visitor.\n * @typeParam R - The value produced by reducer visitor callbacks.\n * @example\n * ```ts\n * // A reducer visitor returns values from leave handlers to build a reduced\n * // result instead of returning an edited AST.\n * import { parse, visit } from 'graphql/language';\n *\n * const document = parse('{ hero { name } }');\n * const printed = visit(document, {\n * Name: {\n * leave: (node) => {\n * return node.value;\n * },\n * },\n * Field: {\n * leave: (node) => {\n * return node.selectionSet == null\n * ? node.name\n * : `${node.name} { ${node.selectionSet} }`;\n * },\n * },\n * SelectionSet: {\n * leave: (node) => {\n * return node.selections.join(' ');\n * },\n * },\n * OperationDefinition: {\n * leave: (node) => {\n * return node.selectionSet;\n * },\n * },\n * Document: {\n * leave: (node) => {\n * return node.definitions.join('\\n');\n * },\n * },\n * });\n *\n * printed; // => 'hero { name }'\n * ```\n */\nexport function visit<R>(\n root: ASTNode,\n visitor: ASTReducer<R>,\n visitorKeys?: ASTVisitorKeyMap,\n): R;\n/**\n * Traverses an AST with visitor or reducer callbacks.\n * @internal\n */\nexport function visit(\n root: ASTNode,\n visitor: ASTVisitor | ASTReducer<any>,\n visitorKeys: ASTVisitorKeyMap = QueryDocumentKeys,\n): any {\n const enterLeaveMap = new Map<Kind, EnterLeaveVisitor<ASTNode>>();\n for (const kind of Object.values(Kind)) {\n enterLeaveMap.set(kind, getEnterLeaveForKind(visitor, kind));\n }\n\n /* eslint-disable no-undef-init */\n let stack: any = undefined;\n let inArray = Array.isArray(root);\n let keys: any = [root];\n let index = -1;\n let edits = [];\n let node: any = root;\n let key: any = undefined;\n let parent: any = undefined;\n const path: any = [];\n const ancestors = [];\n /* eslint-enable no-undef-init */\n\n do {\n index++;\n const isLeaving = index === keys.length;\n const isEdited = isLeaving && edits.length !== 0;\n if (isLeaving) {\n key = ancestors.length === 0 ? undefined : path[path.length - 1];\n node = parent;\n parent = ancestors.pop();\n if (isEdited) {\n if (inArray) {\n node = node.slice();\n\n let editOffset = 0;\n for (const [editKey, editValue] of edits) {\n const arrayKey = editKey - editOffset;\n if (editValue === null) {\n node.splice(arrayKey, 1);\n editOffset++;\n } else {\n node[arrayKey] = editValue;\n }\n }\n } else {\n node = { ...node };\n for (const [editKey, editValue] of edits) {\n node[editKey] = editValue;\n }\n }\n }\n index = stack.index;\n keys = stack.keys;\n edits = stack.edits;\n inArray = stack.inArray;\n stack = stack.prev;\n } else if (parent != null) {\n key = inArray ? index : keys[index];\n node = parent[key];\n if (node === null || node === undefined) {\n continue;\n }\n path.push(key);\n }\n\n let result;\n if (!Array.isArray(node)) {\n devAssert(isNode(node), `Invalid AST Node: ${inspect(node)}.`);\n\n const visitFn = isLeaving\n ? enterLeaveMap.get(node.kind)?.leave\n : enterLeaveMap.get(node.kind)?.enter;\n\n result = visitFn?.call(visitor, node, key, parent, path, ancestors);\n\n if (result === BREAK) {\n break;\n }\n\n if (result === false) {\n if (!isLeaving) {\n path.pop();\n continue;\n }\n } else if (result !== undefined) {\n edits.push([key, result]);\n if (!isLeaving) {\n if (isNode(result)) {\n node = result;\n } else {\n path.pop();\n continue;\n }\n }\n }\n }\n\n if (result === undefined && isEdited) {\n edits.push([key, node]);\n }\n\n if (isLeaving) {\n path.pop();\n } else {\n stack = { inArray, index, keys, edits, prev: stack };\n inArray = Array.isArray(node);\n keys = inArray ? node : ((visitorKeys as any)[node.kind] ?? []);\n index = -1;\n edits = [];\n if (parent != null) {\n ancestors.push(parent);\n }\n parent = node;\n }\n } while (stack !== undefined);\n\n if (edits.length !== 0) {\n // New root\n return edits.at(-1)[1];\n }\n\n return root;\n}\n\n/**\n * Creates a new visitor instance which delegates to many visitors to run in\n * parallel. Each visitor will be visited for each node before moving on.\n *\n * If a prior visitor edits a node, no following visitors will see that node.\n * @param visitors - The visitors to merge into one parallel visitor.\n * @returns A visitor that delegates traversal to each provided visitor.\n * @example\n * ```ts\n * import { parse, visit, visitInParallel } from 'graphql/language';\n *\n * const document = parse('{ hero { name } }');\n * const events = [];\n *\n * visit(\n * document,\n * visitInParallel([\n * {\n * Field: (node) => {\n * events.push(`field:${node.name.value}`);\n * },\n * },\n * {\n * Name: (node) => {\n * events.push(`name:${node.value}`);\n * },\n * },\n * ]),\n * );\n *\n * events; // => ['field:hero', 'name:hero', 'field:name', 'name:name']\n * ```\n */\nexport function visitInParallel(\n visitors: ReadonlyArray<ASTVisitor>,\n): ASTVisitor {\n const skipping = new Array(visitors.length).fill(null);\n const mergedVisitor = Object.create(null);\n\n for (const kind of Object.values(Kind)) {\n let hasVisitor = false;\n const enterList = new Array(visitors.length).fill(undefined);\n const leaveList = new Array(visitors.length).fill(undefined);\n\n for (let i = 0; i < visitors.length; ++i) {\n const { enter, leave } = getEnterLeaveForKind(visitors[i], kind);\n hasVisitor ||= enter != null || leave != null;\n enterList[i] = enter;\n leaveList[i] = leave;\n }\n\n if (!hasVisitor) {\n continue;\n }\n\n const mergedEnterLeave: EnterLeaveVisitor<ASTNode> = {\n enter(...args) {\n const node = args[0];\n for (let i = 0; i < visitors.length; i++) {\n if (skipping[i] === null) {\n const result = enterList[i]?.apply(visitors[i], args);\n if (result === false) {\n skipping[i] = node;\n } else if (result === BREAK) {\n skipping[i] = BREAK;\n } else if (result !== undefined) {\n return result;\n }\n }\n }\n },\n leave(...args) {\n const node = args[0];\n for (let i = 0; i < visitors.length; i++) {\n if (skipping[i] === null) {\n const result = leaveList[i]?.apply(visitors[i], args);\n if (result === BREAK) {\n skipping[i] = BREAK;\n } else if (result !== undefined && result !== false) {\n return result;\n }\n } else if (skipping[i] === node) {\n skipping[i] = null;\n }\n }\n },\n };\n\n mergedVisitor[kind] = mergedEnterLeave;\n }\n\n return mergedVisitor;\n}\n\n/**\n * Given a visitor instance and a node kind, return EnterLeaveVisitor for that kind.\n * @param visitor - The visitor object to inspect.\n * @param kind - The AST node kind to resolve handlers for.\n * @returns The enter and leave handlers that apply for the given node kind.\n * @example\n * ```ts\n * import { Kind, getEnterLeaveForKind } from 'graphql/language';\n *\n * const handlers = getEnterLeaveForKind({ Field: () => {} }, Kind.FIELD);\n *\n * typeof handlers.enter; // => 'function'\n * handlers.leave; // => undefined\n * ```\n */\nexport function getEnterLeaveForKind(\n visitor: ASTVisitor,\n kind: Kind,\n): EnterLeaveVisitor<ASTNode> {\n const kindVisitor:\n | ASTVisitFn<ASTNode>\n | EnterLeaveVisitor<ASTNode>\n | undefined = (visitor as any)[kind];\n\n if (typeof kindVisitor === 'object') {\n // { Kind: { enter() {}, leave() {} } }\n return kindVisitor;\n } else if (typeof kindVisitor === 'function') {\n // { Kind() {} }\n return { enter: kindVisitor, leave: undefined };\n }\n\n // { enter() {}, leave() {} }\n return { enter: (visitor as any).enter, leave: (visitor as any).leave };\n}\n"]}