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 19, 2024
1 parent f6fc884 commit 0d83227
Show file tree
Hide file tree
Showing 14 changed files with 804 additions and 17 deletions.
12 changes: 12 additions & 0 deletions packages/@sanity/codegen/babel.config.json
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"
]
}
18 changes: 17 additions & 1 deletion packages/@sanity/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,24 @@
"watch": "pkg-utils watch --tsconfig tsconfig.lib.json",
"test": "jest"
},
"dependencies": {},
"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",
"tsconfig-paths": "^4.2.0"
},
"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,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')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './exportVar'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo'
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"]`
Loading

0 comments on commit 0d83227

Please sign in to comment.