@@ -1152,3 +1152,195 @@ export function printStatic(node: Node, i: number = 0, recursion?: Recursion): s
1152
1152
return printStaticBrandCombinator ( node , i , recursion )
1153
1153
}
1154
1154
}
1155
+
1156
+ function printCProperty ( p : Property , i : number , recursion ?: Recursion ) : string {
1157
+ const optional = p . isOptional ? '?' : ''
1158
+ const type = printC ( p . type , i , recursion )
1159
+ const sep = ': '
1160
+ return printDescription ( p . description , i ) + indent ( i ) + escapePropertyKey ( p . key ) + optional + sep + type
1161
+ }
1162
+
1163
+ function printCLiteralCombinator ( c : LiteralCombinator ) : string {
1164
+ let s = 't.LiteralC<'
1165
+ s += typeof c . value === 'string' ? escapeString ( c . value ) : String ( c . value )
1166
+ s += '>'
1167
+ return s
1168
+ }
1169
+
1170
+ function printCInterfaceCombinator ( c : InterfaceCombinator , i : number , recursion ?: Recursion ) : string {
1171
+ let s = 't.TypeC<{\n'
1172
+ s += c . properties . map ( p => printCProperty ( p , i + 1 , recursion ) ) . join ( ',\n' )
1173
+ s += `\n${ indent ( i ) } }>`
1174
+ return s
1175
+ }
1176
+
1177
+ function printCPartialCombinator ( c : PartialCombinator , i : number , recursion ?: Recursion ) : string {
1178
+ let s = 't.TypeC<{\n'
1179
+ s += c . properties . map ( p => printCProperty ( { ...p , isOptional : true } , i + 1 , recursion ) ) . join ( ',\n' )
1180
+ s += `\n${ indent ( i ) } }>`
1181
+ return s
1182
+ }
1183
+
1184
+ function printCTypesCombinator ( types : Array < TypeReference > , i : number , recursion ?: Recursion ) : string {
1185
+ const indentation = indent ( i + 1 )
1186
+ return types . map ( t => `${ indentation } ${ printC ( t , i , recursion ) } ` ) . join ( `,\n` )
1187
+ }
1188
+
1189
+ function printCUnionCombinator ( c : UnionCombinator , i : number , recursion ?: Recursion ) : string {
1190
+ return 't.UnionC<[\n' + printCTypesCombinator ( c . types , i , recursion ) + '\n]>'
1191
+ }
1192
+
1193
+ function printCTaggedUnionCombinator ( c : TaggedUnionCombinator , i : number , recursion ?: Recursion ) : string {
1194
+ return 't.UnionC<[\n' + printCTypesCombinator ( c . types , i , recursion ) + '\n]>'
1195
+ }
1196
+
1197
+ function printCIntersectionCombinator ( c : IntersectionCombinator , i : number , recursion ?: Recursion ) : string {
1198
+ return 't.IntersectionC<[\n' + printCTypesCombinator ( c . types , i , recursion ) + '\n]>'
1199
+ }
1200
+
1201
+ function printCKeyofCombinator ( c : KeyofCombinator , i : number ) : string {
1202
+ return printC ( unionCombinator ( c . values . map ( value => literalCombinator ( value ) ) ) , i )
1203
+ }
1204
+
1205
+ function printCArrayCombinator ( c : ArrayCombinator , i : number , recursion ?: Recursion ) : string {
1206
+ return `t.ArrayC<${ printC ( c . type , i , recursion ) } >`
1207
+ }
1208
+
1209
+ function printCExactCombinator ( c : ExactCombinator , i : number , recursion ?: Recursion ) : string {
1210
+ return printC ( c . type , i , recursion )
1211
+ }
1212
+
1213
+ function printCStrictCombinator ( c : StrictCombinator , i : number , recursion ?: Recursion ) : string {
1214
+ let s = 't.TypeC<{\n'
1215
+ s += c . properties . map ( p => printCProperty ( p , i + 1 , recursion ) ) . join ( ',\n' )
1216
+ s += `\n${ indent ( i ) } }>`
1217
+ return s
1218
+ }
1219
+
1220
+ function printCReadonlyCombinator ( c : ReadonlyCombinator , i : number , recursion ?: Recursion ) : string {
1221
+ return `t.ReadonlyC<${ printC ( c . type , i , recursion ) } >`
1222
+ }
1223
+
1224
+ function printCBrandCombinator ( bc : BrandCombinator , i : number , recursion ?: Recursion ) : string {
1225
+ return `t.BrandC<${ printC ( bc . type , i , recursion ) } , ${ bc . name } Brand>`
1226
+ }
1227
+
1228
+ function printCReadonlyArrayCombinator ( c : ReadonlyArrayCombinator , i : number , recursion ?: Recursion ) : string {
1229
+ return `t.ReadonlyArrayC<${ printC ( c . type , i , recursion ) } >`
1230
+ }
1231
+
1232
+ function printCDictionaryCombinator ( c : DictionaryCombinator , i : number , recursion ?: Recursion ) : string {
1233
+ return `t.RecordC<${ printC ( c . domain , i ) } , ${ printC ( c . codomain , i , recursion ) } >`
1234
+ }
1235
+
1236
+ function printCTupleCombinator ( c : TupleCombinator , i : number , recursion ?: Recursion ) : string {
1237
+ const indentation = indent ( i + 1 )
1238
+ let s = '[\n'
1239
+ s += c . types . map ( t => `${ indentation } ${ printC ( t , i , recursion ) } ` ) . join ( ',\n' )
1240
+ s += `\n${ indent ( i ) } ]`
1241
+ return s
1242
+ }
1243
+
1244
+ function printCCustomTypeDeclarationType ( name : string ) : string {
1245
+ return `// exists type ${ name } C extends t.Any`
1246
+ }
1247
+
1248
+ function printCTypeDeclarationType (
1249
+ type : TypeReference ,
1250
+ name : string ,
1251
+ isReadonly : boolean ,
1252
+ isExported : boolean ,
1253
+ description : string | undefined ,
1254
+ recursion ?: Recursion
1255
+ ) : string {
1256
+ if ( type . kind . startsWith ( 'Custom' ) ) {
1257
+ return printCCustomTypeDeclarationType ( name )
1258
+ }
1259
+ let s = printC ( type , 0 , recursion )
1260
+ if ( isReadonly ) {
1261
+ s = `t.ReadonlyC<${ s } >`
1262
+ }
1263
+ s = `type ${ name } C = ${ s } `
1264
+ if ( isExported ) {
1265
+ s = `export ${ s } `
1266
+ }
1267
+ return printDescription ( description , 0 ) + s
1268
+ }
1269
+
1270
+ function printCTypeDeclaration ( declaration : TypeDeclaration ) : string {
1271
+ return printCTypeDeclarationType (
1272
+ declaration . type ,
1273
+ declaration . name ,
1274
+ declaration . isReadonly ,
1275
+ declaration . isExported ,
1276
+ declaration . description
1277
+ )
1278
+ }
1279
+
1280
+ function printCCustomTypeDeclaration ( declaration : CustomTypeDeclaration ) : string {
1281
+ return printCCustomTypeDeclarationType ( declaration . name )
1282
+ }
1283
+
1284
+ export function printC ( node : Node , i : number = 0 , recursion ?: Recursion ) : string {
1285
+ switch ( node . kind ) {
1286
+ case 'Identifier' :
1287
+ return node . name + 'C'
1288
+ case 'StringType' :
1289
+ case 'NumberType' :
1290
+ case 'BooleanType' :
1291
+ case 'NullType' :
1292
+ case 'UndefinedType' :
1293
+ case 'FunctionType' :
1294
+ case 'UnknownType' :
1295
+ return `t.${ node . name
1296
+ . charAt ( 0 )
1297
+ . toUpperCase ( )
1298
+ . concat ( node . name . slice ( 1 ) ) } C`
1299
+ case 'IntType' :
1300
+ return `t.BrandC<t.NumberC, t.${ node . name } Brand>`
1301
+ case 'IntegerType' :
1302
+ return 't.NumberC'
1303
+ case 'AnyArrayType' :
1304
+ return 't.ArrayC<t.UnknownC>'
1305
+ case 'AnyDictionaryType' :
1306
+ return 't.RecordC<t.StringC, t.UnknownC>'
1307
+ case 'LiteralCombinator' :
1308
+ return printCLiteralCombinator ( node )
1309
+ case 'InterfaceCombinator' :
1310
+ return printCInterfaceCombinator ( node , i , recursion )
1311
+ case 'PartialCombinator' :
1312
+ return printCPartialCombinator ( node , i , recursion )
1313
+ case 'UnionCombinator' :
1314
+ return printCUnionCombinator ( node , i , recursion )
1315
+ case 'TaggedUnionCombinator' :
1316
+ return printCTaggedUnionCombinator ( node , i , recursion )
1317
+ case 'IntersectionCombinator' :
1318
+ return printCIntersectionCombinator ( node , i , recursion )
1319
+ case 'KeyofCombinator' :
1320
+ return printCKeyofCombinator ( node , i )
1321
+ case 'ArrayCombinator' :
1322
+ return printCArrayCombinator ( node , i , recursion )
1323
+ case 'ReadonlyArrayCombinator' :
1324
+ return printCReadonlyArrayCombinator ( node , i , recursion )
1325
+ case 'TupleCombinator' :
1326
+ return printCTupleCombinator ( node , i , recursion )
1327
+ case 'RecursiveCombinator' :
1328
+ return printC ( node . type , i , recursion )
1329
+ case 'DictionaryCombinator' :
1330
+ return printCDictionaryCombinator ( node , i , recursion )
1331
+ case 'TypeDeclaration' :
1332
+ return printCTypeDeclaration ( node )
1333
+ case 'CustomTypeDeclaration' :
1334
+ return printCCustomTypeDeclaration ( node )
1335
+ case 'CustomCombinator' :
1336
+ return 't.Any'
1337
+ case 'ExactCombinator' :
1338
+ return printCExactCombinator ( node , i , recursion )
1339
+ case 'StrictCombinator' :
1340
+ return printCStrictCombinator ( node , i , recursion )
1341
+ case 'ReadonlyCombinator' :
1342
+ return printCReadonlyCombinator ( node , i , recursion )
1343
+ case 'BrandCombinator' :
1344
+ return printCBrandCombinator ( node , i , recursion )
1345
+ }
1346
+ }
0 commit comments