1
+ import { combineComments } from './comments' ;
1
2
import { isPrimitive , type Primitive , type Schema } from './ir' ;
2
3
3
4
export type OptimizeFn = ( schema : Schema ) => Schema ;
@@ -167,12 +168,21 @@ export function filterUndefinedUnion(schema: Schema): [boolean, Schema] {
167
168
if ( schemas . length === 0 ) {
168
169
return [ true , { type : 'undefined' } ] ;
169
170
} else if ( schemas . length === 1 ) {
170
- return [ true , schemas [ 0 ] ! ] ;
171
+ return [ true , withComment ( schemas [ 0 ] ! , schema ) ] ;
171
172
} else {
172
- return [ true , { type : 'union' , schemas } ] ;
173
+ return [ true , withComment ( { type : 'union' , schemas } , schema ) ] ;
173
174
}
174
175
}
175
176
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
+
176
186
export function optimize ( schema : Schema ) : Schema {
177
187
if ( schema . type === 'object' ) {
178
188
const properties : Record < string , Schema > = { } ;
@@ -183,11 +193,6 @@ export function optimize(schema: Schema): Schema {
183
193
continue ;
184
194
}
185
195
const [ isOptional , filteredSchema ] = filterUndefinedUnion ( optimized ) ;
186
-
187
- if ( prop . comment ) {
188
- filteredSchema . comment = prop . comment ;
189
- }
190
-
191
196
properties [ key ] = filteredSchema ;
192
197
193
198
if ( schema . required . indexOf ( key ) >= 0 && ! isOptional ) {
@@ -197,48 +202,34 @@ export function optimize(schema: Schema): Schema {
197
202
198
203
const schemaObject : Schema = { type : 'object' , properties, required } ;
199
204
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 ) ;
206
206
} else if ( schema . type === 'intersection' ) {
207
207
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 ) ;
212
210
} else if ( schema . type === 'union' ) {
213
211
const consolidated = consolidateUnion ( schema ) ;
214
212
const simplified = simplifyUnion ( consolidated , optimize ) ;
215
213
const merged = mergeUnions ( simplified ) ;
216
214
217
- if ( schema . comment ) {
218
- return { ...merged , comment : schema . comment } ;
219
- }
220
-
221
- return merged ;
215
+ return withComment ( merged , schema ) ;
222
216
} else if ( schema . type === 'array' ) {
223
217
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 ) ;
228
220
} 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
+ ) ;
235
229
} else if ( schema . type === 'tuple' ) {
236
230
const schemas = schema . schemas . map ( optimize ) ;
237
- return { type : 'tuple' , schemas } ;
231
+ return withComment ( { type : 'tuple' , schemas } , schema ) ;
238
232
} else if ( schema . type === 'ref' ) {
239
- if ( schema . comment ) {
240
- return { ...schema , comment : schema . comment } ;
241
- }
242
233
return schema ;
243
234
} else {
244
235
return schema ;
0 commit comments