From 99888745f5138183b07af92fce1445ea241458b3 Mon Sep 17 00:00:00 2001 From: Sindre Gulseth Date: Tue, 6 Aug 2024 12:12:18 +0200 Subject: [PATCH] feat(typegen): add location of discovered query --- .../__tests__/findQueriesInSource.test.ts | 5 +++++ .../codegen/src/typescript/expressionResolvers.ts | 14 ++++++++++++++ .../codegen/src/typescript/findQueriesInSource.ts | 13 ++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts index a8acc198659d..e52df43ce648 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts +++ b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts @@ -14,7 +14,12 @@ describe('findQueries with the groq template', () => { const queries = findQueriesInSource(source, 'test.ts') const queryResult = queries[0] + expect(queryResult?.name).toEqual('postQuery') expect(queryResult?.result).toEqual('*[_type == "author"]') + expect(queryResult?.location).toStrictEqual({ + end: {line: 3, column: 50, index: 86}, + start: {line: 3, column: 12, index: 48}, + }) }) test('with variables', () => { diff --git a/packages/@sanity/codegen/src/typescript/expressionResolvers.ts b/packages/@sanity/codegen/src/typescript/expressionResolvers.ts index 1ecc25d47656..05ad68e7f7f3 100644 --- a/packages/@sanity/codegen/src/typescript/expressionResolvers.ts +++ b/packages/@sanity/codegen/src/typescript/expressionResolvers.ts @@ -20,6 +20,20 @@ export interface NamedQueryResult { name: string /** result is a groq query */ result: resolveExpressionReturnType + + /** location is the location of the query in the source */ + location: { + start?: { + line: number + column: number + index: number + } + end?: { + line: number + column: number + index: number + } + } } const TAGGED_TEMPLATE_ALLOW_LIST = ['groq'] diff --git a/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts b/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts index 1f0a3d7030c1..b7648c0ad754 100644 --- a/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts +++ b/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts @@ -71,7 +71,18 @@ export function findQueriesInSource( resolver, }) - queries.push({name: queryName, result: queryResult}) + const location = node.loc + ? { + start: { + ...node.loc?.start, + }, + end: { + ...node.loc?.end, + }, + } + : {} + + queries.push({name: queryName, result: queryResult, location}) } }, })