-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): add groq finder methods. (#5980)
Adds methods that parses a js/ts source file and returns all groq queries. Co-authored-by: Tonina Zhelyazkova <[email protected]>
- Loading branch information
1 parent
e94c02f
commit 7addeef
Showing
14 changed files
with
804 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"presets": [ | ||
["@babel/preset-env", {"targets": "maintained node versions"}], | ||
[ | ||
"@babel/preset-react", | ||
{ | ||
"runtime": "automatic" | ||
} | ||
], | ||
"@babel/preset-typescript" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const TODO = 1 | ||
export {findQueriesInSource} from '../typescript/findQueriesInSource' | ||
export {getResolver} from '../typescript/moduleResolver' | ||
export {registerBabel} from '../typescript/registerBabel' |
113 changes: 113 additions & 0 deletions
113
packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import path from 'node:path' | ||
|
||
import {describe, expect, test} from '@jest/globals' | ||
|
||
import {findQueriesInSource} from '../findQueriesInSource' | ||
|
||
describe('findQueries', () => { | ||
describe('should find queries in source', () => { | ||
test('plain string', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
const postQuery = groq\`*[_type == "author"]\` | ||
const res = sanity.fetch(postQuery); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries[0] | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "author"]') | ||
}) | ||
|
||
test('with variables', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
const type = "author"; | ||
const authorQuery = groq\`*[_type == "\${type}"]\` | ||
const res = sanity.fetch(authorQuery); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries[0] | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "author"]') | ||
}) | ||
|
||
test('with function', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
const getType = () => () => () => "author"; | ||
const query = groq\`*[_type == "\${getType()()()}"]\` | ||
const res = sanity.fetch(query); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
|
||
const queryResult = queries[0] | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "author"]') | ||
}) | ||
|
||
test('with block comment', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
const type = "author"; | ||
const query = /* groq */ groq\`*[_type == "\${type}"]\`; | ||
const res = sanity.fetch(query); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries[0] | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "author"]') | ||
}) | ||
}) | ||
|
||
test('should not find inline queries in source', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
const res = sanity.fetch(groq\`*[_type == "author"]\`); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
|
||
expect(queries.length).toBe(0) | ||
}) | ||
|
||
test("should name queries with 'Result' at the end", () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
const postQuery = groq\`*[_type == "author"]\` | ||
const res = sanity.fetch(postQueryResult); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries[0] | ||
|
||
expect(queryResult?.name.substr(-6)).toBe('Result') | ||
}) | ||
|
||
test('should import', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
import {foo} from "./fixtures/exportVar"; | ||
const postQuery = groq\`*[_type == "\${foo}"]\` | ||
const res = sanity.fetch(postQueryResult); | ||
` | ||
|
||
const resolver: NodeJS.RequireResolve = (id) => { | ||
if (id === 'foo') { | ||
return path.resolve(__dirname, 'fixtures', 'exportVar') | ||
} | ||
return require.resolve(id) | ||
} | ||
resolver.paths = (request: string): string[] | null => { | ||
return require.resolve.paths(request) | ||
} | ||
|
||
const queries = findQueriesInSource(source, 'test.ts', undefined, resolver) | ||
const queryResult = queries[0] | ||
|
||
expect(queryResult?.name.substr(-6)).toBe('Result') | ||
}) | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/@sanity/codegen/src/typescript/__tests__/fixtures/exportStar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './exportVar' |
1 change: 1 addition & 0 deletions
1
packages/@sanity/codegen/src/typescript/__tests__/fixtures/exportVar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo = 'foo' |
3 changes: 3 additions & 0 deletions
3
packages/@sanity/codegen/src/typescript/__tests__/fixtures/source1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import groq from 'groq' | ||
|
||
export const postQuery = groq`*[_type == "author"]` |
3 changes: 3 additions & 0 deletions
3
packages/@sanity/codegen/src/typescript/__tests__/fixtures/source2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import groq from 'groq' | ||
|
||
const postQuery = groq`*[_type == "author"]` |
Oops, something went wrong.