@@ -33,7 +33,7 @@ import {
33
33
import {
34
34
indent
35
35
} from "./util" ;
36
- import { Source , NodeKind , ImportStatement } from "./ast" ;
36
+ import { Source , NodeKind , ImportStatement , DeclarationStatement , ExportStatement } from "./ast" ;
37
37
38
38
/** Walker base class. */
39
39
abstract class ExportsWalker {
@@ -143,6 +143,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
143
143
private generatedDecodeFunctions = new Set < string > ( ) ;
144
144
private exportedClasses : Class [ ] = [ ] ;
145
145
private exportedFunctions : Function [ ] = [ ] ;
146
+ private filesByImport = new Map < string , string > ( ) ;
146
147
147
148
static build ( program : Program ) : string {
148
149
return new NEARBindingsBuilder ( program ) . build ( ) ;
@@ -317,7 +318,7 @@ export class NEARBindingsBuilder extends ExportsWalker {
317
318
}` ) ;
318
319
} else {
319
320
this . sb . push ( `pushObject(name: string): bool {
320
- ${ valuePrefix } .push(__near_decode_${ this . encodeType ( fieldType ) } (this.buffer, this.decoder.state));
321
+ ${ valuePrefix } .push(< ${ fieldType } > __near_decode_${ this . encodeType ( fieldType ) } (this.buffer, this.decoder.state));
321
322
return false;
322
323
}
323
324
pushArray(name: string): bool {
@@ -326,12 +327,13 @@ export class NEARBindingsBuilder extends ExportsWalker {
326
327
this.handledRoot = true;
327
328
return true;
328
329
}
329
- ${ valuePrefix } .push(__near_decode_${ this . encodeType ( fieldType ) } (this.buffer, this.decoder.state));
330
+ ${ valuePrefix } .push(< ${ fieldType } > __near_decode_${ this . encodeType ( fieldType ) } (this.buffer, this.decoder.state));
330
331
return false;
331
332
}` ) ;
332
333
}
333
334
}
334
335
336
+
335
337
private generateEncodeFunction ( type : Type ) {
336
338
if ( ! type . classReference ) {
337
339
return ;
@@ -343,6 +345,11 @@ export class NEARBindingsBuilder extends ExportsWalker {
343
345
}
344
346
this . generatedEncodeFunctions . add ( typeName ) ;
345
347
348
+ let methodName = `__near_encode_${ typeName } ` ;
349
+ if ( this . tryUsingImport ( type , methodName ) ) {
350
+ return ;
351
+ }
352
+
346
353
if ( this . isArrayType ( type ) ) {
347
354
// Array
348
355
this . generateEncodeFunction ( type . classReference . typeArguments ! [ 0 ] ) ;
@@ -374,6 +381,24 @@ export class NEARBindingsBuilder extends ExportsWalker {
374
381
this . sb . push ( "}" ) ;
375
382
}
376
383
384
+ private tryUsingImport ( type : Type , methodName : string ) : bool {
385
+ let importedFile = this . filesByImport . get ( type . classReference ! . simpleName ) ;
386
+ if ( importedFile ) {
387
+ if ( this . hasExport ( importedFile , methodName ) ) {
388
+ this . sb . push ( `import { ${ methodName } } from "${ importedFile } ";` ) ;
389
+ return true ;
390
+ }
391
+ }
392
+ return false ;
393
+ }
394
+
395
+ private hasExport ( importedFile : string , name : string ) : bool {
396
+ let importedSource = this . program . sources . filter (
397
+ s => "./" + s . normalizedPath == importedFile + ".ts" ) [ 0 ] ;
398
+
399
+ return this . getExports ( importedSource ) . filter ( d => d . name . text == name ) . length > 0 ;
400
+ }
401
+
377
402
private generateHandler ( type : Type ) {
378
403
let typeName = this . encodeType ( type ) ;
379
404
this . sb . push ( `export class __near_JSONHandler_${ typeName } extends ThrowingJSONHandler {
@@ -416,6 +441,11 @@ export class NEARBindingsBuilder extends ExportsWalker {
416
441
}
417
442
this . generatedDecodeFunctions . add ( typeName ) ;
418
443
444
+ let methodName = `__near_decode_${ typeName } ` ;
445
+ if ( this . tryUsingImport ( type , methodName ) ) {
446
+ return ;
447
+ }
448
+
419
449
this . generateHandler ( type ) ;
420
450
if ( this . isArrayType ( type ) ) {
421
451
// Array
@@ -529,18 +559,31 @@ export class NEARBindingsBuilder extends ExportsWalker {
529
559
}
530
560
531
561
private copyImports ( mainSource : Source ) : any {
532
- let imports = < ImportStatement [ ] > mainSource . statements
533
- . filter ( statement => statement . kind == NodeKind . IMPORT ) ;
534
-
535
- imports . forEach ( statement => {
562
+ this . getImports ( mainSource ) . forEach ( statement => {
536
563
if ( statement . declarations ) {
537
564
let declarationsStr = statement . declarations !
538
565
. map ( declaration => `${ declaration . externalName . text } as ${ declaration . name . text } ` )
539
566
. join ( "," ) ;
540
567
this . sb . push ( `import {${ declarationsStr } } from "${ statement . path . value } ";` ) ;
568
+ statement . declarations . forEach ( d => {
569
+ this . filesByImport . set ( d . name . text , statement . path . value ) ;
570
+ } ) ;
541
571
}
542
572
} ) ;
543
573
}
574
+
575
+ private getImports ( source : Source ) : ImportStatement [ ] {
576
+ return < ImportStatement [ ] > source . statements
577
+ . filter ( statement => statement . kind == NodeKind . IMPORT ) ;
578
+ }
579
+
580
+ private getExports ( source : Source ) : DeclarationStatement [ ] {
581
+ let declarations = < DeclarationStatement [ ] > source . statements
582
+ . filter ( statement =>
583
+ statement . kind == NodeKind . FUNCTIONDECLARATION ||
584
+ statement . kind == NodeKind . CLASSDECLARATION ) ;
585
+ return declarations . filter ( d => d . isTopLevelExport ) ;
586
+ }
544
587
}
545
588
546
589
/** A WebIDL definitions builder. */
0 commit comments