-
Notifications
You must be signed in to change notification settings - Fork 427
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.
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
dff8b1b
commit f222a5c
Showing
12 changed files
with
1,036 additions
and
15 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
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' |
186 changes: 186 additions & 0 deletions
186
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,186 @@ | ||
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.get('postQueryResult') | ||
|
||
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.get('authorQueryResult') | ||
|
||
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.get('queryResult') | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "author"]') | ||
}) | ||
|
||
test('with function arg', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
function getType(type: string) { | ||
return type | ||
} | ||
const getArrowType = (type: string) => { | ||
return type | ||
} | ||
const query = groq\`*[_type == "\${getType("foo")}" || _type == "\${getArrowType("bar")}"]\` | ||
const res = () => sanity.fetch(query); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries.get('queryResult') | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "foo" || _type == "bar"]') | ||
}) | ||
|
||
test('with class methods', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
class Test { | ||
unused() { | ||
return 123; | ||
} | ||
getType() { | ||
return "author"; | ||
} | ||
static getId() { | ||
return "id" | ||
} | ||
static getFoo() { | ||
return "foo" | ||
} | ||
} | ||
const test = new Test(); | ||
const query = groq\`*[_type == "\${test.getType()}"]\` | ||
const res = sanity.fetch(query); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries.get('queryResult') | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "author"]') | ||
}) | ||
|
||
test('with direct class methods', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
class Test { | ||
getType() { | ||
return "author"; | ||
} | ||
static getId() { | ||
return "id" | ||
} | ||
static getFoo() { | ||
return "foo" | ||
} | ||
} | ||
const query = groq\`*[_type == "\${(new Test()).getType()}" && _id == "\${Test.getId()}"]\` | ||
const res = sanity.fetch(query); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries.get('queryResult') | ||
|
||
expect(queryResult?.result).toEqual('*[_type == "author" && _id == "id"]') | ||
}) | ||
|
||
test('with class static method', () => { | ||
const source = ` | ||
import { groq } from "groq"; | ||
class Test { | ||
static unused() { | ||
return "unused"; | ||
} | ||
static getType() { | ||
return "author"; | ||
} | ||
} | ||
const query = groq\`*[_type == "\${Test.getType()}"]\` | ||
const res = sanity.fetch(query); | ||
` | ||
|
||
const queries = findQueriesInSource(source, 'test.ts') | ||
const queryResult = queries.get('queryResult') | ||
|
||
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.get('queryResult') | ||
|
||
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.size).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.get('postQueryResult') | ||
|
||
expect(queryResult?.name.substr(-6)).toBe('Result') | ||
}) | ||
}) |
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"]` |
File renamed without changes.
Oops, something went wrong.