Skip to content

Commit 97b28e1

Browse files
authored
fix: only generate ArrayOf helper when referenced (#40)
1 parent e898bf2 commit 97b28e1

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

src/commands/typegen/__tests__/__snapshots__/generate.test.ts.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,6 @@ export type AllSanitySchemaTypes =
276276
277277
export declare const internalGroqTypeReferenceTo: unique symbol
278278
279-
type ArrayOf<T> = Array<
280-
T & {
281-
_key: string
282-
}
283-
>
284-
285279
// Source: src/queries/authorQueries.ts
286280
// Variable: allAuthorsQuery
287281
// Query: *[_type == "author"]{ _id, name, slug}

src/typescript/__tests__/typeGenerator.test.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,6 @@ describe(TypeGenerator.name, () => {
183183
184184
export declare const internalGroqTypeReferenceTo: unique symbol;
185185
186-
type ArrayOf<T> = Array<T & {
187-
_key: string;
188-
}>;
189-
190186
// Source: foo.ts
191187
// Variable: queryFoo
192188
// Query: *[_type == "foo"]
@@ -292,10 +288,6 @@ describe(TypeGenerator.name, () => {
292288
293289
export declare const internalGroqTypeReferenceTo: unique symbol;
294290
295-
type ArrayOf<T> = Array<T & {
296-
_key: string;
297-
}>;
298-
299291
// Source: foo.ts
300292
// Variable: queryFoo
301293
// Query: *[_type == "foo"]
@@ -364,10 +356,6 @@ describe(TypeGenerator.name, () => {
364356
365357
export declare const internalGroqTypeReferenceTo: unique symbol;
366358
367-
type ArrayOf<T> = Array<T & {
368-
_key: string;
369-
}>;
370-
371359
"
372360
`)
373361
})
@@ -940,11 +928,32 @@ describe(TypeGenerator.name, () => {
940928
941929
export declare const internalGroqTypeReferenceTo: unique symbol;
942930
943-
type ArrayOf<T> = Array<T & {
944-
_key: string;
945-
}>;
946-
947931
"
948932
`)
949933
})
934+
935+
test('ArrayOf should NOT be generated when not used (no inline type references)', async () => {
936+
const schema: SchemaType = [
937+
{
938+
attributes: {
939+
_id: {type: 'objectAttribute', value: {type: 'string'}},
940+
_type: {type: 'objectAttribute', value: {type: 'string', value: 'post'}},
941+
title: {
942+
type: 'objectAttribute',
943+
value: {type: 'string'},
944+
},
945+
},
946+
name: 'post',
947+
type: 'document',
948+
},
949+
]
950+
951+
const typeGenerator = new TypeGenerator()
952+
const {code} = await typeGenerator.generateTypes({
953+
schema,
954+
})
955+
956+
// ArrayOf should NOT be in the output
957+
expect(code).not.toContain('ArrayOf')
958+
})
950959
})

src/typescript/schemaTypeGenerator.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export class SchemaTypeGenerator {
3434
return {stats, tsType}
3535
},
3636
)
37+
private arrayOfUsed = false
38+
3739
private identifiers = new Map<string, t.Identifier>()
3840

3941
private tsTypes = new Map<string, t.TSType>()
@@ -73,6 +75,10 @@ export class SchemaTypeGenerator {
7375
return this.tsTypes.has(typeName)
7476
}
7577

78+
isArrayOfUsed(): boolean {
79+
return this.arrayOfUsed
80+
}
81+
7682
*[Symbol.iterator]() {
7783
for (const {name} of this.schema) {
7884
yield {name, ...this.getType(name)!}
@@ -88,6 +94,7 @@ export class SchemaTypeGenerator {
8894
* wrapped in the ArrayOf wrapper that adds _key prop
8995
*/
9096
private generateArrayOfTsType(typeNode: ArrayTypeNode): t.TSTypeReference {
97+
this.arrayOfUsed = true
9198
const typeNodes = this.generateTsType(typeNode.of)
9299
return t.tsTypeReference(ARRAY_OF, t.tsTypeParameterInstantiation([typeNodes]))
93100
}

src/typescript/typeGenerator.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export class TypeGenerator {
274274
async generateTypes(options: GenerateTypesOptions) {
275275
const {reporter: report} = options
276276
const internalReferenceSymbol = this.getInternalReferenceSymbolDeclaration()
277-
const arrayOfDeclaration = this.getArrayOfDeclaration()
277+
const schemaTypeGenerator = this.getSchemaTypeGenerator(options)
278278
const schemaTypeDeclarations = this.getSchemaTypeDeclarations(options)
279279
const allSanitySchemaTypesDeclaration = this.getAllSanitySchemaTypesDeclaration(options)
280280

@@ -298,14 +298,19 @@ export class TypeGenerator {
298298
program.body.push(internalReferenceSymbol.ast)
299299
code += internalReferenceSymbol.code
300300

301-
program.body.push(arrayOfDeclaration.ast)
302-
code += arrayOfDeclaration.code
303-
304301
const evaluatedModules = await TypeGenerator.getEvaluatedModules({
305302
...options,
306303
schemaTypeDeclarations,
307-
schemaTypeGenerator: this.getSchemaTypeGenerator(options),
304+
schemaTypeGenerator,
308305
})
306+
307+
// Only generate ArrayOf if it's actually used
308+
if (schemaTypeGenerator.isArrayOfUsed()) {
309+
const arrayOfDeclaration = this.getArrayOfDeclaration()
310+
program.body.push(arrayOfDeclaration.ast)
311+
code += arrayOfDeclaration.code
312+
}
313+
309314
for (const {queries} of evaluatedModules) {
310315
for (const query of queries) {
311316
program.body.push(query.ast)

0 commit comments

Comments
 (0)