1+ import { combineComments } from './comments' ;
12import { isPrimitive , type Primitive , type Schema } from './ir' ;
23
34export type OptimizeFn = ( schema : Schema ) => Schema ;
@@ -167,12 +168,21 @@ export function filterUndefinedUnion(schema: Schema): [boolean, Schema] {
167168 if ( schemas . length === 0 ) {
168169 return [ true , { type : 'undefined' } ] ;
169170 } else if ( schemas . length === 1 ) {
170- return [ true , schemas [ 0 ] ! ] ;
171+ return [ true , withComment ( schemas [ 0 ] ! , schema ) ] ;
171172 } else {
172- return [ true , { type : 'union' , schemas } ] ;
173+ return [ true , withComment ( { type : 'union' , schemas } , schema ) ] ;
173174 }
174175}
175176
177+ // This function is a helper that adds back any comments that were removed during optimization
178+ function withComment ( newSchema : Schema , oldSchema : Schema ) : Schema {
179+ if ( oldSchema . comment ) {
180+ newSchema . comment = combineComments ( oldSchema ) ;
181+ }
182+
183+ return newSchema ;
184+ }
185+
176186export function optimize ( schema : Schema ) : Schema {
177187 if ( schema . type === 'object' ) {
178188 const properties : Record < string , Schema > = { } ;
@@ -183,11 +193,6 @@ export function optimize(schema: Schema): Schema {
183193 continue ;
184194 }
185195 const [ isOptional , filteredSchema ] = filterUndefinedUnion ( optimized ) ;
186-
187- if ( prop . comment ) {
188- filteredSchema . comment = prop . comment ;
189- }
190-
191196 properties [ key ] = filteredSchema ;
192197
193198 if ( schema . required . indexOf ( key ) >= 0 && ! isOptional ) {
@@ -197,48 +202,34 @@ export function optimize(schema: Schema): Schema {
197202
198203 const schemaObject : Schema = { type : 'object' , properties, required } ;
199204
200- // only add comment field if there is a comment
201- if ( schema . comment ) {
202- return { ...schemaObject , comment : schema . comment } ;
203- }
204-
205- return schemaObject ;
205+ return withComment ( schemaObject , schema ) ;
206206 } else if ( schema . type === 'intersection' ) {
207207 const newSchema = foldIntersection ( schema , optimize ) ;
208- if ( schema . comment ) {
209- return { ...newSchema , comment : schema . comment } ;
210- }
211- return newSchema ;
208+
209+ return withComment ( newSchema , schema ) ;
212210 } else if ( schema . type === 'union' ) {
213211 const consolidated = consolidateUnion ( schema ) ;
214212 const simplified = simplifyUnion ( consolidated , optimize ) ;
215213 const merged = mergeUnions ( simplified ) ;
216214
217- if ( schema . comment ) {
218- return { ...merged , comment : schema . comment } ;
219- }
220-
221- return merged ;
215+ return withComment ( merged , schema ) ;
222216 } else if ( schema . type === 'array' ) {
223217 const optimized = optimize ( schema . items ) ;
224- if ( schema . comment ) {
225- return { type : 'array' , items : optimized , comment : schema . comment } ;
226- }
227- return { type : 'array' , items : optimized } ;
218+
219+ return withComment ( { type : 'array' , items : optimized } , schema ) ;
228220 } else if ( schema . type === 'record' ) {
229- return {
230- type : 'record' ,
231- ...( schema . domain ? { domain : optimize ( schema . domain ) } : { } ) ,
232- codomain : optimize ( schema . codomain ) ,
233- ...( schema . comment ? { comment : schema . comment } : { } ) ,
234- } ;
221+ return withComment (
222+ {
223+ type : 'record' ,
224+ ...( schema . domain ? { domain : optimize ( schema . domain ) } : { } ) ,
225+ codomain : optimize ( schema . codomain ) ,
226+ } ,
227+ schema ,
228+ ) ;
235229 } else if ( schema . type === 'tuple' ) {
236230 const schemas = schema . schemas . map ( optimize ) ;
237- return { type : 'tuple' , schemas } ;
231+ return withComment ( { type : 'tuple' , schemas } , schema ) ;
238232 } else if ( schema . type === 'ref' ) {
239- if ( schema . comment ) {
240- return { ...schema , comment : schema . comment } ;
241- }
242233 return schema ;
243234 } else {
244235 return schema ;
0 commit comments