Skip to content

Commit

Permalink
fix(typegen): move type new line separator into formatter (#6649)
Browse files Browse the repository at this point in the history
* fix(typegen): move type new line separator into formatter

* fix(typegen): remove double linebreak between schema and query types
  • Loading branch information
sgulseth authored and ricokahler committed May 14, 2024
1 parent 52a5308 commit 109a180
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
6 changes: 3 additions & 3 deletions packages/@sanity/cli/src/actions/typegen/generateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default async function typegenGenerateAction(

if (msg.type === 'schema') {
stats.schemaTypesCount += msg.length
fileTypeString += `${msg.schema}\n\n`
fileTypeString += msg.schema
typeFile.write(fileTypeString)
return
}
Expand All @@ -140,13 +140,13 @@ export default async function typegenGenerateAction(
} of msg.types) {
fileTypeString += `// Variable: ${queryName}\n`
fileTypeString += `// Query: ${query.replace(/(\r\n|\n|\r)/gm, '')}\n`
fileTypeString += `${type}\n`
fileTypeString += type
stats.queriesCount++
stats.typeNodesGenerated += typeNodesGenerated
stats.unknownTypeNodesGenerated += unknownTypeNodesGenerated
stats.emptyUnionTypeNodesGenerated += emptyUnionTypeNodesGenerated
}
typeFile.write(`${fileTypeString}\n`)
typeFile.write(fileTypeString)
stats.size += Buffer.byteLength(fileTypeString)
})
worker.addListener('error', reject)
Expand Down
6 changes: 3 additions & 3 deletions packages/@sanity/cli/src/workers/typegenGenerate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ registerBabel()

function maybeFormatCode(code: string, prettierConfig: PrettierOptions | null): Promise<string> {
if (!prettierConfig) {
return Promise.resolve(code)
return Promise.resolve(`${code}\n`) // add an extra new newline, poor mans formatting
}

try {
Expand All @@ -82,7 +82,7 @@ async function main() {

const typeGenerator = new TypeGenerator(schema)
const schemaTypes = await maybeFormatCode(
[typeGenerator.generateSchemaTypes(), TypeGenerator.generateKnownTypes()].join('\n'),
[typeGenerator.generateSchemaTypes(), TypeGenerator.generateKnownTypes()].join('\n').trim(),
opts.prettierConfig,
)
const resolver = getResolver()
Expand Down Expand Up @@ -125,7 +125,7 @@ async function main() {
const queryTypes = typeEvaluate(ast, schema)

const type = await maybeFormatCode(
typeGenerator.generateTypeNodeTypes(`${queryName}Result`, queryTypes),
typeGenerator.generateTypeNodeTypes(`${queryName}Result`, queryTypes).trim(),
opts.prettierConfig,
)

Expand Down
4 changes: 0 additions & 4 deletions packages/@sanity/cli/test/__snapshots__/typegen.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ export type Slug = {
source?: string;
};
export declare const internalGroqTypeReferenceTo: unique symbol;
// Source: ./src/queries.ts
// Variable: PAGE_QUERY
// Query: *[_type == \\"page\\" && slug.current == $slug][0]
export type PAGE_QUERYResult = null;
"
`;
4 changes: 2 additions & 2 deletions packages/@sanity/codegen/src/typescript/typeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class TypeGenerator {
type,
)

return new CodeGenerator(t.exportNamedDeclaration(typeAlias)).generate().code
return new CodeGenerator(t.exportNamedDeclaration(typeAlias)).generate().code.trim()
}

static generateKnownTypes(): string {
Expand All @@ -82,7 +82,7 @@ export class TypeGenerator {

const decleration = t.variableDeclaration('const', [t.variableDeclarator(identifier)])
decleration.declare = true
return new CodeGenerator(t.exportNamedDeclaration(decleration)).generate().code
return new CodeGenerator(t.exportNamedDeclaration(decleration)).generate().code.trim()
}

/**
Expand Down

0 comments on commit 109a180

Please sign in to comment.