@@ -282,14 +282,18 @@ function getAll(objects) {
282
282
283
283
/**
284
284
* Return the declarations of custom types.
285
- * @param {string[] } types - The list of types defined in comments.
285
+ * @param {object[] } types - The list of types defined in comments.
286
+ * Of the form { declaration: string, implementation: string }.
286
287
* @returns {string } The joined declarations.
287
288
*/
288
289
function getTypeDeclarations ( types ) {
289
290
return (
290
291
"// TYPES\n" +
291
292
types
292
- . map ( ( type ) => type . replace ( / \\ \/ / g, "/" ) . replace ( / \\ \\ / g, "\\" ) )
293
+ . filter ( ( type ) => ! type . class )
294
+ . map ( ( type ) =>
295
+ type . declaration . replace ( / \\ \/ / g, "/" ) . replace ( / \\ \\ / g, "\\" )
296
+ )
293
297
. join ( "" )
294
298
) ;
295
299
}
@@ -299,9 +303,10 @@ function getTypeDeclarations(types) {
299
303
* vanilla JavaScript, e.g. String, Array.
300
304
* @param {string } name - The class's name.
301
305
* @param {object } c - The class's data.
306
+ * @param {object[] } types
302
307
* @returns {string } The class's declaration.
303
308
*/
304
- function getBuiltinClassDeclaration ( name , c ) {
309
+ function getBuiltinClassDeclaration ( name , c , types ) {
305
310
return (
306
311
`interface ${ name } Constructor {\n` +
307
312
indent (
@@ -329,6 +334,7 @@ function getBuiltinClassDeclaration(name, c) {
329
334
) } `. trim ( )
330
335
)
331
336
. concat ( name === "Array" ? [ "[index: number]: T" ] : [ ] )
337
+ . concat ( types . map ( ( type ) => type . declaration ) )
332
338
. join ( "\n\n" )
333
339
) +
334
340
`\n}\n\n${ getDocumentation ( c . object ) } ` ) +
@@ -340,9 +346,10 @@ function getBuiltinClassDeclaration(name, c) {
340
346
* Get the declaration of a class that is not builtin.
341
347
* @param {string } name - The class's name.
342
348
* @param {object } c - The class's data.
349
+ * @param {object[] } types
343
350
* @returns {string } The class's declaration.
344
351
*/
345
- function getOtherClassDeclaration ( name , c ) {
352
+ function getOtherClassDeclaration ( name , c , types ) {
346
353
return (
347
354
`${ getDocumentation ( c . object ) } \ndeclare class ${
348
355
c . object ?. typescript || name
@@ -367,6 +374,7 @@ function getOtherClassDeclaration(name, c) {
367
374
) } `. trim ( )
368
375
)
369
376
. concat ( name === "ArrayBufferView" ? [ "[index: number]: number" ] : [ ] )
377
+ . concat ( types . map ( ( type ) => type . declaration ) )
370
378
. join ( "\n\n" )
371
379
) +
372
380
"\n}"
@@ -376,17 +384,26 @@ function getOtherClassDeclaration(name, c) {
376
384
/**
377
385
* Return the class declarations (not including libraries).
378
386
* @param {object } classes - The object of classes (see `getClasses`).
387
+ * @param {object[] } types
379
388
* @returns {string } The class declarations.
380
389
*/
381
- function getClassDeclarations ( classes ) {
390
+ function getClassDeclarations ( classes , types ) {
382
391
return (
383
392
"\n\n// CLASSES\n\n" +
384
393
Object . entries ( classes )
385
394
. filter ( ( [ _ , c ] ) => ! c . library )
386
395
. map ( ( [ name , c ] ) =>
387
396
name in global
388
- ? getBuiltinClassDeclaration ( name , c )
389
- : getOtherClassDeclaration ( name , c )
397
+ ? getBuiltinClassDeclaration (
398
+ name ,
399
+ c ,
400
+ types . filter ( ( type ) => type . class === name )
401
+ )
402
+ : getOtherClassDeclaration (
403
+ name ,
404
+ c ,
405
+ types . filter ( ( type ) => type . class === name )
406
+ )
390
407
)
391
408
. join ( "\n\n" )
392
409
) ;
@@ -456,13 +473,13 @@ function getLibraryDeclarations(classes) {
456
473
function buildTypes ( ) {
457
474
return new Promise ( ( resolve ) => {
458
475
require ( "./common.js" ) . readAllWrapperFiles ( function ( objects , types ) {
459
- const [ classes , globals ] = getAll ( objects ) ;
476
+ const [ classes , globals ] = getAll ( objects , types ) ;
460
477
461
478
resolve (
462
479
"// NOTE: This file has been automatically generated.\n\n" +
463
480
'/// <reference path="other.d.ts" />\n\n' +
464
481
getTypeDeclarations ( types ) +
465
- getClassDeclarations ( classes ) +
482
+ getClassDeclarations ( classes , types ) +
466
483
getGlobalDeclarations ( globals ) +
467
484
getLibraryDeclarations ( classes )
468
485
) ;
0 commit comments