From 864a1c3daa96e9f7ad5abfe66b6a82ac0fab3fa7 Mon Sep 17 00:00:00 2001 From: Lars-Ivar Giskegjerde Date: Tue, 7 May 2024 23:31:26 +0200 Subject: [PATCH] Feat(typegen): Allow @sanity-ignore-typegen in any leading comment --- .../__tests__/findQueriesInSource.test.ts | 13 +++++++ .../src/typescript/findQueriesInSource.ts | 34 +++++++++++-------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts index 62d1289bc401..33a25f36500d 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts +++ b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts @@ -143,4 +143,17 @@ describe('findQueries', () => { const queries = findQueriesInSource(source, __filename, undefined) expect(queries.length).toBe(0) }) + + test('will ignore declerations if any of the leading comments are ignore tags', () => { + const source = ` + import { groq } from "groq"; + + // @sanity-typegen-ignore + // This should be ignored because of the comment above + export const postQuery = groq\`*[_type == "foo"]\` + ` + + const queries = findQueriesInSource(source, __filename, undefined) + expect(queries.length).toBe(0) + }) }) diff --git a/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts b/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts index 2a48fedc8dc5..089f4318403d 100644 --- a/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts +++ b/packages/@sanity/codegen/src/typescript/findQueriesInSource.ts @@ -47,7 +47,9 @@ export function findQueriesInSource( babelTypes.isIdentifier(node.id) && init.tag.name === groqTagName ) { - if (getDeclarationLeadingComment(path)?.trim() === ignoreValue) { + // If we find a comment leading the decleration which macthes with ignoreValue we don't add + // the query + if (declarationLeadingCommentContains(path, ignoreValue)) { return } @@ -69,9 +71,7 @@ export function findQueriesInSource( return queries } -function getDeclarationLeadingComment( - path: NodePath, -): string | null { +function declarationLeadingCommentContains(path: NodePath, comment: string): boolean { /* * We have to consider these cases: * @@ -120,20 +120,24 @@ function getDeclarationLeadingComment( */ const variableDeclaration = path.find((node) => node.isVariableDeclaration()) - if (!variableDeclaration) return null - - if (variableDeclaration.node.leadingComments) { - return getLastInArray(variableDeclaration.node.leadingComments)?.value || null + if (!variableDeclaration) return false + + if ( + variableDeclaration.node.leadingComments?.find( + (commentItem) => commentItem.value.trim() === comment, + ) + ) { + return true } // If the declaration is exported, the comment lies on the parent of the export declaration - if (variableDeclaration.parent.leadingComments) { - return getLastInArray(variableDeclaration.parent.leadingComments)?.value || null + if ( + variableDeclaration.parent.leadingComments?.find( + (commentItem) => commentItem.value.trim() === comment, + ) + ) { + return true } - return null -} - -function getLastInArray(arr: T[]) { - return arr[arr.length - 1] + return false }