Skip to content

Commit

Permalink
feat(codegen): add groq finder methods.
Browse files Browse the repository at this point in the history
Adds methods that parses a js/ts source file and returns all groq queries.

co-authored-by: Tonina Zhelyazkova <[email protected]>
  • Loading branch information
sgulseth and tzhelyazkova committed Mar 12, 2024
1 parent 2715130 commit e133519
Show file tree
Hide file tree
Showing 12 changed files with 1,034 additions and 14 deletions.
14 changes: 14 additions & 0 deletions packages/@sanity/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,22 @@
"test": "jest"
},
"dependencies": {
"@babel/core": "^7.23.9",
"@babel/preset-env": "^7.23.8",
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@babel/register": "^7.23.7",
"@babel/traverse": "^7.23.5",
"@babel/types": "^7.23.9",
"debug": "^4.3.4"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/babel__core": "^7.20.5",
"@types/babel__register": "^7.17.3",
"@types/babel__traverse": "^7.18.1",
"@types/debug": "^4.1.12",
"groq": "workspace:*",
"rimraf": "^3.0.2"
},
"engines": {
Expand Down
4 changes: 3 additions & 1 deletion packages/@sanity/codegen/src/_exports/index.ts
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'
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')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import groq from 'groq'

export const postQuery = groq`*[_type == "author"]`
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.
Loading

0 comments on commit e133519

Please sign in to comment.