Skip to content

Commit

Permalink
feat(typegen): add location of discovered query (#7327)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth authored and cngonzalez committed Aug 20, 2024
1 parent 662f950 commit 60b4000
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -29,6 +34,10 @@ describe('findQueries with the groq template', () => {
const queryResult = queries[0]

expect(queryResult?.result).toEqual('*[_type == "author"]')
expect(queryResult?.location).toStrictEqual({
end: {line: 4, column: 53, index: 118},
start: {line: 4, column: 12, index: 77},
})
})

test('with function', () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/@sanity/codegen/src/typescript/expressionResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
13 changes: 12 additions & 1 deletion packages/@sanity/codegen/src/typescript/findQueriesInSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
},
})
Expand Down

0 comments on commit 60b4000

Please sign in to comment.