@sanity/codegen 8.2.0 → 8.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_exports/index.d.ts +71 -0
- package/dist/_exports/index.js +1 -0
- package/dist/_exports/index.js.map +1 -1
- package/dist/safeParseQuery.js +1 -1
- package/dist/safeParseQuery.js.map +1 -1
- package/dist/typescript/expressionResolvers.js +18 -0
- package/dist/typescript/expressionResolvers.js.map +1 -1
- package/dist/typescript/schemaTypeGenerator.js +77 -63
- package/dist/typescript/schemaTypeGenerator.js.map +1 -1
- package/package.json +1 -1
package/dist/_exports/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import {DefinedTelemetryTrace} from '@sanity/telemetry'
|
|
2
|
+
import {DocumentSchemaType} from 'groq-js'
|
|
2
3
|
import {ExprNode} from 'groq-js'
|
|
3
4
|
import {FSWatcher} from 'chokidar'
|
|
4
5
|
import {SchemaType} from 'groq-js'
|
|
5
6
|
import * as t from '@babel/types'
|
|
6
7
|
import {TransformOptions} from '@babel/core'
|
|
8
|
+
import {TypeDeclarationSchemaType} from 'groq-js'
|
|
9
|
+
import {TypeNode} from 'groq-js'
|
|
7
10
|
import * as v from 'valibot'
|
|
8
11
|
import {WorkerChannel} from '@sanity/worker-channels'
|
|
9
12
|
import {WorkerChannelReporter} from '@sanity/worker-channels'
|
|
@@ -500,6 +503,74 @@ export declare function runTypegenWatcher(options: RunTypegenOptions): {
|
|
|
500
503
|
*/
|
|
501
504
|
export declare function safeParseQuery(query: string): ExprNode
|
|
502
505
|
|
|
506
|
+
/**
|
|
507
|
+
* Generates TypeScript types from a schema, as `sanity schema extract` writes it.
|
|
508
|
+
*
|
|
509
|
+
* On construction, every type in the schema gets a TypeScript type and a unique alias
|
|
510
|
+
* identifier; iterating the instance yields them as `{name, id, tsType}`, in schema order.
|
|
511
|
+
* `generateTsType` turns any groq-js type node into a TypeScript type that names schema types
|
|
512
|
+
* by those aliases, and `evaluateQuery` does so for the result of a GROQ query. A tool that
|
|
513
|
+
* types GROQ outside `TypeGenerator`, such as a TypeScript content mapper for `.groq` files,
|
|
514
|
+
* uses this class so that its output agrees with what `sanity typegen generate` writes.
|
|
515
|
+
* @beta
|
|
516
|
+
*/
|
|
517
|
+
export declare class SchemaTypeGenerator {
|
|
518
|
+
readonly schema: SchemaType
|
|
519
|
+
evaluateQuery: (arg: Pick<ExtractedQuery, 'query'>) => {
|
|
520
|
+
stats: TypeEvaluationStats
|
|
521
|
+
tsType: t.TSType
|
|
522
|
+
}
|
|
523
|
+
private arrayOfUsed
|
|
524
|
+
private identifiers
|
|
525
|
+
private tsTypes
|
|
526
|
+
constructor(schema: SchemaType)
|
|
527
|
+
/** The names of the schema's document types, in schema order. */
|
|
528
|
+
documentTypeNames(): string[]
|
|
529
|
+
/**
|
|
530
|
+
* Evaluates a projection for one document type, as `*[_type == TYPE][0]PROJECTION` without
|
|
531
|
+
* the `null` for a missing document: the SDK projects documents it has found.
|
|
532
|
+
*/
|
|
533
|
+
evaluateProjection({documentType, projection}: {documentType: string; projection: string}): {
|
|
534
|
+
stats: TypeEvaluationStats
|
|
535
|
+
tsType: t.TSType
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* The TypeScript type for a groq-js type node. A reference to a schema type becomes its alias
|
|
539
|
+
* identifier, an array of inline types becomes `ArrayOf<...>` (and `isArrayOfUsed` reports
|
|
540
|
+
* that the helper is needed), and a reference object carries `internalGroqTypeReferenceTo`.
|
|
541
|
+
*/
|
|
542
|
+
generateTsType(typeNode: DocumentSchemaType | TypeDeclarationSchemaType | TypeNode): t.TSType
|
|
543
|
+
getType(typeName: string):
|
|
544
|
+
| {
|
|
545
|
+
id: t.Identifier
|
|
546
|
+
tsType: t.TSType
|
|
547
|
+
}
|
|
548
|
+
| undefined
|
|
549
|
+
hasType(typeName: string): boolean
|
|
550
|
+
isArrayOfUsed(): boolean
|
|
551
|
+
[Symbol.iterator](): Generator<
|
|
552
|
+
{
|
|
553
|
+
id: t.Identifier
|
|
554
|
+
tsType: t.TSType
|
|
555
|
+
name: string
|
|
556
|
+
},
|
|
557
|
+
void,
|
|
558
|
+
unknown
|
|
559
|
+
>
|
|
560
|
+
typeNames(): string[]
|
|
561
|
+
/**
|
|
562
|
+
* Helper function used to generate TS types for arrays of inline types, or arrays of inline types
|
|
563
|
+
* wrapped in the ArrayOf wrapper that adds _key prop
|
|
564
|
+
*/
|
|
565
|
+
private generateArrayOfTsType
|
|
566
|
+
private generateArrayTsType
|
|
567
|
+
private generateDocumentTsType
|
|
568
|
+
private generateInlineTsType
|
|
569
|
+
private generateObjectTsType
|
|
570
|
+
private generateTsObjectProperty
|
|
571
|
+
private generateUnionTsType
|
|
572
|
+
}
|
|
573
|
+
|
|
503
574
|
/** Builds a tuple from elements, stopping at the first `never`. */
|
|
504
575
|
declare type TakeUntilNever<T extends unknown[]> = T extends [infer H, ...infer Rest]
|
|
505
576
|
? [H] extends [never]
|
package/dist/_exports/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { findQueriesInPath } from '../typescript/findQueriesInPath.js';
|
|
|
8
8
|
export { findQueriesInSource } from '../typescript/findQueriesInSource.js';
|
|
9
9
|
export { getResolver } from '../typescript/moduleResolver.js';
|
|
10
10
|
export { registerBabel } from '../typescript/registerBabel.js';
|
|
11
|
+
export { SchemaTypeGenerator } from '../typescript/schemaTypeGenerator.js';
|
|
11
12
|
export { TypeGenerator } from '../typescript/typeGenerator.js';
|
|
12
13
|
export { QueryExtractionError } from '../typescript/types.js';
|
|
13
14
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/_exports/index.ts"],"sourcesContent":["export {runTypegenGenerate} from '../actions/typegenGenerate.js'\nexport {runTypegenWatcher} from '../actions/typegenWatch.js'\nexport {\n type GenerationResult,\n type RunTypegenOptions,\n type TypegenProgressEvent,\n} from '../actions/types.js'\nexport {\n type CodegenConfig,\n configDefinition,\n readConfig,\n type TypeGenConfig,\n} from '../readConfig.js'\nexport {readSchema} from '../readSchema.js'\nexport {safeParseQuery} from '../safeParseQuery.js'\nexport {TypegenWatchModeTrace, TypesGeneratedTrace} from '../typegen.telemetry.js'\nexport {findQueriesInPath} from '../typescript/findQueriesInPath.js'\nexport {findQueriesInSource} from '../typescript/findQueriesInSource.js'\nexport {getResolver} from '../typescript/moduleResolver.js'\nexport {registerBabel} from '../typescript/registerBabel.js'\nexport {\n type GenerateTypesOptions,\n TypeGenerator,\n type TypegenResource,\n type TypegenWorkerChannel,\n} from '../typescript/typeGenerator.js'\nexport {\n type EvaluatedModule,\n type EvaluatedProjection,\n type EvaluatedQuery,\n type ExtractedModule,\n type ExtractedProjection,\n type ExtractedQuery,\n QueryExtractionError,\n} from '../typescript/types.js'\nexport {type FilterByType, type Get} from '../typeUtils.js'\n"],"names":["runTypegenGenerate","runTypegenWatcher","configDefinition","readConfig","readSchema","safeParseQuery","TypegenWatchModeTrace","TypesGeneratedTrace","findQueriesInPath","findQueriesInSource","getResolver","registerBabel","TypeGenerator","QueryExtractionError"],"mappings":"AAAA,SAAQA,kBAAkB,QAAO,gCAA+B;AAChE,SAAQC,iBAAiB,QAAO,6BAA4B;AAM5D,SAEEC,gBAAgB,EAChBC,UAAU,QAEL,mBAAkB;AACzB,SAAQC,UAAU,QAAO,mBAAkB;AAC3C,SAAQC,cAAc,QAAO,uBAAsB;AACnD,SAAQC,qBAAqB,EAAEC,mBAAmB,QAAO,0BAAyB;AAClF,SAAQC,iBAAiB,QAAO,qCAAoC;AACpE,SAAQC,mBAAmB,QAAO,uCAAsC;AACxE,SAAQC,WAAW,QAAO,kCAAiC;AAC3D,SAAQC,aAAa,QAAO,iCAAgC;AAC5D,SAEEC,aAAa,QAGR,iCAAgC;AACvC,SAOEC,oBAAoB,QACf,yBAAwB"}
|
|
1
|
+
{"version":3,"sources":["../../src/_exports/index.ts"],"sourcesContent":["export {runTypegenGenerate} from '../actions/typegenGenerate.js'\nexport {runTypegenWatcher} from '../actions/typegenWatch.js'\nexport {\n type GenerationResult,\n type RunTypegenOptions,\n type TypegenProgressEvent,\n} from '../actions/types.js'\nexport {\n type CodegenConfig,\n configDefinition,\n readConfig,\n type TypeGenConfig,\n} from '../readConfig.js'\nexport {readSchema} from '../readSchema.js'\nexport {safeParseQuery} from '../safeParseQuery.js'\nexport {TypegenWatchModeTrace, TypesGeneratedTrace} from '../typegen.telemetry.js'\nexport {findQueriesInPath} from '../typescript/findQueriesInPath.js'\nexport {findQueriesInSource} from '../typescript/findQueriesInSource.js'\nexport {getResolver} from '../typescript/moduleResolver.js'\nexport {registerBabel} from '../typescript/registerBabel.js'\nexport {SchemaTypeGenerator} from '../typescript/schemaTypeGenerator.js'\nexport {\n type GenerateTypesOptions,\n TypeGenerator,\n type TypegenResource,\n type TypegenWorkerChannel,\n} from '../typescript/typeGenerator.js'\nexport {\n type EvaluatedModule,\n type EvaluatedProjection,\n type EvaluatedQuery,\n type ExtractedModule,\n type ExtractedProjection,\n type ExtractedQuery,\n QueryExtractionError,\n} from '../typescript/types.js'\nexport {type FilterByType, type Get} from '../typeUtils.js'\n"],"names":["runTypegenGenerate","runTypegenWatcher","configDefinition","readConfig","readSchema","safeParseQuery","TypegenWatchModeTrace","TypesGeneratedTrace","findQueriesInPath","findQueriesInSource","getResolver","registerBabel","SchemaTypeGenerator","TypeGenerator","QueryExtractionError"],"mappings":"AAAA,SAAQA,kBAAkB,QAAO,gCAA+B;AAChE,SAAQC,iBAAiB,QAAO,6BAA4B;AAM5D,SAEEC,gBAAgB,EAChBC,UAAU,QAEL,mBAAkB;AACzB,SAAQC,UAAU,QAAO,mBAAkB;AAC3C,SAAQC,cAAc,QAAO,uBAAsB;AACnD,SAAQC,qBAAqB,EAAEC,mBAAmB,QAAO,0BAAyB;AAClF,SAAQC,iBAAiB,QAAO,qCAAoC;AACpE,SAAQC,mBAAmB,QAAO,uCAAsC;AACxE,SAAQC,WAAW,QAAO,kCAAiC;AAC3D,SAAQC,aAAa,QAAO,iCAAgC;AAC5D,SAAQC,mBAAmB,QAAO,uCAAsC;AACxE,SAEEC,aAAa,QAGR,iCAAgC;AACvC,SAOEC,oBAAoB,QACf,yBAAwB"}
|
package/dist/safeParseQuery.js
CHANGED
|
@@ -17,7 +17,7 @@ import { parse } from 'groq-js';
|
|
|
17
17
|
* the names of the start and end variables.
|
|
18
18
|
* @internal
|
|
19
19
|
*/ export function* extractSliceParams(query) {
|
|
20
|
-
const sliceRegex = /\[(\$(\w+)|\d)
|
|
20
|
+
const sliceRegex = /\[\s*(\$(\w+)|\d+)\s*\.\.\.?\s*(\$(\w+)|\d+)\s*\]/g;
|
|
21
21
|
const matches = query.matchAll(sliceRegex);
|
|
22
22
|
if (!matches) {
|
|
23
23
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/safeParseQuery.ts"],"sourcesContent":["import {type ExprNode, parse} from 'groq-js'\n\n/**\n * safeParseQuery parses a GROQ query string, but first attempts to extract any parameters used in slices. This method is _only_\n * intended for use in type generation where we don't actually execute the parsed AST on a dataset, and should not be used elsewhere.\n * @internal\n */\nexport function safeParseQuery(query: string): ExprNode {\n const params: Record<string, unknown> = {}\n\n for (const param of extractSliceParams(query)) {\n params[param] = 0 // we don't care about the value, just the type\n }\n return parse(query, {params})\n}\n\n/**\n * Finds occurences of `[($start|{number})..($end|{number})]` in a query string and returns the start and end values, and return\n * the names of the start and end variables.\n * @internal\n */\nexport function* extractSliceParams(query: string): Generator<string> {\n const sliceRegex = /\\[(\\$(\\w+)|\\d)
|
|
1
|
+
{"version":3,"sources":["../src/safeParseQuery.ts"],"sourcesContent":["import {type ExprNode, parse} from 'groq-js'\n\n/**\n * safeParseQuery parses a GROQ query string, but first attempts to extract any parameters used in slices. This method is _only_\n * intended for use in type generation where we don't actually execute the parsed AST on a dataset, and should not be used elsewhere.\n * @internal\n */\nexport function safeParseQuery(query: string): ExprNode {\n const params: Record<string, unknown> = {}\n\n for (const param of extractSliceParams(query)) {\n params[param] = 0 // we don't care about the value, just the type\n }\n return parse(query, {params})\n}\n\n/**\n * Finds occurences of `[($start|{number})..($end|{number})]` in a query string and returns the start and end values, and return\n * the names of the start and end variables.\n * @internal\n */\nexport function* extractSliceParams(query: string): Generator<string> {\n const sliceRegex = /\\[\\s*(\\$(\\w+)|\\d+)\\s*\\.\\.\\.?\\s*(\\$(\\w+)|\\d+)\\s*\\]/g\n const matches = query.matchAll(sliceRegex)\n if (!matches) {\n return\n }\n for (const match of matches) {\n const start = match[1] === `$${match[2]}` ? match[2] : null\n if (start !== null) {\n yield start!\n }\n const end = match[3] === `$${match[4]}` ? match[4] : null\n if (end !== null) {\n yield end!\n }\n }\n}\n"],"names":["parse","safeParseQuery","query","params","param","extractSliceParams","sliceRegex","matches","matchAll","match","start","end"],"mappings":"AAAA,SAAuBA,KAAK,QAAO,UAAS;AAE5C;;;;CAIC,GACD,OAAO,SAASC,eAAeC,KAAa;IAC1C,MAAMC,SAAkC,CAAC;IAEzC,KAAK,MAAMC,SAASC,mBAAmBH,OAAQ;QAC7CC,MAAM,CAACC,MAAM,GAAG,GAAE,+CAA+C;IACnE;IACA,OAAOJ,MAAME,OAAO;QAACC;IAAM;AAC7B;AAEA;;;;CAIC,GACD,OAAO,UAAUE,mBAAmBH,KAAa;IAC/C,MAAMI,aAAa;IACnB,MAAMC,UAAUL,MAAMM,QAAQ,CAACF;IAC/B,IAAI,CAACC,SAAS;QACZ;IACF;IACA,KAAK,MAAME,SAASF,QAAS;QAC3B,MAAMG,QAAQD,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,EAAEA,KAAK,CAAC,EAAE,EAAE,GAAGA,KAAK,CAAC,EAAE,GAAG;QACvD,IAAIC,UAAU,MAAM;YAClB,MAAMA;QACR;QACA,MAAMC,MAAMF,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,EAAEA,KAAK,CAAC,EAAE,EAAE,GAAGA,KAAK,CAAC,EAAE,GAAG;QACrD,IAAIE,QAAQ,MAAM;YAChB,MAAMA;QACR;IACF;AACF"}
|
|
@@ -196,6 +196,24 @@ function resolveIdentifier({ babelConfig, file, filename, fnArguments, node, par
|
|
|
196
196
|
if (!argument && paramIndex !== -1 && babelTypes.isAssignmentPattern(params[paramIndex])) {
|
|
197
197
|
argument = params[paramIndex].right;
|
|
198
198
|
}
|
|
199
|
+
// `isLiteral` is true for a template literal.
|
|
200
|
+
if (argument && babelTypes.isTemplateLiteral(argument)) {
|
|
201
|
+
const forwardsThisParameter = argument.expressions.some((expression)=>babelTypes.isIdentifier(expression) && expression.name === node.name);
|
|
202
|
+
if (forwardsThisParameter) {
|
|
203
|
+
throw new Error(`Could not forward parameter "${node.name}" through a template literal in ${filename}:${argument.loc?.start.line}:${argument.loc?.start.column}`);
|
|
204
|
+
}
|
|
205
|
+
const unbindParameter = (items)=>items.filter((_, index)=>index !== paramIndex);
|
|
206
|
+
return resolveExpression({
|
|
207
|
+
babelConfig,
|
|
208
|
+
file,
|
|
209
|
+
filename,
|
|
210
|
+
fnArguments: unbindParameter(fnArguments),
|
|
211
|
+
node: argument,
|
|
212
|
+
params: unbindParameter(params),
|
|
213
|
+
resolver,
|
|
214
|
+
scope
|
|
215
|
+
});
|
|
216
|
+
}
|
|
199
217
|
if (argument && babelTypes.isLiteral(argument)) {
|
|
200
218
|
return resolveExpression({
|
|
201
219
|
babelConfig,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/typescript/expressionResolvers.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\n\nimport {type TransformOptions, traverse} from '@babel/core'\nimport {Scope} from '@babel/traverse'\nimport * as babelTypes from '@babel/types'\nimport {createDebug} from 'obug'\n\nimport {formatPath} from '../utils/formatPath.js'\nimport {parseSourceFile} from './parseSource.js'\n\nconst debug = createDebug('sanity:codegen:findQueries:debug')\n\ntype resolveExpressionReturnType = string\n\nconst TAGGED_TEMPLATE_ALLOW_LIST = new Set(['groq'])\nconst FUNCTION_WRAPPER_ALLOW_LIST = new Set(['defineQuery'])\n\n/**\n * resolveExpression takes a node and returns the resolved value of the expression.\n * @beta\n * @internal\n */\nexport function resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments = [],\n node,\n params = [],\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments?: babelTypes.Node[]\n node: babelTypes.Node\n params?: babelTypes.Node[]\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): resolveExpressionReturnType {\n debug(\n `Resolving node ${node.type} in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n if (\n babelTypes.isTaggedTemplateExpression(node) &&\n babelTypes.isIdentifier(node.tag) &&\n TAGGED_TEMPLATE_ALLOW_LIST.has(node.tag.name)\n ) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.quasi,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isTemplateLiteral(node)) {\n const resolvedExpressions = node.expressions.map((expression) =>\n resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: expression,\n params,\n resolver,\n scope,\n }),\n )\n return node.quasis\n .map((quasi, idx) => {\n return (quasi.value.cooked || '') + (resolvedExpressions[idx] || '')\n })\n .join('')\n }\n\n if (babelTypes.isLiteral(node)) {\n if (node.type === 'NullLiteral' || node.type === 'RegExpLiteral') {\n throw new Error(`Unsupported literal type: ${node.type}`)\n }\n\n return node.value.toString()\n }\n\n if (babelTypes.isIdentifier(node)) {\n return resolveIdentifier({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isVariableDeclarator(node)) {\n const init = node.init ?? (babelTypes.isAssignmentPattern(node.id) && node.id.right)\n if (!init) {\n throw new Error(`Unsupported variable declarator`)\n }\n\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: init,\n resolver,\n scope,\n })\n }\n\n if (\n babelTypes.isCallExpression(node) &&\n babelTypes.isIdentifier(node.callee) &&\n FUNCTION_WRAPPER_ALLOW_LIST.has(node.callee.name)\n ) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n node: node.arguments[0]!,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isCallExpression(node)) {\n return resolveCallExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n params,\n resolver,\n scope,\n })\n }\n\n if (\n babelTypes.isArrowFunctionExpression(node) ||\n babelTypes.isFunctionDeclaration(node) ||\n babelTypes.isFunctionExpression(node)\n ) {\n const newScope = new Scope(scope.path, scope)\n\n for (const [i, param] of params.entries()) {\n newScope.push({\n id: param as babelTypes.LVal,\n init: fnArguments[i] as babelTypes.Expression | undefined,\n })\n }\n\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.body,\n params: node.params,\n resolver,\n scope: newScope,\n })\n }\n\n if (babelTypes.isNewExpression(node)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n node: node.callee,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isImportDefaultSpecifier(node) || babelTypes.isImportSpecifier(node)) {\n return resolveImportSpecifier({babelConfig, file, filename, fnArguments, node, resolver})\n }\n\n if (babelTypes.isAssignmentPattern(node)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.right,\n params,\n resolver,\n scope,\n })\n }\n\n // Handle TypeScript type assertions (e.g., `'foo' as string`)\n if (babelTypes.isTSAsExpression(node)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.expression,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isMemberExpression(node)) {\n const propertyName = getMemberPropertyName(node)\n const objExpr = resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.object,\n resolver,\n scope,\n })\n const prop = findObjectProperty(objExpr, propertyName, filename, node)\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: prop.value,\n params,\n resolver,\n scope,\n })\n }\n\n throw new Error(\n `Unsupported expression type: ${node.type} in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveIdentifier({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n params,\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments: babelTypes.Node[]\n node: babelTypes.Identifier\n params: babelTypes.Node[]\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): resolveExpressionReturnType {\n const paramIndex = params.findIndex(\n (param) =>\n (babelTypes.isIdentifier(param) && node.name === param.name) ||\n (babelTypes.isAssignmentPattern(param) &&\n babelTypes.isIdentifier(param.left) &&\n node.name === param.left.name),\n )\n let argument = fnArguments[paramIndex]\n if (!argument && paramIndex !== -1 && babelTypes.isAssignmentPattern(params[paramIndex])) {\n argument = params[paramIndex].right\n }\n if (argument && babelTypes.isLiteral(argument)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: argument,\n params,\n resolver,\n scope,\n })\n }\n const binding = scope.getBinding(node.name)\n if (binding) {\n if (babelTypes.isIdentifier(binding.path.node)) {\n const isSame = binding.path.node.name === node.name\n if (isSame) {\n throw new Error(\n `Could not resolve same identifier \"${node.name}\" in \"${filename}:${node.loc?.start.line}:${node.loc?.start.column}\"`,\n )\n }\n }\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: binding.path.node,\n params,\n resolver,\n scope,\n })\n }\n\n throw new Error(\n `Could not find binding for node \"${node.name}\" in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveCallExpression({\n babelConfig,\n file,\n filename,\n node,\n params,\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments: babelTypes.Node[]\n node: babelTypes.CallExpression\n params: babelTypes.Node[]\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): resolveExpressionReturnType {\n const {callee} = node\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments: node.arguments,\n node: callee,\n params,\n resolver,\n scope,\n })\n}\n\nfunction resolveImportBinding({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n node: babelTypes.ExportSpecifier | babelTypes.ImportDefaultSpecifier | babelTypes.ImportSpecifier\n resolver: NodeJS.RequireResolve\n}) {\n let importDeclaration: babelTypes.ImportDeclaration | undefined\n traverse(file, {\n ImportDeclaration(n) {\n if (!babelTypes.isImportDeclaration(n.node)) {\n return\n }\n for (const specifier of n.node.specifiers) {\n if (\n babelTypes.isImportDefaultSpecifier(specifier) &&\n specifier.local.loc?.identifierName === node.local.name\n ) {\n importDeclaration = n.node\n break\n }\n if (specifier.local.name === node.local.name) {\n importDeclaration = n.node\n }\n }\n },\n })\n\n if (!importDeclaration) {\n throw new Error(`Could not find import declaration for ${node.local.name}`)\n }\n\n const importName = node.local.name\n const importFileName = importDeclaration.source.value\n\n const importPath =\n importFileName.startsWith('./') || importFileName.startsWith('../')\n ? path.resolve(path.dirname(filename), importFileName)\n : importFileName\n\n const resolvedFile = resolver(formatPath(importPath))\n const source = fs.readFileSync(resolvedFile)\n const tree = parseSourceFile(source.toString(), resolvedFile, babelConfig)\n\n let scope: Scope | undefined\n traverse(tree, {\n Program(p) {\n scope = p.scope\n },\n })\n if (!scope) {\n throw new Error(`Could not find scope for ${filename}`)\n }\n\n const binding = scope.getBinding(importName)\n return {binding, importFileName, importName, resolvedFile, scope, tree}\n}\n\nfunction resolveImportSpecifier({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments: babelTypes.Node[]\n node: babelTypes.ExportSpecifier | babelTypes.ImportDefaultSpecifier | babelTypes.ImportSpecifier\n resolver: NodeJS.RequireResolve\n}): resolveExpressionReturnType {\n const {binding, importFileName, importName, resolvedFile, scope, tree} = resolveImportBinding({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n })\n\n if (binding) {\n return resolveExpression({\n babelConfig,\n file: tree,\n filename: resolvedFile,\n fnArguments,\n node: binding.path.node,\n resolver,\n scope,\n })\n }\n\n // It's not a global binding, but it might be a named export\n let namedExport: babelTypes.ExportNamedDeclaration | undefined\n let newImportName: string | undefined\n traverse(tree, {\n ExportDeclaration(p) {\n if (p.node.type === 'ExportNamedDeclaration') {\n for (const specifier of p.node.specifiers) {\n if (\n specifier.type === 'ExportSpecifier' &&\n specifier.exported.type === 'Identifier' &&\n specifier.exported.name === importName\n ) {\n namedExport = p.node\n newImportName = specifier.exported.name\n }\n }\n }\n },\n })\n\n if (namedExport && newImportName) {\n return resolveExportSpecifier({\n babelConfig,\n filename: resolvedFile,\n fnArguments,\n importName: newImportName,\n node: namedExport,\n resolver,\n })\n }\n\n let result: resolveExpressionReturnType | undefined\n traverse(tree, {\n ExportDeclaration(p) {\n if (p.node.type === 'ExportAllDeclaration') {\n try {\n result = resolveExportSpecifier({\n babelConfig,\n filename: resolvedFile,\n fnArguments,\n importName,\n node: p.node,\n resolver,\n })\n } catch (e) {\n if ((e as Error).cause !== `noBinding:${importName}`) throw e\n }\n }\n },\n })\n if (result) return result\n\n throw new Error(`Could not find binding for import \"${importName}\" in ${importFileName}`)\n}\n\nfunction resolveExportSpecifier({\n babelConfig,\n filename,\n fnArguments,\n importName,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n filename: string\n fnArguments: babelTypes.Node[]\n importName: string\n node: babelTypes.ExportAllDeclaration | babelTypes.ExportNamedDeclaration\n resolver: NodeJS.RequireResolve\n}): resolveExpressionReturnType {\n if (!node.source) {\n throw new Error(`Could not find source for export \"${importName}\" in ${filename}`)\n }\n\n const importFileName = node.source.value\n const importPath = path.resolve(path.dirname(filename), importFileName)\n const resolvedFile = resolver(formatPath(importPath))\n const source = fs.readFileSync(resolvedFile)\n const tree = parseSourceFile(source.toString(), resolvedFile, babelConfig)\n\n let newScope: Scope | undefined\n traverse(tree, {\n Program(p) {\n newScope = p.scope\n },\n })\n if (!newScope) {\n throw new Error(`Could not find scope for ${filename}`)\n }\n\n const binding = newScope.getBinding(importName)\n if (binding) {\n return resolveExpression({\n babelConfig,\n file: tree,\n filename: resolvedFile,\n fnArguments,\n node: binding.path.node,\n resolver,\n scope: newScope,\n })\n }\n\n throw new Error(`Could not find binding for export \"${importName}\" in ${importFileName}`, {\n cause: `noBinding:${importName}`,\n })\n}\n\nfunction getMemberPropertyName(node: babelTypes.MemberExpression): string {\n const {computed, loc, property} = node\n if (!computed && babelTypes.isIdentifier(property)) {\n return property.name\n }\n if (computed && babelTypes.isStringLiteral(property)) {\n return property.value\n }\n const locInfo = loc ? `${loc.filename}:${loc.start.line}:${loc.start.column}` : 'unknown location'\n throw new Error(`Unsupported MemberExpression property type: ${property.type} @ ${locInfo}`)\n}\n\nfunction findObjectProperty(\n objExpr: babelTypes.ObjectExpression,\n propertyName: string,\n filename: string,\n node: babelTypes.Node,\n): babelTypes.ObjectProperty {\n for (const prop of objExpr.properties) {\n if (!babelTypes.isObjectProperty(prop)) continue\n if (babelTypes.isIdentifier(prop.key) && prop.key.name === propertyName) {\n return prop\n }\n if (babelTypes.isStringLiteral(prop.key) && prop.key.value === propertyName) {\n return prop\n }\n }\n throw new Error(\n `Could not find property \"${propertyName}\" in object expression in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n node: babelTypes.Node\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): babelTypes.ObjectExpression {\n if (babelTypes.isObjectExpression(node)) {\n return node\n }\n if (babelTypes.isTSAsExpression(node)) {\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.expression,\n resolver,\n scope,\n })\n }\n if (babelTypes.isIdentifier(node)) {\n const binding = scope.getBinding(node.name)\n if (!binding) {\n throw new Error(`Could not find binding for \"${node.name}\" in ${filename}`)\n }\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: binding.path.node,\n resolver,\n scope,\n })\n }\n if (babelTypes.isVariableDeclarator(node)) {\n if (!node.init) {\n throw new Error(`Variable declarator has no init`)\n }\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.init,\n resolver,\n scope,\n })\n }\n if (babelTypes.isMemberExpression(node)) {\n const propertyName = getMemberPropertyName(node)\n const objExpr = resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.object,\n resolver,\n scope,\n })\n const prop = findObjectProperty(objExpr, propertyName, filename, node)\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: prop.value,\n resolver,\n scope,\n })\n }\n if (babelTypes.isImportDefaultSpecifier(node) || babelTypes.isImportSpecifier(node)) {\n return resolveImportToObjectExpression({babelConfig, file, filename, node, resolver})\n }\n throw new Error(\n `Cannot resolve node type \"${node.type}\" to ObjectExpression in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveImportToObjectExpression({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n node: babelTypes.ImportDefaultSpecifier | babelTypes.ImportSpecifier\n resolver: NodeJS.RequireResolve\n}): babelTypes.ObjectExpression {\n const {binding, importFileName, importName, resolvedFile, scope, tree} = resolveImportBinding({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n })\n\n if (!binding) {\n throw new Error(`Could not find binding for import \"${importName}\" in ${importFileName}`)\n }\n\n return resolveToObjectExpression({\n babelConfig,\n file: tree,\n filename: resolvedFile,\n node: binding.path.node,\n resolver,\n scope,\n })\n}\n"],"names":["fs","path","traverse","Scope","babelTypes","createDebug","formatPath","parseSourceFile","debug","TAGGED_TEMPLATE_ALLOW_LIST","Set","FUNCTION_WRAPPER_ALLOW_LIST","resolveExpression","babelConfig","file","filename","fnArguments","node","params","resolver","scope","type","loc","start","line","column","isTaggedTemplateExpression","isIdentifier","tag","has","name","quasi","isTemplateLiteral","resolvedExpressions","expressions","map","expression","quasis","idx","value","cooked","join","isLiteral","Error","toString","resolveIdentifier","isVariableDeclarator","init","isAssignmentPattern","id","right","isCallExpression","callee","arguments","resolveCallExpression","isArrowFunctionExpression","isFunctionDeclaration","isFunctionExpression","newScope","i","param","entries","push","body","isNewExpression","isImportDefaultSpecifier","isImportSpecifier","resolveImportSpecifier","isTSAsExpression","isMemberExpression","propertyName","getMemberPropertyName","objExpr","resolveToObjectExpression","object","prop","findObjectProperty","paramIndex","findIndex","left","argument","binding","getBinding","isSame","resolveImportBinding","importDeclaration","ImportDeclaration","n","isImportDeclaration","specifier","specifiers","local","identifierName","importName","importFileName","source","importPath","startsWith","resolve","dirname","resolvedFile","readFileSync","tree","Program","p","namedExport","newImportName","ExportDeclaration","exported","resolveExportSpecifier","result","e","cause","computed","property","isStringLiteral","locInfo","properties","isObjectProperty","key","isObjectExpression","resolveImportToObjectExpression"],"mappings":"AAAA,OAAOA,QAAQ,UAAS;AACxB,OAAOC,UAAU,YAAW;AAE5B,SAA+BC,QAAQ,QAAO,cAAa;AAC3D,SAAQC,KAAK,QAAO,kBAAiB;AACrC,YAAYC,gBAAgB,eAAc;AAC1C,SAAQC,WAAW,QAAO,OAAM;AAEhC,SAAQC,UAAU,QAAO,yBAAwB;AACjD,SAAQC,eAAe,QAAO,mBAAkB;AAEhD,MAAMC,QAAQH,YAAY;AAI1B,MAAMI,6BAA6B,IAAIC,IAAI;IAAC;CAAO;AACnD,MAAMC,8BAA8B,IAAID,IAAI;IAAC;CAAc;AAE3D;;;;CAIC,GACD,OAAO,SAASE,kBAAkB,EAChCC,WAAW,EACXC,IAAI,EACJC,QAAQ,EACRC,cAAc,EAAE,EAChBC,IAAI,EACJC,SAAS,EAAE,EACXC,QAAQ,EACRC,KAAK,EAUN;IACCZ,MACE,CAAC,eAAe,EAAES,KAAKI,IAAI,CAAC,IAAI,EAAEN,SAAS,CAAC,EAAEE,KAAK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YACAC;YACAC;YACAE,MAAMA,KAAKyD,MAAM;YACjBvD;YACAC;QACF;QACA,MAAMuD,OAAOC,mBAAmBJ,SAASF,cAAcvD,UAAUE;QACjE,OAAOwD,0BAA0B;YAC/B5D;YACAC;YACAC;YACAE,MAAM0D,KAAKpC,KAAK;YAChBpB;YACAC;QACF;IACF;IACA,IAAIhB,WAAW6D,wBAAwB,CAAChD,SAASb,WAAW8D,iBAAiB,CAACjD,OAAO;QACnF,OAAOwG,gCAAgC;YAAC5G;YAAaC;YAAMC;YAAUE;YAAME;QAAQ;IACrF;IACA,MAAM,IAAIwB,MACR,CAAC,0BAA0B,EAAE1B,KAAKI,IAAI,CAAC,yBAAyB,EAAEN,SAAS,CAAC,EAAEE,KAAKK,GAAG,EAAEC,MAAMC,KAAK,CAAC,EAAEP,KAAKK,GAAG,EAAEC,MAAME,QAAQ;AAElI;AAEA,SAASgG,gCAAgC,EACvC5G,WAAW,EACXC,IAAI,EACJC,QAAQ,EACRE,IAAI,EACJE,QAAQ,EAOT;IACC,MAAM,EAAC8D,OAAO,EAAEa,cAAc,EAAED,UAAU,EAAEO,YAAY,EAAEhF,KAAK,EAAEkF,IAAI,EAAC,GAAGlB,qBAAqB;QAC5FvE;QACAC;QACAC;QACAE;QACAE;IACF;IAEA,IAAI,CAAC8D,SAAS;QACZ,MAAM,IAAItC,MAAM,CAAC,mCAAmC,EAAEkD,WAAW,KAAK,EAAEC,gBAAgB;IAC1F;IAEA,OAAOrB,0BAA0B;QAC/B5D;QACAC,MAAMwF;QACNvF,UAAUqF;QACVnF,MAAMgE,QAAQhF,IAAI,CAACgB,IAAI;QACvBE;QACAC;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../src/typescript/expressionResolvers.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\n\nimport {type TransformOptions, traverse} from '@babel/core'\nimport {Scope} from '@babel/traverse'\nimport * as babelTypes from '@babel/types'\nimport {createDebug} from 'obug'\n\nimport {formatPath} from '../utils/formatPath.js'\nimport {parseSourceFile} from './parseSource.js'\n\nconst debug = createDebug('sanity:codegen:findQueries:debug')\n\ntype resolveExpressionReturnType = string\n\nconst TAGGED_TEMPLATE_ALLOW_LIST = new Set(['groq'])\nconst FUNCTION_WRAPPER_ALLOW_LIST = new Set(['defineQuery'])\n\n/**\n * resolveExpression takes a node and returns the resolved value of the expression.\n * @beta\n * @internal\n */\nexport function resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments = [],\n node,\n params = [],\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments?: babelTypes.Node[]\n node: babelTypes.Node\n params?: babelTypes.Node[]\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): resolveExpressionReturnType {\n debug(\n `Resolving node ${node.type} in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n if (\n babelTypes.isTaggedTemplateExpression(node) &&\n babelTypes.isIdentifier(node.tag) &&\n TAGGED_TEMPLATE_ALLOW_LIST.has(node.tag.name)\n ) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.quasi,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isTemplateLiteral(node)) {\n const resolvedExpressions = node.expressions.map((expression) =>\n resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: expression,\n params,\n resolver,\n scope,\n }),\n )\n return node.quasis\n .map((quasi, idx) => {\n return (quasi.value.cooked || '') + (resolvedExpressions[idx] || '')\n })\n .join('')\n }\n\n if (babelTypes.isLiteral(node)) {\n if (node.type === 'NullLiteral' || node.type === 'RegExpLiteral') {\n throw new Error(`Unsupported literal type: ${node.type}`)\n }\n\n return node.value.toString()\n }\n\n if (babelTypes.isIdentifier(node)) {\n return resolveIdentifier({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isVariableDeclarator(node)) {\n const init = node.init ?? (babelTypes.isAssignmentPattern(node.id) && node.id.right)\n if (!init) {\n throw new Error(`Unsupported variable declarator`)\n }\n\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: init,\n resolver,\n scope,\n })\n }\n\n if (\n babelTypes.isCallExpression(node) &&\n babelTypes.isIdentifier(node.callee) &&\n FUNCTION_WRAPPER_ALLOW_LIST.has(node.callee.name)\n ) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n node: node.arguments[0]!,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isCallExpression(node)) {\n return resolveCallExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n params,\n resolver,\n scope,\n })\n }\n\n if (\n babelTypes.isArrowFunctionExpression(node) ||\n babelTypes.isFunctionDeclaration(node) ||\n babelTypes.isFunctionExpression(node)\n ) {\n const newScope = new Scope(scope.path, scope)\n\n for (const [i, param] of params.entries()) {\n newScope.push({\n id: param as babelTypes.LVal,\n init: fnArguments[i] as babelTypes.Expression | undefined,\n })\n }\n\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.body,\n params: node.params,\n resolver,\n scope: newScope,\n })\n }\n\n if (babelTypes.isNewExpression(node)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n node: node.callee,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isImportDefaultSpecifier(node) || babelTypes.isImportSpecifier(node)) {\n return resolveImportSpecifier({babelConfig, file, filename, fnArguments, node, resolver})\n }\n\n if (babelTypes.isAssignmentPattern(node)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.right,\n params,\n resolver,\n scope,\n })\n }\n\n // Handle TypeScript type assertions (e.g., `'foo' as string`)\n if (babelTypes.isTSAsExpression(node)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: node.expression,\n params,\n resolver,\n scope,\n })\n }\n\n if (babelTypes.isMemberExpression(node)) {\n const propertyName = getMemberPropertyName(node)\n const objExpr = resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.object,\n resolver,\n scope,\n })\n const prop = findObjectProperty(objExpr, propertyName, filename, node)\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: prop.value,\n params,\n resolver,\n scope,\n })\n }\n\n throw new Error(\n `Unsupported expression type: ${node.type} in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveIdentifier({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n params,\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments: babelTypes.Node[]\n node: babelTypes.Identifier\n params: babelTypes.Node[]\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): resolveExpressionReturnType {\n const paramIndex = params.findIndex(\n (param) =>\n (babelTypes.isIdentifier(param) && node.name === param.name) ||\n (babelTypes.isAssignmentPattern(param) &&\n babelTypes.isIdentifier(param.left) &&\n node.name === param.left.name),\n )\n let argument = fnArguments[paramIndex]\n if (!argument && paramIndex !== -1 && babelTypes.isAssignmentPattern(params[paramIndex])) {\n argument = params[paramIndex].right\n }\n // `isLiteral` is true for a template literal.\n if (argument && babelTypes.isTemplateLiteral(argument)) {\n const forwardsThisParameter = argument.expressions.some(\n (expression) => babelTypes.isIdentifier(expression) && expression.name === node.name,\n )\n if (forwardsThisParameter) {\n throw new Error(\n `Could not forward parameter \"${node.name}\" through a template literal in ${filename}:${argument.loc?.start.line}:${argument.loc?.start.column}`,\n )\n }\n const unbindParameter = (items: babelTypes.Node[]) =>\n items.filter((_, index) => index !== paramIndex)\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments: unbindParameter(fnArguments),\n node: argument,\n params: unbindParameter(params),\n resolver,\n scope,\n })\n }\n if (argument && babelTypes.isLiteral(argument)) {\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: argument,\n params,\n resolver,\n scope,\n })\n }\n const binding = scope.getBinding(node.name)\n if (binding) {\n if (babelTypes.isIdentifier(binding.path.node)) {\n const isSame = binding.path.node.name === node.name\n if (isSame) {\n throw new Error(\n `Could not resolve same identifier \"${node.name}\" in \"${filename}:${node.loc?.start.line}:${node.loc?.start.column}\"`,\n )\n }\n }\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments,\n node: binding.path.node,\n params,\n resolver,\n scope,\n })\n }\n\n throw new Error(\n `Could not find binding for node \"${node.name}\" in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveCallExpression({\n babelConfig,\n file,\n filename,\n node,\n params,\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments: babelTypes.Node[]\n node: babelTypes.CallExpression\n params: babelTypes.Node[]\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): resolveExpressionReturnType {\n const {callee} = node\n return resolveExpression({\n babelConfig,\n file,\n filename,\n fnArguments: node.arguments,\n node: callee,\n params,\n resolver,\n scope,\n })\n}\n\nfunction resolveImportBinding({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n node: babelTypes.ExportSpecifier | babelTypes.ImportDefaultSpecifier | babelTypes.ImportSpecifier\n resolver: NodeJS.RequireResolve\n}) {\n let importDeclaration: babelTypes.ImportDeclaration | undefined\n traverse(file, {\n ImportDeclaration(n) {\n if (!babelTypes.isImportDeclaration(n.node)) {\n return\n }\n for (const specifier of n.node.specifiers) {\n if (\n babelTypes.isImportDefaultSpecifier(specifier) &&\n specifier.local.loc?.identifierName === node.local.name\n ) {\n importDeclaration = n.node\n break\n }\n if (specifier.local.name === node.local.name) {\n importDeclaration = n.node\n }\n }\n },\n })\n\n if (!importDeclaration) {\n throw new Error(`Could not find import declaration for ${node.local.name}`)\n }\n\n const importName = node.local.name\n const importFileName = importDeclaration.source.value\n\n const importPath =\n importFileName.startsWith('./') || importFileName.startsWith('../')\n ? path.resolve(path.dirname(filename), importFileName)\n : importFileName\n\n const resolvedFile = resolver(formatPath(importPath))\n const source = fs.readFileSync(resolvedFile)\n const tree = parseSourceFile(source.toString(), resolvedFile, babelConfig)\n\n let scope: Scope | undefined\n traverse(tree, {\n Program(p) {\n scope = p.scope\n },\n })\n if (!scope) {\n throw new Error(`Could not find scope for ${filename}`)\n }\n\n const binding = scope.getBinding(importName)\n return {binding, importFileName, importName, resolvedFile, scope, tree}\n}\n\nfunction resolveImportSpecifier({\n babelConfig,\n file,\n filename,\n fnArguments,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n fnArguments: babelTypes.Node[]\n node: babelTypes.ExportSpecifier | babelTypes.ImportDefaultSpecifier | babelTypes.ImportSpecifier\n resolver: NodeJS.RequireResolve\n}): resolveExpressionReturnType {\n const {binding, importFileName, importName, resolvedFile, scope, tree} = resolveImportBinding({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n })\n\n if (binding) {\n return resolveExpression({\n babelConfig,\n file: tree,\n filename: resolvedFile,\n fnArguments,\n node: binding.path.node,\n resolver,\n scope,\n })\n }\n\n // It's not a global binding, but it might be a named export\n let namedExport: babelTypes.ExportNamedDeclaration | undefined\n let newImportName: string | undefined\n traverse(tree, {\n ExportDeclaration(p) {\n if (p.node.type === 'ExportNamedDeclaration') {\n for (const specifier of p.node.specifiers) {\n if (\n specifier.type === 'ExportSpecifier' &&\n specifier.exported.type === 'Identifier' &&\n specifier.exported.name === importName\n ) {\n namedExport = p.node\n newImportName = specifier.exported.name\n }\n }\n }\n },\n })\n\n if (namedExport && newImportName) {\n return resolveExportSpecifier({\n babelConfig,\n filename: resolvedFile,\n fnArguments,\n importName: newImportName,\n node: namedExport,\n resolver,\n })\n }\n\n let result: resolveExpressionReturnType | undefined\n traverse(tree, {\n ExportDeclaration(p) {\n if (p.node.type === 'ExportAllDeclaration') {\n try {\n result = resolveExportSpecifier({\n babelConfig,\n filename: resolvedFile,\n fnArguments,\n importName,\n node: p.node,\n resolver,\n })\n } catch (e) {\n if ((e as Error).cause !== `noBinding:${importName}`) throw e\n }\n }\n },\n })\n if (result) return result\n\n throw new Error(`Could not find binding for import \"${importName}\" in ${importFileName}`)\n}\n\nfunction resolveExportSpecifier({\n babelConfig,\n filename,\n fnArguments,\n importName,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n filename: string\n fnArguments: babelTypes.Node[]\n importName: string\n node: babelTypes.ExportAllDeclaration | babelTypes.ExportNamedDeclaration\n resolver: NodeJS.RequireResolve\n}): resolveExpressionReturnType {\n if (!node.source) {\n throw new Error(`Could not find source for export \"${importName}\" in ${filename}`)\n }\n\n const importFileName = node.source.value\n const importPath = path.resolve(path.dirname(filename), importFileName)\n const resolvedFile = resolver(formatPath(importPath))\n const source = fs.readFileSync(resolvedFile)\n const tree = parseSourceFile(source.toString(), resolvedFile, babelConfig)\n\n let newScope: Scope | undefined\n traverse(tree, {\n Program(p) {\n newScope = p.scope\n },\n })\n if (!newScope) {\n throw new Error(`Could not find scope for ${filename}`)\n }\n\n const binding = newScope.getBinding(importName)\n if (binding) {\n return resolveExpression({\n babelConfig,\n file: tree,\n filename: resolvedFile,\n fnArguments,\n node: binding.path.node,\n resolver,\n scope: newScope,\n })\n }\n\n throw new Error(`Could not find binding for export \"${importName}\" in ${importFileName}`, {\n cause: `noBinding:${importName}`,\n })\n}\n\nfunction getMemberPropertyName(node: babelTypes.MemberExpression): string {\n const {computed, loc, property} = node\n if (!computed && babelTypes.isIdentifier(property)) {\n return property.name\n }\n if (computed && babelTypes.isStringLiteral(property)) {\n return property.value\n }\n const locInfo = loc ? `${loc.filename}:${loc.start.line}:${loc.start.column}` : 'unknown location'\n throw new Error(`Unsupported MemberExpression property type: ${property.type} @ ${locInfo}`)\n}\n\nfunction findObjectProperty(\n objExpr: babelTypes.ObjectExpression,\n propertyName: string,\n filename: string,\n node: babelTypes.Node,\n): babelTypes.ObjectProperty {\n for (const prop of objExpr.properties) {\n if (!babelTypes.isObjectProperty(prop)) continue\n if (babelTypes.isIdentifier(prop.key) && prop.key.name === propertyName) {\n return prop\n }\n if (babelTypes.isStringLiteral(prop.key) && prop.key.value === propertyName) {\n return prop\n }\n }\n throw new Error(\n `Could not find property \"${propertyName}\" in object expression in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n scope,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n node: babelTypes.Node\n resolver: NodeJS.RequireResolve\n scope: Scope\n}): babelTypes.ObjectExpression {\n if (babelTypes.isObjectExpression(node)) {\n return node\n }\n if (babelTypes.isTSAsExpression(node)) {\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.expression,\n resolver,\n scope,\n })\n }\n if (babelTypes.isIdentifier(node)) {\n const binding = scope.getBinding(node.name)\n if (!binding) {\n throw new Error(`Could not find binding for \"${node.name}\" in ${filename}`)\n }\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: binding.path.node,\n resolver,\n scope,\n })\n }\n if (babelTypes.isVariableDeclarator(node)) {\n if (!node.init) {\n throw new Error(`Variable declarator has no init`)\n }\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.init,\n resolver,\n scope,\n })\n }\n if (babelTypes.isMemberExpression(node)) {\n const propertyName = getMemberPropertyName(node)\n const objExpr = resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: node.object,\n resolver,\n scope,\n })\n const prop = findObjectProperty(objExpr, propertyName, filename, node)\n return resolveToObjectExpression({\n babelConfig,\n file,\n filename,\n node: prop.value,\n resolver,\n scope,\n })\n }\n if (babelTypes.isImportDefaultSpecifier(node) || babelTypes.isImportSpecifier(node)) {\n return resolveImportToObjectExpression({babelConfig, file, filename, node, resolver})\n }\n throw new Error(\n `Cannot resolve node type \"${node.type}\" to ObjectExpression in ${filename}:${node.loc?.start.line}:${node.loc?.start.column}`,\n )\n}\n\nfunction resolveImportToObjectExpression({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n}: {\n babelConfig: TransformOptions\n file: babelTypes.File\n filename: string\n node: babelTypes.ImportDefaultSpecifier | babelTypes.ImportSpecifier\n resolver: NodeJS.RequireResolve\n}): babelTypes.ObjectExpression {\n const {binding, importFileName, importName, resolvedFile, scope, tree} = resolveImportBinding({\n babelConfig,\n file,\n filename,\n node,\n resolver,\n })\n\n if (!binding) {\n throw new Error(`Could not find binding for import \"${importName}\" in ${importFileName}`)\n }\n\n return resolveToObjectExpression({\n babelConfig,\n file: tree,\n filename: resolvedFile,\n node: binding.path.node,\n resolver,\n scope,\n })\n}\n"],"names":["fs","path","traverse","Scope","babelTypes","createDebug","formatPath","parseSourceFile","debug","TAGGED_TEMPLATE_ALLOW_LIST","Set","FUNCTION_WRAPPER_ALLOW_LIST","resolveExpression","babelConfig","file","filename","fnArguments","node","params","resolver","scope","type","loc","start","line","column","isTaggedTemplateExpression","isIdentifier","tag","has","name","quasi","isTemplateLiteral","resolvedExpressions","expressions","map","expression","quasis","idx","value","cooked","join","isLiteral","Error","toString","resolveIdentifier","isVariableDeclarator","init","isAssignmentPattern","id","right","isCallExpression","callee","arguments","resolveCallExpression","isArrowFunctionExpression","isFunctionDeclaration","isFunctionExpression","newScope","i","param","entries","push","body","isNewExpression","isImportDefaultSpecifier","isImportSpecifier","resolveImportSpecifier","isTSAsExpression","isMemberExpression","propertyName","getMemberPropertyName","objExpr","resolveToObjectExpression","object","prop","findObjectProperty","paramIndex","findIndex","left","argument","forwardsThisParameter","some","unbindParameter","items","filter","_","index","binding","getBinding","isSame","resolveImportBinding","importDeclaration","ImportDeclaration","n","isImportDeclaration","specifier","specifiers","local","identifierName","importName","importFileName","source","importPath","startsWith","resolve","dirname","resolvedFile","readFileSync","tree","Program","p","namedExport","newImportName","ExportDeclaration","exported","resolveExportSpecifier","result","e","cause","computed","property","isStringLiteral","locInfo","properties","isObjectProperty","key","isObjectExpression","resolveImportToObjectExpression"],"mappings":"AAAA,OAAOA,QAAQ,UA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A0B;YACxC5D;YACAC;YACAC;YACAE,MAAMA,KAAKyD,MAAM;YACjBvD;YACAC;QACF;QACA,MAAMuD,OAAOC,mBAAmBJ,SAASF,cAAcvD,UAAUE;QACjE,OAAOwD,0BAA0B;YAC/B5D;YACAC;YACAC;YACAE,MAAM0D,KAAKpC,KAAK;YAChBpB;YACAC;QACF;IACF;IACA,IAAIhB,WAAW6D,wBAAwB,CAAChD,SAASb,WAAW8D,iBAAiB,CAACjD,OAAO;QACnF,OAAO+G,gCAAgC;YAACnH;YAAaC;YAAMC;YAAUE;YAAME;QAAQ;IACrF;IACA,MAAM,IAAIwB,MACR,CAAC,0BAA0B,EAAE1B,KAAKI,IAAI,CAAC,yBAAyB,EAAEN,SAAS,CAAC,EAAEE,KAAKK,GAAG,EAAEC,MAAMC,KAAK,CAAC,EAAEP,KAAKK,GAAG,EAAEC,MAAME,QAAQ;AAElI;AAEA,SAASuG,gCAAgC,EACvCnH,WAAW,EACXC,IAAI,EACJC,QAAQ,EACRE,IAAI,EACJE,QAAQ,EAOT;IACC,MAAM,EAACqE,OAAO,EAAEa,cAAc,EAAED,UAAU,EAAEO,YAAY,EAAEvF,KAAK,EAAEyF,IAAI,EAAC,GAAGlB,qBAAqB;QAC5F9E;QACAC;QACAC;QACAE;QACAE;IACF;IAEA,IAAI,CAACqE,SAAS;QACZ,MAAM,IAAI7C,MAAM,CAAC,mCAAmC,EAAEyD,WAAW,KAAK,EAAEC,gBAAgB;IAC1F;IAEA,OAAO5B,0BAA0B;QAC/B5D;QACAC,MAAM+F;QACN9F,UAAU4F;QACV1F,MAAMuE,QAAQvF,IAAI,CAACgB,IAAI;QACvBE;QACAC;IACF;AACF"}
|
|
@@ -3,7 +3,17 @@ import { typeEvaluate } from 'groq-js';
|
|
|
3
3
|
import { safeParseQuery } from '../safeParseQuery.js';
|
|
4
4
|
import { ARRAY_OF, INTERNAL_REFERENCE_SYMBOL } from './constants.js';
|
|
5
5
|
import { getFilterArrayUnionType, getUniqueIdentifierForName, isIdentifierName, weakMapMemo } from './helpers.js';
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Generates TypeScript types from a schema, as `sanity schema extract` writes it.
|
|
8
|
+
*
|
|
9
|
+
* On construction, every type in the schema gets a TypeScript type and a unique alias
|
|
10
|
+
* identifier; iterating the instance yields them as `{name, id, tsType}`, in schema order.
|
|
11
|
+
* `generateTsType` turns any groq-js type node into a TypeScript type that names schema types
|
|
12
|
+
* by those aliases, and `evaluateQuery` does so for the result of a GROQ query. A tool that
|
|
13
|
+
* types GROQ outside `TypeGenerator`, such as a TypeScript content mapper for `.groq` files,
|
|
14
|
+
* uses this class so that its output agrees with what `sanity typegen generate` writes.
|
|
15
|
+
* @beta
|
|
16
|
+
*/ export class SchemaTypeGenerator {
|
|
7
17
|
schema;
|
|
8
18
|
evaluateQuery = weakMapMemo(// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
9
19
|
({ query })=>{
|
|
@@ -53,6 +63,72 @@ export class SchemaTypeGenerator {
|
|
|
53
63
|
tsType: this.generateTsType(typeNode)
|
|
54
64
|
};
|
|
55
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* The TypeScript type for a groq-js type node. A reference to a schema type becomes its alias
|
|
68
|
+
* identifier, an array of inline types becomes `ArrayOf<...>` (and `isArrayOfUsed` reports
|
|
69
|
+
* that the helper is needed), and a reference object carries `internalGroqTypeReferenceTo`.
|
|
70
|
+
*/ generateTsType(typeNode) {
|
|
71
|
+
switch(typeNode.type){
|
|
72
|
+
case 'array':
|
|
73
|
+
{
|
|
74
|
+
return this.generateArrayTsType(typeNode);
|
|
75
|
+
}
|
|
76
|
+
case 'boolean':
|
|
77
|
+
{
|
|
78
|
+
if (typeNode.value !== undefined) {
|
|
79
|
+
return t.tsLiteralType(t.booleanLiteral(typeNode.value));
|
|
80
|
+
}
|
|
81
|
+
return t.tsBooleanKeyword();
|
|
82
|
+
}
|
|
83
|
+
case 'document':
|
|
84
|
+
{
|
|
85
|
+
return this.generateDocumentTsType(typeNode);
|
|
86
|
+
}
|
|
87
|
+
case 'inline':
|
|
88
|
+
{
|
|
89
|
+
return this.generateInlineTsType(typeNode);
|
|
90
|
+
}
|
|
91
|
+
case 'null':
|
|
92
|
+
{
|
|
93
|
+
return t.tsNullKeyword();
|
|
94
|
+
}
|
|
95
|
+
case 'number':
|
|
96
|
+
{
|
|
97
|
+
if (typeNode.value !== undefined) {
|
|
98
|
+
return t.tsLiteralType(t.numericLiteral(typeNode.value));
|
|
99
|
+
}
|
|
100
|
+
return t.tsNumberKeyword();
|
|
101
|
+
}
|
|
102
|
+
case 'object':
|
|
103
|
+
{
|
|
104
|
+
return this.generateObjectTsType(typeNode);
|
|
105
|
+
}
|
|
106
|
+
case 'string':
|
|
107
|
+
{
|
|
108
|
+
if (typeNode.value !== undefined) {
|
|
109
|
+
return t.tsLiteralType(t.stringLiteral(typeNode.value));
|
|
110
|
+
}
|
|
111
|
+
return t.tsStringKeyword();
|
|
112
|
+
}
|
|
113
|
+
case 'type':
|
|
114
|
+
{
|
|
115
|
+
return this.generateTsType(typeNode.value);
|
|
116
|
+
}
|
|
117
|
+
case 'union':
|
|
118
|
+
{
|
|
119
|
+
return this.generateUnionTsType(typeNode);
|
|
120
|
+
}
|
|
121
|
+
case 'unknown':
|
|
122
|
+
{
|
|
123
|
+
return t.tsUnknownKeyword();
|
|
124
|
+
}
|
|
125
|
+
default:
|
|
126
|
+
{
|
|
127
|
+
throw new Error(`Encountered unsupported node type "${// @ts-expect-error This should never happen
|
|
128
|
+
typeNode.type}" while generating schema types`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
56
132
|
getType(typeName) {
|
|
57
133
|
const tsType = this.tsTypes.get(typeName);
|
|
58
134
|
const id = this.identifiers.get(typeName);
|
|
@@ -188,68 +264,6 @@ export class SchemaTypeGenerator {
|
|
|
188
264
|
propertySignature.optional = attribute.optional;
|
|
189
265
|
return propertySignature;
|
|
190
266
|
}
|
|
191
|
-
generateTsType(typeNode) {
|
|
192
|
-
switch(typeNode.type){
|
|
193
|
-
case 'array':
|
|
194
|
-
{
|
|
195
|
-
return this.generateArrayTsType(typeNode);
|
|
196
|
-
}
|
|
197
|
-
case 'boolean':
|
|
198
|
-
{
|
|
199
|
-
if (typeNode.value !== undefined) {
|
|
200
|
-
return t.tsLiteralType(t.booleanLiteral(typeNode.value));
|
|
201
|
-
}
|
|
202
|
-
return t.tsBooleanKeyword();
|
|
203
|
-
}
|
|
204
|
-
case 'document':
|
|
205
|
-
{
|
|
206
|
-
return this.generateDocumentTsType(typeNode);
|
|
207
|
-
}
|
|
208
|
-
case 'inline':
|
|
209
|
-
{
|
|
210
|
-
return this.generateInlineTsType(typeNode);
|
|
211
|
-
}
|
|
212
|
-
case 'null':
|
|
213
|
-
{
|
|
214
|
-
return t.tsNullKeyword();
|
|
215
|
-
}
|
|
216
|
-
case 'number':
|
|
217
|
-
{
|
|
218
|
-
if (typeNode.value !== undefined) {
|
|
219
|
-
return t.tsLiteralType(t.numericLiteral(typeNode.value));
|
|
220
|
-
}
|
|
221
|
-
return t.tsNumberKeyword();
|
|
222
|
-
}
|
|
223
|
-
case 'object':
|
|
224
|
-
{
|
|
225
|
-
return this.generateObjectTsType(typeNode);
|
|
226
|
-
}
|
|
227
|
-
case 'string':
|
|
228
|
-
{
|
|
229
|
-
if (typeNode.value !== undefined) {
|
|
230
|
-
return t.tsLiteralType(t.stringLiteral(typeNode.value));
|
|
231
|
-
}
|
|
232
|
-
return t.tsStringKeyword();
|
|
233
|
-
}
|
|
234
|
-
case 'type':
|
|
235
|
-
{
|
|
236
|
-
return this.generateTsType(typeNode.value);
|
|
237
|
-
}
|
|
238
|
-
case 'union':
|
|
239
|
-
{
|
|
240
|
-
return this.generateUnionTsType(typeNode);
|
|
241
|
-
}
|
|
242
|
-
case 'unknown':
|
|
243
|
-
{
|
|
244
|
-
return t.tsUnknownKeyword();
|
|
245
|
-
}
|
|
246
|
-
default:
|
|
247
|
-
{
|
|
248
|
-
throw new Error(`Encountered unsupported node type "${// @ts-expect-error This should never happen
|
|
249
|
-
typeNode.type}" while generating schema types`);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
267
|
// Helper function used to generate TS types for union type nodes.
|
|
254
268
|
generateUnionTsType(typeNode) {
|
|
255
269
|
if (typeNode.of.length === 0) return t.tsNeverKeyword();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/typescript/schemaTypeGenerator.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport {\n type ArrayTypeNode,\n type DocumentSchemaType,\n type InlineTypeNode,\n type ObjectAttribute,\n type ObjectTypeNode,\n type SchemaType,\n type TypeDeclarationSchemaType,\n typeEvaluate,\n type TypeNode,\n type UnionTypeNode,\n} from 'groq-js'\n\nimport {safeParseQuery} from '../safeParseQuery.js'\nimport {ARRAY_OF, INTERNAL_REFERENCE_SYMBOL} from './constants.js'\nimport {\n getFilterArrayUnionType,\n getUniqueIdentifierForName,\n isIdentifierName,\n weakMapMemo,\n} from './helpers.js'\nimport {type ExtractedQuery, type TypeEvaluationStats} from './types.js'\n\nexport class SchemaTypeGenerator {\n public readonly schema: SchemaType\n evaluateQuery = weakMapMemo(\n // eslint-disable-next-line unicorn/consistent-function-scoping\n ({query}: Pick<ExtractedQuery, 'query'>): {stats: TypeEvaluationStats; tsType: t.TSType} => {\n const ast = safeParseQuery(query)\n const typeNode = typeEvaluate(ast, this.schema)\n const tsType = this.generateTsType(typeNode)\n const stats = walkAndCountQueryTypeNodeStats(typeNode)\n return {stats, tsType}\n },\n )\n private arrayOfUsed = false\n\n private identifiers = new Map<string, t.Identifier>()\n\n private tsTypes = new Map<string, t.TSType>()\n\n constructor(schema: SchemaType) {\n this.schema = schema\n\n const uniqueTypeNames = new Set<string>()\n for (const type of schema) {\n if (uniqueTypeNames.has(type.name)) {\n throw new Error(\n `Duplicate type name \"${type.name}\" in schema. Type names must be unique within the same schema.`,\n )\n }\n uniqueTypeNames.add(type.name)\n }\n\n for (const type of schema) {\n const currentIdentifierNames = new Set([...this.identifiers.values()].map((id) => id.name))\n const uniqueIdentifier = getUniqueIdentifierForName(type.name, currentIdentifierNames)\n this.identifiers.set(type.name, uniqueIdentifier)\n }\n\n for (const type of schema) {\n this.tsTypes.set(type.name, this.generateTsType(type))\n }\n }\n\n /** The names of the schema's document types, in schema order. */\n documentTypeNames(): string[] {\n return this.schema.filter((schemaType) => schemaType.type === 'document').map(({name}) => name)\n }\n\n /**\n * Evaluates a projection for one document type, as `*[_type == TYPE][0]PROJECTION` without\n * the `null` for a missing document: the SDK projects documents it has found.\n */\n evaluateProjection({documentType, projection}: {documentType: string; projection: string}): {\n stats: TypeEvaluationStats\n tsType: t.TSType\n } {\n const ast = safeParseQuery(`*[_type == ${JSON.stringify(documentType)}][0]${projection}`)\n const typeNode = withoutNull(typeEvaluate(ast, this.schema))\n return {stats: walkAndCountQueryTypeNodeStats(typeNode), tsType: this.generateTsType(typeNode)}\n }\n\n getType(typeName: string): {id: t.Identifier; tsType: t.TSType} | undefined {\n const tsType = this.tsTypes.get(typeName)\n const id = this.identifiers.get(typeName)\n if (tsType && id) return {id, tsType}\n return undefined\n }\n\n hasType(typeName: string): boolean {\n return this.tsTypes.has(typeName)\n }\n\n isArrayOfUsed(): boolean {\n return this.arrayOfUsed\n }\n\n *[Symbol.iterator]() {\n for (const {name} of this.schema) {\n yield {name, ...this.getType(name)!}\n }\n }\n\n typeNames(): string[] {\n return this.schema.map((schemaType) => schemaType.name)\n }\n\n /**\n * Helper function used to generate TS types for arrays of inline types, or arrays of inline types\n * wrapped in the ArrayOf wrapper that adds _key prop\n */\n private generateArrayOfTsType(typeNode: ArrayTypeNode): t.TSTypeReference {\n this.arrayOfUsed = true\n const typeNodes = this.generateTsType(typeNode.of)\n return t.tsTypeReference(ARRAY_OF, t.tsTypeParameterInstantiation([typeNodes]))\n }\n\n // Helper function used to generate TS types for array type nodes.\n private generateArrayTsType(typeNode: ArrayTypeNode): t.TSTypeReference | t.TSUnionType {\n // if it's an array of a single inline type, wrap it in ArrayOf\n if (typeNode.of.type === 'inline') {\n return this.generateArrayOfTsType(typeNode)\n }\n\n // if it's not an inline object and not a union, wrap in Array\n if (typeNode.of.type !== 'union') {\n const typeNodes = this.generateTsType(typeNode.of)\n return t.tsTypeReference(t.identifier('Array'), t.tsTypeParameterInstantiation([typeNodes]))\n }\n\n // if it's not a union type or all of the union type members are non-inlines, wrap type in Array\n if (typeNode.of.of.every((unionTypeNode) => unionTypeNode.type !== 'inline')) {\n const typeNodes = this.generateTsType(typeNode.of)\n return t.tsTypeReference(t.identifier('Array'), t.tsTypeParameterInstantiation([typeNodes]))\n }\n\n // all the union types nodes are inline\n if (typeNode.of.of.every((unionMember) => unionMember.type === 'inline')) {\n return this.generateArrayOfTsType(typeNode)\n }\n\n // some of the union types are inlines, while some are not - split and recurse\n const arrayOfNonInline = getFilterArrayUnionType(typeNode, (member) => member.type !== 'inline')\n const arrayOfInline = getFilterArrayUnionType(typeNode, (member) => member.type === 'inline')\n\n return t.tsUnionType([\n this.generateArrayTsType(arrayOfNonInline),\n this.generateArrayTsType(arrayOfInline),\n ])\n }\n\n // Helper function used to generate TS types for document type nodes.\n private generateDocumentTsType(document: DocumentSchemaType): t.TSType {\n const props = Object.entries(document.attributes).map(([key, node]) =>\n this.generateTsObjectProperty(key, node),\n )\n\n return t.tsTypeLiteral(props)\n }\n\n private generateInlineTsType(typeNode: InlineTypeNode): t.TSType {\n const id = this.identifiers.get(typeNode.name)\n if (!id) {\n // Not found in schema, return unknown type\n return t.addComment(\n t.tsUnknownKeyword(),\n 'trailing',\n ` Unable to locate the referenced type \"${typeNode.name}\" in schema`,\n true,\n )\n }\n\n return t.tsTypeReference(id)\n }\n\n // Helper function used to generate TS types for object type nodes.\n private generateObjectTsType(typeNode: ObjectTypeNode): t.TSType {\n const props: t.TSPropertySignature[] = []\n for (const [key, attribute] of Object.entries(typeNode.attributes)) {\n props.push(this.generateTsObjectProperty(key, attribute))\n }\n const rest = typeNode.rest\n\n if (rest) {\n switch (rest.type) {\n case 'inline': {\n const resolved = this.generateInlineTsType(rest)\n // if object rest is unknown, we can't generate a type literal for it\n if (t.isTSUnknownKeyword(resolved)) return resolved\n return t.tsIntersectionType([t.tsTypeLiteral(props), resolved])\n }\n case 'object': {\n for (const [key, attribute] of Object.entries(rest.attributes)) {\n props.push(this.generateTsObjectProperty(key, attribute))\n }\n break\n }\n case 'unknown': {\n return t.tsUnknownKeyword()\n }\n default: {\n // @ts-expect-error This should never happen\n throw new Error(`Type \"${rest.type}\" not found in schema`)\n }\n }\n }\n\n if (typeNode.dereferencesTo) {\n const derefType = Object.assign(\n t.tsPropertySignature(\n INTERNAL_REFERENCE_SYMBOL,\n t.tsTypeAnnotation(t.tsLiteralType(t.stringLiteral(typeNode.dereferencesTo))),\n ),\n {computed: true, optional: true},\n )\n props.push(derefType)\n }\n\n return t.tsTypeLiteral(props)\n }\n\n // Helper function used to generate TS types for object properties.\n private generateTsObjectProperty(key: string, attribute: ObjectAttribute): t.TSPropertySignature {\n const type = this.generateTsType(attribute.value)\n const keyNode = isIdentifierName(key) ? t.identifier(key) : t.stringLiteral(key)\n const propertySignature = t.tsPropertySignature(keyNode, t.tsTypeAnnotation(type))\n propertySignature.optional = attribute.optional\n\n return propertySignature\n }\n\n private generateTsType(\n typeNode: DocumentSchemaType | TypeDeclarationSchemaType | TypeNode,\n ): t.TSType {\n switch (typeNode.type) {\n case 'array': {\n return this.generateArrayTsType(typeNode)\n }\n case 'boolean': {\n if (typeNode.value !== undefined) {\n return t.tsLiteralType(t.booleanLiteral(typeNode.value))\n }\n return t.tsBooleanKeyword()\n }\n case 'document': {\n return this.generateDocumentTsType(typeNode)\n }\n case 'inline': {\n return this.generateInlineTsType(typeNode)\n }\n case 'null': {\n return t.tsNullKeyword()\n }\n case 'number': {\n if (typeNode.value !== undefined) {\n return t.tsLiteralType(t.numericLiteral(typeNode.value))\n }\n return t.tsNumberKeyword()\n }\n case 'object': {\n return this.generateObjectTsType(typeNode)\n }\n case 'string': {\n if (typeNode.value !== undefined) {\n return t.tsLiteralType(t.stringLiteral(typeNode.value))\n }\n return t.tsStringKeyword()\n }\n case 'type': {\n return this.generateTsType(typeNode.value)\n }\n case 'union': {\n return this.generateUnionTsType(typeNode)\n }\n case 'unknown': {\n return t.tsUnknownKeyword()\n }\n\n default: {\n throw new Error(\n `Encountered unsupported node type \"${\n // @ts-expect-error This should never happen\n typeNode.type\n }\" while generating schema types`,\n )\n }\n }\n }\n\n // Helper function used to generate TS types for union type nodes.\n private generateUnionTsType(typeNode: UnionTypeNode): t.TSType {\n if (typeNode.of.length === 0) return t.tsNeverKeyword()\n if (typeNode.of.length === 1) return this.generateTsType(typeNode.of[0]!)\n return t.tsUnionType(typeNode.of.map((node) => this.generateTsType(node)))\n }\n}\n\nexport function walkAndCountQueryTypeNodeStats(typeNode: TypeNode): TypeEvaluationStats {\n switch (typeNode.type) {\n case 'array': {\n const acc = walkAndCountQueryTypeNodeStats(typeNode.of)\n acc.allTypes += 1 // count the array type itself\n return acc\n }\n case 'object': {\n // if the rest is unknown, we count it as one unknown type\n if (typeNode.rest && typeNode.rest.type === 'unknown') {\n return {allTypes: 2, emptyUnions: 0, unknownTypes: 1} // count the object type itself as well\n }\n\n const restStats = typeNode.rest\n ? walkAndCountQueryTypeNodeStats(typeNode.rest)\n : {allTypes: 0, emptyUnions: 0, unknownTypes: 0}\n\n // count the object type itself\n restStats.allTypes += 1\n\n const attrs = Object.values(typeNode.attributes)\n let acc = restStats\n for (const attribute of attrs) {\n const {allTypes, emptyUnions, unknownTypes} = walkAndCountQueryTypeNodeStats(\n attribute.value,\n )\n acc = {\n allTypes: acc.allTypes + allTypes,\n emptyUnions: acc.emptyUnions + emptyUnions,\n unknownTypes: acc.unknownTypes + unknownTypes,\n }\n }\n return acc\n }\n case 'union': {\n if (typeNode.of.length === 0) {\n return {allTypes: 1, emptyUnions: 1, unknownTypes: 0}\n }\n\n let acc = {allTypes: 1, emptyUnions: 0, unknownTypes: 0} // count the union type itself\n for (const type of typeNode.of) {\n const {allTypes, emptyUnions, unknownTypes} = walkAndCountQueryTypeNodeStats(type)\n acc = {\n allTypes: acc.allTypes + allTypes,\n emptyUnions: acc.emptyUnions + emptyUnions,\n unknownTypes: acc.unknownTypes + unknownTypes,\n }\n }\n return acc\n }\n case 'unknown': {\n return {allTypes: 1, emptyUnions: 0, unknownTypes: 1}\n }\n default: {\n return {allTypes: 1, emptyUnions: 0, unknownTypes: 0}\n }\n }\n}\n\nfunction withoutNull(typeNode: TypeNode): TypeNode {\n if (typeNode.type !== 'union') return typeNode\n const of = typeNode.of.filter((member) => member.type !== 'null')\n return of.length === 1 ? of[0]! : {...typeNode, of}\n}\n"],"names":["t","typeEvaluate","safeParseQuery","ARRAY_OF","INTERNAL_REFERENCE_SYMBOL","getFilterArrayUnionType","getUniqueIdentifierForName","isIdentifierName","weakMapMemo","SchemaTypeGenerator","schema","evaluateQuery","query","ast","typeNode","tsType","generateTsType","stats","walkAndCountQueryTypeNodeStats","arrayOfUsed","identifiers","Map","tsTypes","uniqueTypeNames","Set","type","has","name","Error","add","currentIdentifierNames","values","map","id","uniqueIdentifier","set","documentTypeNames","filter","schemaType","evaluateProjection","documentType","projection","JSON","stringify","withoutNull","getType","typeName","get","undefined","hasType","isArrayOfUsed","Symbol","iterator","typeNames","generateArrayOfTsType","typeNodes","of","tsTypeReference","tsTypeParameterInstantiation","generateArrayTsType","identifier","every","unionTypeNode","unionMember","arrayOfNonInline","member","arrayOfInline","tsUnionType","generateDocumentTsType","document","props","Object","entries","attributes","key","node","generateTsObjectProperty","tsTypeLiteral","generateInlineTsType","addComment","tsUnknownKeyword","generateObjectTsType","attribute","push","rest","resolved","isTSUnknownKeyword","tsIntersectionType","dereferencesTo","derefType","assign","tsPropertySignature","tsTypeAnnotation","tsLiteralType","stringLiteral","computed","optional","value","keyNode","propertySignature","booleanLiteral","tsBooleanKeyword","tsNullKeyword","numericLiteral","tsNumberKeyword","tsStringKeyword","generateUnionTsType","length","tsNeverKeyword","acc","allTypes","emptyUnions","unknownTypes","restStats","attrs"],"mappings":"AAAA,YAAYA,OAAO,eAAc;AACjC,SAQEC,YAAY,QAGP,UAAS;AAEhB,SAAQC,cAAc,QAAO,uBAAsB;AACnD,SAAQC,QAAQ,EAAEC,yBAAyB,QAAO,iBAAgB;AAClE,SACEC,uBAAuB,EACvBC,0BAA0B,EAC1BC,gBAAgB,EAChBC,WAAW,QACN,eAAc;AAGrB,OAAO,MAAMC;IACKC,OAAkB;IAClCC,gBAAgBH,YACd,+DAA+D;IAC/D,CAAC,EAACI,KAAK,EAAgC;QACrC,MAAMC,MAAMX,eAAeU;QAC3B,MAAME,WAAWb,aAAaY,KAAK,IAAI,CAACH,MAAM;QAC9C,MAAMK,SAAS,IAAI,CAACC,cAAc,CAACF;QACnC,MAAMG,QAAQC,+BAA+BJ;QAC7C,OAAO;YAACG;YAAOF;QAAM;IACvB,GACD;IACOI,cAAc,MAAK;IAEnBC,cAAc,IAAIC,MAA2B;IAE7CC,UAAU,IAAID,MAAuB;IAE7C,YAAYX,MAAkB,CAAE;QAC9B,IAAI,CAACA,MAAM,GAAGA;QAEd,MAAMa,kBAAkB,IAAIC;QAC5B,KAAK,MAAMC,QAAQf,OAAQ;YACzB,IAAIa,gBAAgBG,GAAG,CAACD,KAAKE,IAAI,GAAG;gBAClC,MAAM,IAAIC,MACR,CAAC,qBAAqB,EAAEH,KAAKE,IAAI,CAAC,8DAA8D,CAAC;YAErG;YACAJ,gBAAgBM,GAAG,CAACJ,KAAKE,IAAI;QAC/B;QAEA,KAAK,MAAMF,QAAQf,OAAQ;YACzB,MAAMoB,yBAAyB,IAAIN,IAAI;mBAAI,IAAI,CAACJ,WAAW,CAACW,MAAM;aAAG,CAACC,GAAG,CAAC,CAACC,KAAOA,GAAGN,IAAI;YACzF,MAAMO,mBAAmB5B,2BAA2BmB,KAAKE,IAAI,EAAEG;YAC/D,IAAI,CAACV,WAAW,CAACe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CAACmD,MAAM,KAAK,GAAG;oBAC5B,OAAO;wBAACG,UAAU;wBAAGC,aAAa;wBAAGC,cAAc;oBAAC;gBACtD;gBAEA,IAAIH,MAAM;oBAACC,UAAU;oBAAGC,aAAa;oBAAGC,cAAc;gBAAC,EAAE,8BAA8B;;gBACvF,KAAK,MAAMvF,QAAQX,SAAS0C,EAAE,CAAE;oBAC9B,MAAM,EAACsD,QAAQ,EAAEC,WAAW,EAAEC,YAAY,EAAC,GAAG9F,+BAA+BO;oBAC7EoF,MAAM;wBACJC,UAAUD,IAAIC,QAAQ,GAAGA;wBACzBC,aAAaF,IAAIE,WAAW,GAAGA;wBAC/BC,cAAcH,IAAIG,YAAY,GAAGA;oBACnC;gBACF;gBACA,OAAOH;YACT;QACA,KAAK;YAAW;gBACd,OAAO;oBAACC,UAAU;oBAAGC,aAAa;oBAAGC,cAAc;gBAAC;YACtD;QACA;YAAS;gBACP,OAAO;oBAACF,UAAU;oBAAGC,aAAa;oBAAGC,cAAc;gBAAC;YACtD;IACF;AACF;AAEA,SAASpE,YAAY9B,QAAkB;IACrC,IAAIA,SAASW,IAAI,KAAK,SAAS,OAAOX;IACtC,MAAM0C,KAAK1C,SAAS0C,EAAE,CAACnB,MAAM,CAAC,CAAC4B,SAAWA,OAAOxC,IAAI,KAAK;IAC1D,OAAO+B,GAAGmD,MAAM,KAAK,IAAInD,EAAE,CAAC,EAAE,GAAI;QAAC,GAAG1C,QAAQ;QAAE0C;IAAE;AACpD"}
|
|
1
|
+
{"version":3,"sources":["../../src/typescript/schemaTypeGenerator.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport {\n type ArrayTypeNode,\n type DocumentSchemaType,\n type InlineTypeNode,\n type ObjectAttribute,\n type ObjectTypeNode,\n type SchemaType,\n type TypeDeclarationSchemaType,\n typeEvaluate,\n type TypeNode,\n type UnionTypeNode,\n} from 'groq-js'\n\nimport {safeParseQuery} from '../safeParseQuery.js'\nimport {ARRAY_OF, INTERNAL_REFERENCE_SYMBOL} from './constants.js'\nimport {\n getFilterArrayUnionType,\n getUniqueIdentifierForName,\n isIdentifierName,\n weakMapMemo,\n} from './helpers.js'\nimport {type ExtractedQuery, type TypeEvaluationStats} from './types.js'\n\n/**\n * Generates TypeScript types from a schema, as `sanity schema extract` writes it.\n *\n * On construction, every type in the schema gets a TypeScript type and a unique alias\n * identifier; iterating the instance yields them as `{name, id, tsType}`, in schema order.\n * `generateTsType` turns any groq-js type node into a TypeScript type that names schema types\n * by those aliases, and `evaluateQuery` does so for the result of a GROQ query. A tool that\n * types GROQ outside `TypeGenerator`, such as a TypeScript content mapper for `.groq` files,\n * uses this class so that its output agrees with what `sanity typegen generate` writes.\n * @beta\n */\nexport class SchemaTypeGenerator {\n public readonly schema: SchemaType\n evaluateQuery = weakMapMemo(\n // eslint-disable-next-line unicorn/consistent-function-scoping\n ({query}: Pick<ExtractedQuery, 'query'>): {stats: TypeEvaluationStats; tsType: t.TSType} => {\n const ast = safeParseQuery(query)\n const typeNode = typeEvaluate(ast, this.schema)\n const tsType = this.generateTsType(typeNode)\n const stats = walkAndCountQueryTypeNodeStats(typeNode)\n return {stats, tsType}\n },\n )\n private arrayOfUsed = false\n\n private identifiers = new Map<string, t.Identifier>()\n\n private tsTypes = new Map<string, t.TSType>()\n\n constructor(schema: SchemaType) {\n this.schema = schema\n\n const uniqueTypeNames = new Set<string>()\n for (const type of schema) {\n if (uniqueTypeNames.has(type.name)) {\n throw new Error(\n `Duplicate type name \"${type.name}\" in schema. Type names must be unique within the same schema.`,\n )\n }\n uniqueTypeNames.add(type.name)\n }\n\n for (const type of schema) {\n const currentIdentifierNames = new Set([...this.identifiers.values()].map((id) => id.name))\n const uniqueIdentifier = getUniqueIdentifierForName(type.name, currentIdentifierNames)\n this.identifiers.set(type.name, uniqueIdentifier)\n }\n\n for (const type of schema) {\n this.tsTypes.set(type.name, this.generateTsType(type))\n }\n }\n\n /** The names of the schema's document types, in schema order. */\n documentTypeNames(): string[] {\n return this.schema.filter((schemaType) => schemaType.type === 'document').map(({name}) => name)\n }\n\n /**\n * Evaluates a projection for one document type, as `*[_type == TYPE][0]PROJECTION` without\n * the `null` for a missing document: the SDK projects documents it has found.\n */\n evaluateProjection({documentType, projection}: {documentType: string; projection: string}): {\n stats: TypeEvaluationStats\n tsType: t.TSType\n } {\n const ast = safeParseQuery(`*[_type == ${JSON.stringify(documentType)}][0]${projection}`)\n const typeNode = withoutNull(typeEvaluate(ast, this.schema))\n return {stats: walkAndCountQueryTypeNodeStats(typeNode), tsType: this.generateTsType(typeNode)}\n }\n\n /**\n * The TypeScript type for a groq-js type node. A reference to a schema type becomes its alias\n * identifier, an array of inline types becomes `ArrayOf<...>` (and `isArrayOfUsed` reports\n * that the helper is needed), and a reference object carries `internalGroqTypeReferenceTo`.\n */\n generateTsType(typeNode: DocumentSchemaType | TypeDeclarationSchemaType | TypeNode): t.TSType {\n switch (typeNode.type) {\n case 'array': {\n return this.generateArrayTsType(typeNode)\n }\n case 'boolean': {\n if (typeNode.value !== undefined) {\n return t.tsLiteralType(t.booleanLiteral(typeNode.value))\n }\n return t.tsBooleanKeyword()\n }\n case 'document': {\n return this.generateDocumentTsType(typeNode)\n }\n case 'inline': {\n return this.generateInlineTsType(typeNode)\n }\n case 'null': {\n return t.tsNullKeyword()\n }\n case 'number': {\n if (typeNode.value !== undefined) {\n return t.tsLiteralType(t.numericLiteral(typeNode.value))\n }\n return t.tsNumberKeyword()\n }\n case 'object': {\n return this.generateObjectTsType(typeNode)\n }\n case 'string': {\n if (typeNode.value !== undefined) {\n return t.tsLiteralType(t.stringLiteral(typeNode.value))\n }\n return t.tsStringKeyword()\n }\n case 'type': {\n return this.generateTsType(typeNode.value)\n }\n case 'union': {\n return this.generateUnionTsType(typeNode)\n }\n case 'unknown': {\n return t.tsUnknownKeyword()\n }\n\n default: {\n throw new Error(\n `Encountered unsupported node type \"${\n // @ts-expect-error This should never happen\n typeNode.type\n }\" while generating schema types`,\n )\n }\n }\n }\n\n getType(typeName: string): {id: t.Identifier; tsType: t.TSType} | undefined {\n const tsType = this.tsTypes.get(typeName)\n const id = this.identifiers.get(typeName)\n if (tsType && id) return {id, tsType}\n return undefined\n }\n\n hasType(typeName: string): boolean {\n return this.tsTypes.has(typeName)\n }\n\n isArrayOfUsed(): boolean {\n return this.arrayOfUsed\n }\n\n *[Symbol.iterator]() {\n for (const {name} of this.schema) {\n yield {name, ...this.getType(name)!}\n }\n }\n\n typeNames(): string[] {\n return this.schema.map((schemaType) => schemaType.name)\n }\n\n /**\n * Helper function used to generate TS types for arrays of inline types, or arrays of inline types\n * wrapped in the ArrayOf wrapper that adds _key prop\n */\n private generateArrayOfTsType(typeNode: ArrayTypeNode): t.TSTypeReference {\n this.arrayOfUsed = true\n const typeNodes = this.generateTsType(typeNode.of)\n return t.tsTypeReference(ARRAY_OF, t.tsTypeParameterInstantiation([typeNodes]))\n }\n\n // Helper function used to generate TS types for array type nodes.\n private generateArrayTsType(typeNode: ArrayTypeNode): t.TSTypeReference | t.TSUnionType {\n // if it's an array of a single inline type, wrap it in ArrayOf\n if (typeNode.of.type === 'inline') {\n return this.generateArrayOfTsType(typeNode)\n }\n\n // if it's not an inline object and not a union, wrap in Array\n if (typeNode.of.type !== 'union') {\n const typeNodes = this.generateTsType(typeNode.of)\n return t.tsTypeReference(t.identifier('Array'), t.tsTypeParameterInstantiation([typeNodes]))\n }\n\n // if it's not a union type or all of the union type members are non-inlines, wrap type in Array\n if (typeNode.of.of.every((unionTypeNode) => unionTypeNode.type !== 'inline')) {\n const typeNodes = this.generateTsType(typeNode.of)\n return t.tsTypeReference(t.identifier('Array'), t.tsTypeParameterInstantiation([typeNodes]))\n }\n\n // all the union types nodes are inline\n if (typeNode.of.of.every((unionMember) => unionMember.type === 'inline')) {\n return this.generateArrayOfTsType(typeNode)\n }\n\n // some of the union types are inlines, while some are not - split and recurse\n const arrayOfNonInline = getFilterArrayUnionType(typeNode, (member) => member.type !== 'inline')\n const arrayOfInline = getFilterArrayUnionType(typeNode, (member) => member.type === 'inline')\n\n return t.tsUnionType([\n this.generateArrayTsType(arrayOfNonInline),\n this.generateArrayTsType(arrayOfInline),\n ])\n }\n\n // Helper function used to generate TS types for document type nodes.\n private generateDocumentTsType(document: DocumentSchemaType): t.TSType {\n const props = Object.entries(document.attributes).map(([key, node]) =>\n this.generateTsObjectProperty(key, node),\n )\n\n return t.tsTypeLiteral(props)\n }\n\n private generateInlineTsType(typeNode: InlineTypeNode): t.TSType {\n const id = this.identifiers.get(typeNode.name)\n if (!id) {\n // Not found in schema, return unknown type\n return t.addComment(\n t.tsUnknownKeyword(),\n 'trailing',\n ` Unable to locate the referenced type \"${typeNode.name}\" in schema`,\n true,\n )\n }\n\n return t.tsTypeReference(id)\n }\n\n // Helper function used to generate TS types for object type nodes.\n private generateObjectTsType(typeNode: ObjectTypeNode): t.TSType {\n const props: t.TSPropertySignature[] = []\n for (const [key, attribute] of Object.entries(typeNode.attributes)) {\n props.push(this.generateTsObjectProperty(key, attribute))\n }\n const rest = typeNode.rest\n\n if (rest) {\n switch (rest.type) {\n case 'inline': {\n const resolved = this.generateInlineTsType(rest)\n // if object rest is unknown, we can't generate a type literal for it\n if (t.isTSUnknownKeyword(resolved)) return resolved\n return t.tsIntersectionType([t.tsTypeLiteral(props), resolved])\n }\n case 'object': {\n for (const [key, attribute] of Object.entries(rest.attributes)) {\n props.push(this.generateTsObjectProperty(key, attribute))\n }\n break\n }\n case 'unknown': {\n return t.tsUnknownKeyword()\n }\n default: {\n // @ts-expect-error This should never happen\n throw new Error(`Type \"${rest.type}\" not found in schema`)\n }\n }\n }\n\n if (typeNode.dereferencesTo) {\n const derefType = Object.assign(\n t.tsPropertySignature(\n INTERNAL_REFERENCE_SYMBOL,\n t.tsTypeAnnotation(t.tsLiteralType(t.stringLiteral(typeNode.dereferencesTo))),\n ),\n {computed: true, optional: true},\n )\n props.push(derefType)\n }\n\n return t.tsTypeLiteral(props)\n }\n\n // Helper function used to generate TS types for object properties.\n private generateTsObjectProperty(key: string, attribute: ObjectAttribute): t.TSPropertySignature {\n const type = this.generateTsType(attribute.value)\n const keyNode = isIdentifierName(key) ? t.identifier(key) : t.stringLiteral(key)\n const propertySignature = t.tsPropertySignature(keyNode, t.tsTypeAnnotation(type))\n propertySignature.optional = attribute.optional\n\n return propertySignature\n }\n\n // Helper function used to generate TS types for union type nodes.\n private generateUnionTsType(typeNode: UnionTypeNode): t.TSType {\n if (typeNode.of.length === 0) return t.tsNeverKeyword()\n if (typeNode.of.length === 1) return this.generateTsType(typeNode.of[0]!)\n return t.tsUnionType(typeNode.of.map((node) => this.generateTsType(node)))\n }\n}\n\nexport function walkAndCountQueryTypeNodeStats(typeNode: TypeNode): TypeEvaluationStats {\n switch (typeNode.type) {\n case 'array': {\n const acc = walkAndCountQueryTypeNodeStats(typeNode.of)\n acc.allTypes += 1 // count the array type itself\n return acc\n }\n case 'object': {\n // if the rest is unknown, we count it as one unknown type\n if (typeNode.rest && typeNode.rest.type === 'unknown') {\n return {allTypes: 2, emptyUnions: 0, unknownTypes: 1} // count the object type itself as well\n }\n\n const restStats = typeNode.rest\n ? walkAndCountQueryTypeNodeStats(typeNode.rest)\n : {allTypes: 0, emptyUnions: 0, unknownTypes: 0}\n\n // count the object type itself\n restStats.allTypes += 1\n\n const attrs = Object.values(typeNode.attributes)\n let acc = restStats\n for (const attribute of attrs) {\n const {allTypes, emptyUnions, unknownTypes} = walkAndCountQueryTypeNodeStats(\n attribute.value,\n )\n acc = {\n allTypes: acc.allTypes + allTypes,\n emptyUnions: acc.emptyUnions + emptyUnions,\n unknownTypes: acc.unknownTypes + unknownTypes,\n }\n }\n return acc\n }\n case 'union': {\n if (typeNode.of.length === 0) {\n return {allTypes: 1, emptyUnions: 1, unknownTypes: 0}\n }\n\n let acc = {allTypes: 1, emptyUnions: 0, unknownTypes: 0} // count the union type itself\n for (const type of typeNode.of) {\n const {allTypes, emptyUnions, unknownTypes} = walkAndCountQueryTypeNodeStats(type)\n acc = {\n allTypes: acc.allTypes + allTypes,\n emptyUnions: acc.emptyUnions + emptyUnions,\n unknownTypes: acc.unknownTypes + unknownTypes,\n }\n }\n return acc\n }\n case 'unknown': {\n return {allTypes: 1, emptyUnions: 0, unknownTypes: 1}\n }\n default: {\n return {allTypes: 1, emptyUnions: 0, unknownTypes: 0}\n }\n }\n}\n\nfunction withoutNull(typeNode: TypeNode): TypeNode {\n if (typeNode.type !== 'union') return typeNode\n const of = typeNode.of.filter((member) => member.type !== 'null')\n return of.length === 1 ? of[0]! : {...typeNode, of}\n}\n"],"names":["t","typeEvaluate","safeParseQuery","ARRAY_OF","INTERNAL_REFERENCE_SYMBOL","getFilterArrayUnionType","getUniqueIdentifierForName","isIdentifierName","weakMapMemo","SchemaTypeGenerator","schema","evaluateQuery","query","ast","typeNode","tsType","generateTsType","stats","walkAndCountQueryTypeNodeStats","arrayOfUsed","identifiers","Map","tsTypes","uniqueTypeNames","Set","type","has","name","Error","add","currentIdentifierNames","values","map","id","uniqueIdentifier","set","documentTypeNames","filter","schemaType","evaluateProjection","documentType","projection","JSON","stringify","withoutNull","generateArrayTsType","value","undefined","tsLiteralType","booleanLiteral","tsBooleanKeyword","generateDocumentTsType","generateInlineTsType","tsNullKeyword","numericLiteral","tsNumberKeyword","generateObjectTsType","stringLiteral","tsStringKeyword","generateUnionTsType","tsUnknownKeyword","getType","typeName","get","hasType","isArrayOfUsed","Symbol","iterator","typeNames","generateArrayOfTsType","typeNodes","of","tsTypeReference","tsTypeParameterInstantiation","identifier","every","unionTypeNode","unionMember","arrayOfNonInline","member","arrayOfInline","tsUnionType","document","props","Object","entries","attributes","key","node","generateTsObjectProperty","tsTypeLiteral","addComment","attribute","push","rest","resolved","isTSUnknownKeyword","tsIntersectionType","dereferencesTo","derefType","assign","tsPropertySignature","tsTypeAnnotation","computed","optional","keyNode","propertySignature","length","tsNeverKeyword","acc","allTypes","emptyUnions","unknownTypes","restStats","attrs"],"mappings":"AAAA,YAAYA,OAAO,eAAc;AACjC,SAQEC,YAAY,QAGP,UAAS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