import { t as SwcSpanConverter } from "./swc-span-C4QReqWr.mjs"; import { ArrowFunctionExpression, CallExpression, Node, TaggedTemplateExpression } from "@swc/types"; //#region packages/common/src/template-extraction/types.d.ts /** * Shared types for tagged template extraction and formatting. * @module */ /** Operation kind extracted from tagged template tag name. */ type OperationKind = "query" | "mutation" | "subscription" | "fragment"; /** A single template extracted from a TypeScript source file. */ type ExtractedTemplate = { /** Resolved schema name from gql.{schemaName}. */ readonly schemaName: string; /** Operation kind from tag name. */ readonly kind: OperationKind; /** * Raw content extracted from template. * For tagged templates: GraphQL body (e.g., "{ user { id } }"). * For callback-variables: variable definition string (e.g., "($id: ID!)"). * When source is "callback-variables", content is NOT standalone valid GraphQL — * consumers must wrap it in a dummy operation before parsing. */ readonly content: string; /** Element name from curried tag call (e.g., "GetUser" from query("GetUser")). */ readonly elementName?: string; /** Type name from curried fragment call (e.g., "User" from fragment("UserFields", "User")). */ readonly typeName?: string; /** Character offset range of typeName within TS source (excludes quotes). */ readonly typeNameSpan?: { readonly start: number; readonly end: number; }; /** Character offset range of content within TS source (excludes backticks/quotes). */ readonly contentRange?: { readonly start: number; readonly end: number; }; /** Character offset ranges of interpolation expressions within TS source (for __FRAG_SPREAD_N__ restoration). */ readonly expressionRanges?: readonly { readonly start: number; readonly end: number; }[]; /** * Extraction source discriminator. Undefined for tagged template (default). * "callback-variables" for variables property extracted from callback builder options object. */ readonly source?: "tagged-template" | "callback-variables"; }; /** ExtractedTemplate with guaranteed position information (when positionCtx is provided). */ type ExtractedTemplateWithPosition = ExtractedTemplate & { readonly contentRange: { readonly start: number; readonly end: number; }; }; /** A text edit to apply to source code for template formatting. */ type TemplateFormatEdit = { readonly start: number; readonly end: number; readonly newText: string; }; /** * A single f("fieldName") call in a callback builder field selection. * Schema-independent — extracted purely from SWC AST. */ type FieldCallNode = { readonly fieldName: string; readonly fieldNameSpan: { readonly start: number; readonly end: number; }; readonly callSpan: { readonly start: number; readonly end: number; }; readonly nested: FieldCallNested | null; }; type FieldCallNested = { readonly kind: "object"; readonly span: { readonly start: number; readonly end: number; }; readonly children: readonly FieldCallNode[]; } | { readonly kind: "union"; readonly span: { readonly start: number; readonly end: number; }; readonly branches: readonly UnionBranchNode[]; }; type UnionBranchNode = { readonly typeName: string; readonly typeNameSpan: { readonly start: number; readonly end: number; }; readonly branchSpan: { readonly start: number; readonly end: number; }; readonly children: readonly FieldCallNode[]; }; type ExtractedFieldTree = { readonly schemaName: string; readonly kind: OperationKind; readonly elementName?: string; readonly rootSpan: { readonly start: number; readonly end: number; }; readonly children: readonly FieldCallNode[]; }; //#endregion //#region packages/common/src/template-extraction/extract.d.ts declare const OPERATION_KINDS: Set; declare const isOperationKind: (value: string) => value is OperationKind; /** Optional position tracking context for extraction. */ type PositionTrackingContext = { readonly spanOffset: number; readonly converter: SwcSpanConverter; }; /** * Check if a call expression is a gql.{schemaName}(...) call. * Returns the schema name if it is, null otherwise. */ declare const getGqlCallSchemaName: (identifiers: ReadonlySet, call: CallExpression) => string | null; /** * Extract variables template from a callback builder options object. * Handles patterns like: * - `query("Name")({ variables: \`($id: ID!)\`, fields: ... })({})` * - `query("Name")({ variables: "($id: ID!)", fields: ... })` * * Returns true if a callback builder pattern was detected (even if no variables property found). */ declare const extractVariablesFromCallbackBuilder: (expr: Node, schemaName: string, templates: ExtractedTemplate[], positionCtx?: PositionTrackingContext) => boolean; /** * Extract templates from a gql callback's arrow function body. * Handles tagged templates, metadata chaining, and callback builder variables. */ declare const extractTemplatesFromCallback: (arrow: ArrowFunctionExpression, schemaName: string, positionCtx?: PositionTrackingContext) => ExtractedTemplate[]; /** * Extract a single template from a tagged template expression. * Supports both bare-tag (Identifier) and curried (CallExpression) tag forms. */ declare const extractFromTaggedTemplate: (tagged: TaggedTemplateExpression, schemaName: string, templates: ExtractedTemplate[], positionCtx?: PositionTrackingContext) => void; /** * Find the innermost gql call, unwrapping method chains like .attach(). */ declare const findGqlCall: (identifiers: ReadonlySet, node: Node) => CallExpression | null; /** * Walk AST to find gql calls and extract templates. */ declare function walkAndExtract(node: Node, identifiers: ReadonlySet, positionCtx: PositionTrackingContext): ExtractedTemplateWithPosition[]; declare function walkAndExtract(node: Node, identifiers: ReadonlySet, positionCtx?: PositionTrackingContext): ExtractedTemplate[]; /** * Walk AST to find gql callback builder calls and extract field call trees. * Companion to walkAndExtract — collects ExtractedFieldTree instead of ExtractedTemplate. */ declare const walkAndExtractFieldTrees: (node: Node, identifiers: ReadonlySet, positionCtx?: PositionTrackingContext) => ExtractedFieldTree[]; /** * Extract a field call tree from a callback builder expression. * * Expects a top-level expression of the callback builder pattern: * `query("Name")({ fields: ({ f }) => ({ ...f("id")(), ...f("name")() }) })({})` * * Returns an `ExtractedFieldTree` describing the root children (fields on the root selection set), * or `null` if the expression is not a callback builder or has no `fields` property. * * The `positionCtx` is optional — when provided, all span values will be character offsets * in the original TypeScript source. When omitted, spans are raw SWC byte positions. */ declare const extractFieldCallTree: (expr: Node, schemaName: string, positionCtx?: PositionTrackingContext) => ExtractedFieldTree | null; //#endregion //#region packages/common/src/template-extraction/format.d.ts /** A function that formats GraphQL source text. */ type FormatGraphqlFn = (source: string) => string; /** * Detect the base indentation for a template by looking at the line * containing the opening backtick. */ declare const detectBaseIndent: (tsSource: string, contentStartOffset: number) => string; /** * Re-indent formatted GraphQL to match the embedding context. * * **Inline templates** (content does NOT start with `\n`): * - Line 0 (`{`): no prefix — appears right after backtick * - Lines 1..N: graphql-js indentation only (converted to target unit) * * **Block templates** (content starts with `\n`): * - All lines: `baseIndent + indentUnit` prefix + converted graphql indent * - Trailing: `\n` + `baseIndent` */ declare const reindent: (formatted: string, baseIndent: string, originalContent: string, indentUnit?: string) => string; /** * Detect the indentation unit used in a TypeScript source file. * * Uses the smallest indentation width found as the indent unit. * In files with embedded templates (e.g., graphql in backticks), * template content lines are at `baseIndent + graphqlIndent`, * so their absolute indent is always >= the file's indent unit. */ declare const detectIndentUnit: (tsSource: string) => string; /** * Reconstruct a full GraphQL document from template content + metadata. * * For curried syntax, the template `content` is only the body (e.g., `{ user { id } }`). * GraphQL parsers need a full document (e.g., `query GetUser { user { id } }`). * * For bare-tag syntax, the content already starts with a keyword and is a full document. * * @returns The wrapped source and a regex pattern to strip the synthetic prefix after formatting */ declare const buildGraphqlWrapper: (template: ExtractedTemplate) => { wrapped: string; prefixPattern: RegExp | null; }; /** * Strip the reconstructed prefix from formatted output to get back the template body. * Uses regex pattern matching to handle whitespace normalization by the formatter * (e.g., `query Foo ($id: ID!)` → `query Foo($id: ID!)`). */ declare const unwrapFormattedContent: (formatted: string, prefixPattern: RegExp | null) => string; /** * Format GraphQL templates within TypeScript source. * * Applies the given format function to each template's content, handles * wrapper reconstruction for curried syntax, and re-indents the result * to match the TypeScript embedding context. * * @returns Array of edits to apply to the source (sorted by position, not yet applied) */ declare const formatTemplatesInSource: (templates: readonly ExtractedTemplate[], tsSource: string, formatGraphql: FormatGraphqlFn) => readonly TemplateFormatEdit[]; //#endregion export { ExtractedFieldTree, ExtractedTemplate, ExtractedTemplateWithPosition, FieldCallNested, FieldCallNode, FormatGraphqlFn, OPERATION_KINDS, OperationKind, PositionTrackingContext, TemplateFormatEdit, UnionBranchNode, buildGraphqlWrapper, detectBaseIndent, detectIndentUnit, extractFieldCallTree, extractFromTaggedTemplate, extractTemplatesFromCallback, extractVariablesFromCallbackBuilder, findGqlCall, formatTemplatesInSource, getGqlCallSchemaName, isOperationKind, reindent, unwrapFormattedContent, walkAndExtract, walkAndExtractFieldTrees }; //# sourceMappingURL=template-extraction.d.mts.map