Skip to content

Commit

Permalink
fix(codegen): uppercase query type names (#6080)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth authored Mar 20, 2024
1 parent 2e06b84 commit 7402093
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
Expand All @@ -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;"`;
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
Expand Down
10 changes: 4 additions & 6 deletions packages/@sanity/codegen/src/typescript/typeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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
Expand Down

0 comments on commit 7402093

Please sign in to comment.