From 74020938c9cbea29f9ef20ff70b4a27ad2e420a9 Mon Sep 17 00:00:00 2001 From: Sindre Gulseth Date: Wed, 20 Mar 2024 13:49:51 +0100 Subject: [PATCH] fix(codegen): uppercase query type names (#6080) --- .../__snapshots__/typeGenerator.test.ts.snap | 14 +++++++------- .../src/typescript/__tests__/typeGenerator.test.ts | 2 +- .../codegen/src/typescript/typeGenerator.ts | 10 ++++------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap b/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap index 2fa9289ae2f..2bea548cf76 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap +++ b/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap @@ -2,15 +2,15 @@ exports[`generateSchemaTypes can generate well known types 1`] = `"export declare const internalGroqTypeReferenceTo: unique symbol;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: boolean 1`] = `"export type test_2 = boolean;"`; +exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: boolean 1`] = `"export type Test_2 = boolean;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: null 1`] = `"export type test_5 = null;"`; +exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: null 1`] = `"export type Test_5 = null;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: number 1`] = `"export type test_3 = number;"`; +exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: number 1`] = `"export type Test_3 = number;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: string 1`] = `"export type test = string;"`; +exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: string 1`] = `"export type Test = string;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: unknown 1`] = `"export type test_4 = unknown;"`; +exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: unknown 1`] = `"export type Test_4 = unknown;"`; exports[`generateSchemaTypes should generate TypeScript type declarations for a schema 1`] = ` "export type Author = { @@ -212,7 +212,7 @@ export type SanityImagePaletteSwatch = { `; exports[`generateSchemaTypes should generate correct types for document schema with inline fields 1`] = ` -"export type myObject = { +"export type MyObject = { inlineField: { test: string; } & Test; @@ -224,4 +224,4 @@ exports[`generateSchemaTypes should generate correct types for document schema w };" `; -exports[`generateSchemaTypes should generate correct types for document schema with inline fields 2`] = `"export type someOtherType = MyObject;"`; +exports[`generateSchemaTypes should generate correct types for document schema with inline fields 2`] = `"export type SomeOtherType = MyObject;"`; diff --git a/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts b/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts index 49a66fd73f9..f05e7ad9087 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts +++ b/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts @@ -399,7 +399,7 @@ export type Author = { const objectNodeOut = typeGenerator.generateTypeNodeTypes('myObject', objectNode) expect(objectNodeOut).toMatchSnapshot() - const someOtherTypeOut = typeGenerator.generateTypeNodeTypes('someOtherType', { + const someOtherTypeOut = typeGenerator.generateTypeNodeTypes('SomeOtherType', { type: 'inline', name: 'myObject', }) diff --git a/packages/@sanity/codegen/src/typescript/typeGenerator.ts b/packages/@sanity/codegen/src/typescript/typeGenerator.ts index cc70b42f5d6..4267ab3b66a 100644 --- a/packages/@sanity/codegen/src/typescript/typeGenerator.ts +++ b/packages/@sanity/codegen/src/typescript/typeGenerator.ts @@ -41,7 +41,7 @@ export class TypeGenerator { const typeLiteral = this.getTypeNodeType(schema) const typeAlias = t.tsTypeAliasDeclaration( - t.identifier(this.getTypeName(schema.name, true)), + t.identifier(this.getTypeName(schema.name)), null, typeLiteral, ) @@ -65,7 +65,7 @@ export class TypeGenerator { const type = this.getTypeNodeType(typeNode) const typeAlias = t.tsTypeAliasDeclaration( - t.identifier(this.getTypeName(identifierName, false)), + t.identifier(this.getTypeName(identifierName)), null, type, ) @@ -90,10 +90,8 @@ export class TypeGenerator { * types would be sanityized into MuxVideo. To avoid this we keep track of the generated type names and add a index to the name. * When we reference a type we also keep track of the original name so we can reference the correct type later. */ - private getTypeName(name: string, shouldUppercaseFirstLetter: boolean): string { - const desiredName = shouldUppercaseFirstLetter - ? uppercaseFirstLetter(sanitizeIdentifier(name)) - : sanitizeIdentifier(name) + private getTypeName(name: string): string { + const desiredName = uppercaseFirstLetter(sanitizeIdentifier(name)) let generatedName = desiredName let i = 2