Skip to content

Commit

Permalink
chore(typegen): track empty union type nodes generated (#6495)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed Apr 29, 2024
1 parent 21d5b32 commit 124be88
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface TypesGeneratedTraceAttrubutes {
typeNodesGenerated: number
unknownTypeNodesGenerated: number
unknownTypeNodesRatio: number
emptyUnionTypeNodesGenerated: number
}

export const TypesGeneratedTrace = defineTrace<TypesGeneratedTraceAttrubutes>({
Expand Down
4 changes: 4 additions & 0 deletions packages/@sanity/cli/src/actions/typegen/generateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default async function typegenGenerateAction(
schemaTypesCount: 0,
unknownTypeNodesGenerated: 0,
typeNodesGenerated: 0,
emptyUnionTypeNodesGenerated: 0,
size: 0,
}

Expand Down Expand Up @@ -135,13 +136,15 @@ export default async function typegenGenerateAction(
type,
typeNodesGenerated,
unknownTypeNodesGenerated,
emptyUnionTypeNodesGenerated,
} of msg.types) {
fileTypeString += `// Variable: ${queryName}\n`
fileTypeString += `// Query: ${query.replace(/(\r\n|\n|\r)/gm, '')}\n`
fileTypeString += `${type}\n`
stats.queriesCount++
stats.typeNodesGenerated += typeNodesGenerated
stats.unknownTypeNodesGenerated += unknownTypeNodesGenerated
stats.emptyUnionTypeNodesGenerated += emptyUnionTypeNodesGenerated
}
typeFile.write(`${fileTypeString}\n`)
stats.size += Buffer.byteLength(fileTypeString)
Expand All @@ -161,6 +164,7 @@ export default async function typegenGenerateAction(
unknownTypeNodesGenerated: stats.unknownTypeNodesGenerated,
unknownTypeNodesRatio:
stats.typeNodesGenerated > 0 ? stats.unknownTypeNodesGenerated / stats.typeNodesGenerated : 0,
emptyUnionTypeNodesGenerated: stats.emptyUnionTypeNodesGenerated,
})

trace.complete()
Expand Down
36 changes: 27 additions & 9 deletions packages/@sanity/cli/src/workers/typegenGenerate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type TypegenGenerateTypesWorkerMessage =
type: string
unknownTypeNodesGenerated: number
typeNodesGenerated: number
emptyUnionTypeNodesGenerated: number
}[]
}
| {
Expand Down Expand Up @@ -116,6 +117,7 @@ async function main() {
type: string
unknownTypeNodesGenerated: number
typeNodesGenerated: number
emptyUnionTypeNodesGenerated: number
}[] = []
for (const {name: queryName, result: query} of result.queries) {
try {
Expand All @@ -134,6 +136,7 @@ async function main() {
type,
unknownTypeNodesGenerated: queryTypeStats.unknownTypes,
typeNodesGenerated: queryTypeStats.allTypes,
emptyUnionTypeNodesGenerated: queryTypeStats.emptyUnions,
})
} catch (err) {
parentPort?.postMessage({
Expand Down Expand Up @@ -166,10 +169,11 @@ async function main() {
function walkAndCountQueryTypeNodeStats(typeNode: TypeNode): {
allTypes: number
unknownTypes: number
emptyUnions: number
} {
switch (typeNode.type) {
case 'unknown': {
return {allTypes: 1, unknownTypes: 1}
return {allTypes: 1, unknownTypes: 1, emptyUnions: 0}
}
case 'array': {
const acc = walkAndCountQueryTypeNodeStats(typeNode.of)
Expand All @@ -179,29 +183,43 @@ function walkAndCountQueryTypeNodeStats(typeNode: TypeNode): {
case 'object': {
// if the rest is unknown, we count it as one unknown type
if (typeNode.rest && typeNode.rest.type === 'unknown') {
return {allTypes: 2, unknownTypes: 1} // count the object type itself as well
return {allTypes: 2, unknownTypes: 1, emptyUnions: 0} // count the object type itself as well
}

const restStats = typeNode.rest
? walkAndCountQueryTypeNodeStats(typeNode.rest)
: {allTypes: 1, unknownTypes: 0} // count the object type itself
: {allTypes: 1, unknownTypes: 0, emptyUnions: 0} // count the object type itself

return Object.values(typeNode.attributes).reduce((acc, attribute) => {
const {allTypes, unknownTypes} = walkAndCountQueryTypeNodeStats(attribute.value)
return {allTypes: acc.allTypes + allTypes, unknownTypes: acc.unknownTypes + unknownTypes}
const {allTypes, unknownTypes, emptyUnions} = walkAndCountQueryTypeNodeStats(
attribute.value,
)
return {
allTypes: acc.allTypes + allTypes,
unknownTypes: acc.unknownTypes + unknownTypes,
emptyUnions: acc.emptyUnions + emptyUnions,
}
}, restStats)
}
case 'union': {
if (typeNode.of.length === 0) {
return {allTypes: 1, unknownTypes: 0, emptyUnions: 1}
}

return typeNode.of.reduce(
(acc, type) => {
const {allTypes, unknownTypes} = walkAndCountQueryTypeNodeStats(type)
return {allTypes: acc.allTypes + allTypes, unknownTypes: acc.unknownTypes + unknownTypes}
const {allTypes, unknownTypes, emptyUnions} = walkAndCountQueryTypeNodeStats(type)
return {
allTypes: acc.allTypes + allTypes,
unknownTypes: acc.unknownTypes + unknownTypes,
emptyUnions: acc.emptyUnions + emptyUnions,
}
},
{allTypes: 1, unknownTypes: 0}, // count the union type itself
{allTypes: 1, unknownTypes: 0, emptyUnions: 0}, // count the union type itself
)
}
default: {
return {allTypes: 1, unknownTypes: 0}
return {allTypes: 1, unknownTypes: 0, emptyUnions: 0}
}
}
}
Expand Down

0 comments on commit 124be88

Please sign in to comment.