From 3cf3d62286261ab917766c27aa5c6a4e3ecb7d0a Mon Sep 17 00:00:00 2001 From: Melissa <93585768+fantastiskm@users.noreply.github.com> Date: Thu, 29 Feb 2024 09:25:36 +0100 Subject: [PATCH 1/4] Global Identifiers are forced lower case Global identifiers are converted to lower case before being used in this file. This patch corrects the comparison function to so they match in the file if the source identifier is not all lower case --- packages/graphql-tag-pluck/src/visitor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/graphql-tag-pluck/src/visitor.ts b/packages/graphql-tag-pluck/src/visitor.ts index 111c0743c1b..351f49502b3 100644 --- a/packages/graphql-tag-pluck/src/visitor.ts +++ b/packages/graphql-tag-pluck/src/visitor.ts @@ -225,7 +225,7 @@ export default (code: string, out: any, options: GraphQLTagPluckOptions = {}) => // Check if identifier is defined and imported from registered packages function isValidIdentifier(name: string) { return ( - definedIdentifierNames.some(id => id === name) || globalGqlIdentifierName!.includes(name) + definedIdentifierNames.some(id => id === name) || globalGqlIdentifierName!.includes(name.toLowerCase()) ); } From 2ea922b11f328722b07ed1be39ace51a85f8aa69 Mon Sep 17 00:00:00 2001 From: Melissa Date: Thu, 29 Feb 2024 09:55:30 +0100 Subject: [PATCH 2/4] Prettier & Tests --- packages/graphql-tag-pluck/src/visitor.ts | 3 +- .../tests/graphql-tag-pluck.test.ts | 41 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/graphql-tag-pluck/src/visitor.ts b/packages/graphql-tag-pluck/src/visitor.ts index 351f49502b3..93c15d8143c 100644 --- a/packages/graphql-tag-pluck/src/visitor.ts +++ b/packages/graphql-tag-pluck/src/visitor.ts @@ -225,7 +225,8 @@ export default (code: string, out: any, options: GraphQLTagPluckOptions = {}) => // Check if identifier is defined and imported from registered packages function isValidIdentifier(name: string) { return ( - definedIdentifierNames.some(id => id === name) || globalGqlIdentifierName!.includes(name.toLowerCase()) + definedIdentifierNames.some(id => id === name) || + globalGqlIdentifierName!.includes(name.toLowerCase()) ); } diff --git a/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts b/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts index b25c99a7152..65f53869708 100644 --- a/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts +++ b/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts @@ -268,7 +268,7 @@ describe('graphql-tag-pluck', () => { ); }); - it.only("should pluck graphql-tag template literals from .ts that use 'using' keyword", async () => { + it("should pluck graphql-tag template literals from .ts that use 'using' keyword", async () => { const sources = await pluck( 'tmp-XXXXXX.ts', freeText(` @@ -1943,6 +1943,45 @@ describe('graphql-tag-pluck', () => { globalGqlIdentifierName: 'anothergql', }, ); + expect(sources.map(source => source.body).join('\n\n')).toEqual( + freeText(` + fragment Foo on FooType { + id + } + + query foo { + foo { + ...Foo + } + } + `), + ); + }); + + it('should be able to specify the global GraphQL identifier name case insensitively', async () => { + const sources = await pluck( + 'tmp-XXXXXX.js', + freeText(` + const fragment = anotherGql(\` + fragment Foo on FooType { + id + } + \`) + + const doc = AnotherGql\` + query foo { + foo { + ...Foo + } + } + + \${fragment} + \` + `), + { + globalGqlIdentifierName: 'anothergql', + }, + ); expect(sources.map(source => source.body).join('\n\n')).toEqual( freeText(` From 65ce7cedfeab67148806d6ff74c2f3f13db4bdcc Mon Sep 17 00:00:00 2001 From: Melissa Date: Thu, 29 Feb 2024 10:34:53 +0100 Subject: [PATCH 3/4] Update graphql-tag-pluck.mdx --- website/src/pages/docs/graphql-tag-pluck.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/docs/graphql-tag-pluck.mdx b/website/src/pages/docs/graphql-tag-pluck.mdx index c389672ba2b..38690daf7da 100644 --- a/website/src/pages/docs/graphql-tag-pluck.mdx +++ b/website/src/pages/docs/graphql-tag-pluck.mdx @@ -103,7 +103,7 @@ be translated into `/* GraphQL */` in code. ### `globalGqlIdentifierName` -Allows using a global identifier instead of a module import. +Allows using a global identifier instead of a module import. Identifiers are case insensitive. ```js // `graphql` is a global function From 54026040bfb001d137a716403dbf8a76d2430408 Mon Sep 17 00:00:00 2001 From: Melissa Date: Thu, 29 Feb 2024 14:32:23 +0100 Subject: [PATCH 4/4] Change to be case sensitive - don't lower case any globalGqlIdentifierNames --- packages/graphql-tag-pluck/src/visitor.ts | 5 ++--- .../tests/graphql-tag-pluck.test.ts | 13 +++---------- website/src/pages/docs/graphql-tag-pluck.mdx | 2 +- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/packages/graphql-tag-pluck/src/visitor.ts b/packages/graphql-tag-pluck/src/visitor.ts index 93c15d8143c..35667f300e2 100644 --- a/packages/graphql-tag-pluck/src/visitor.ts +++ b/packages/graphql-tag-pluck/src/visitor.ts @@ -203,7 +203,7 @@ export default (code: string, out: any, options: GraphQLTagPluckOptions = {}) => identifier: mod.identifier && mod.identifier.toLowerCase(), }; }); - globalGqlIdentifierName = asArray(globalGqlIdentifierName).map(s => s!.toLowerCase()); + globalGqlIdentifierName = asArray(globalGqlIdentifierName ?? ''); const hooksOptions = { skipIndent, gqlMagicComment, modules, globalGqlIdentifierName }; @@ -225,8 +225,7 @@ export default (code: string, out: any, options: GraphQLTagPluckOptions = {}) => // Check if identifier is defined and imported from registered packages function isValidIdentifier(name: string) { return ( - definedIdentifierNames.some(id => id === name) || - globalGqlIdentifierName!.includes(name.toLowerCase()) + definedIdentifierNames.some(id => id === name) || globalGqlIdentifierName!.includes(name) ); } diff --git a/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts b/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts index 65f53869708..4fef8393070 100644 --- a/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts +++ b/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts @@ -1958,7 +1958,7 @@ describe('graphql-tag-pluck', () => { ); }); - it('should be able to specify the global GraphQL identifier name case insensitively', async () => { + it('should be able to specify the global GraphQL identifier name case sensitively', async () => { const sources = await pluck( 'tmp-XXXXXX.js', freeText(` @@ -1979,7 +1979,7 @@ describe('graphql-tag-pluck', () => { \` `), { - globalGqlIdentifierName: 'anothergql', + globalGqlIdentifierName: 'anotherGql', }, ); @@ -1987,14 +1987,7 @@ describe('graphql-tag-pluck', () => { freeText(` fragment Foo on FooType { id - } - - query foo { - foo { - ...Foo - } - } - `), + }`), ); }); diff --git a/website/src/pages/docs/graphql-tag-pluck.mdx b/website/src/pages/docs/graphql-tag-pluck.mdx index 38690daf7da..2c1e2423f5c 100644 --- a/website/src/pages/docs/graphql-tag-pluck.mdx +++ b/website/src/pages/docs/graphql-tag-pluck.mdx @@ -103,7 +103,7 @@ be translated into `/* GraphQL */` in code. ### `globalGqlIdentifierName` -Allows using a global identifier instead of a module import. Identifiers are case insensitive. +Allows using a global identifier instead of a module import. Identifiers are case sensitive. ```js // `graphql` is a global function