@@ -34,17 +34,32 @@ import { ESLint } from 'eslint';
34
34
* @param inputLocation The file path to the .d.ts produced by API explorer.
35
35
* @param outputLocation The output location for the pruned .d.ts file.
36
36
*/
37
- export function pruneDts ( inputLocation : string , outputLocation : string , otherExportFileLocations : string [ ] = [ ] ) : void {
37
+ export function pruneDts (
38
+ inputLocation : string ,
39
+ outputLocation : string ,
40
+ otherExportFileLocations : string [ ] = [ ]
41
+ ) : void {
38
42
const compilerOptions = { } ;
39
43
const host = ts . createCompilerHost ( compilerOptions ) ;
40
- const program = ts . createProgram ( [ inputLocation , ...otherExportFileLocations ] , compilerOptions , host ) ;
44
+ const program = ts . createProgram (
45
+ [ inputLocation , ...otherExportFileLocations ] ,
46
+ compilerOptions ,
47
+ host
48
+ ) ;
41
49
const printer : ts . Printer = ts . createPrinter ( ) ;
42
50
const sourceFile = program . getSourceFile ( inputLocation ) ! ;
43
- const otherExportSourceFiles = otherExportFileLocations . map ( otherFileLocation => program . getSourceFile ( otherFileLocation ) ) . filter ( value => value !== undefined ) as ts . SourceFile [ ] ;
51
+ const otherExportSourceFiles = otherExportFileLocations
52
+ . map ( otherFileLocation => program . getSourceFile ( otherFileLocation ) )
53
+ . filter ( value => value !== undefined ) as ts . SourceFile [ ] ;
44
54
45
55
const result : ts . TransformationResult < ts . SourceFile > =
46
56
ts . transform < ts . SourceFile > ( sourceFile , [
47
- dropPrivateApiTransformer . bind ( null , program , host , otherExportSourceFiles )
57
+ dropPrivateApiTransformer . bind (
58
+ null ,
59
+ program ,
60
+ host ,
61
+ otherExportSourceFiles
62
+ )
48
63
] ) ;
49
64
const transformedSourceFile : ts . SourceFile = result . transformed [ 0 ] ;
50
65
let content = printer . printFile ( transformedSourceFile ) ;
@@ -424,7 +439,7 @@ function extractJSDocComment(
424
439
function extractExportedSymbol (
425
440
typeChecker : ts . TypeChecker ,
426
441
sourceFile : ts . SourceFile ,
427
- typeName : ts . Node ,
442
+ typeName : ts . Node
428
443
) : ts . Symbol | undefined {
429
444
if ( ! ts . isIdentifier ( typeName ) ) {
430
445
return undefined ;
@@ -508,15 +523,18 @@ function extractExportedSymbol(
508
523
function findExternalExport (
509
524
typeChecker : ts . TypeChecker ,
510
525
sourceFile : ts . SourceFile ,
511
- node : ts . InterfaceDeclaration | ts . ClassDeclaration | ts . TypeAliasDeclaration | ts . EnumDeclaration ,
526
+ node :
527
+ | ts . InterfaceDeclaration
528
+ | ts . ClassDeclaration
529
+ | ts . TypeAliasDeclaration
530
+ | ts . EnumDeclaration ,
512
531
otherExportSourceFiles : ts . SourceFile [ ]
513
532
) : ts . SourceFile | undefined {
514
533
if ( ! node . name ) return undefined ;
515
534
516
535
const localSymbolName = node . name . text ;
517
536
518
537
for ( const otherExportSourceFile of otherExportSourceFiles ) {
519
-
520
538
const otherExportedSymbols = typeChecker . getExportsOfModule (
521
539
typeChecker . getSymbolAtLocation ( otherExportSourceFile ) !
522
540
) ;
@@ -539,8 +557,7 @@ function dropPrivateApiTransformer(
539
557
program : ts . Program ,
540
558
host : ts . CompilerHost ,
541
559
otherExportSourceFiles : ts . SourceFile [ ] ,
542
- context : ts . TransformationContext ,
543
-
560
+ context : ts . TransformationContext
544
561
) : ts . Transformer < ts . SourceFile > {
545
562
const typeChecker = program . getTypeChecker ( ) ;
546
563
@@ -583,9 +600,19 @@ function dropPrivateApiTransformer(
583
600
ts . isEnumDeclaration ( node )
584
601
) {
585
602
// Remove any types that are exported externally
586
- const externalExportFile = findExternalExport ( typeChecker , sourceFile , node , otherExportSourceFiles ) ;
603
+ const externalExportFile = findExternalExport (
604
+ typeChecker ,
605
+ sourceFile ,
606
+ node ,
607
+ otherExportSourceFiles
608
+ ) ;
587
609
if ( externalExportFile && node . name ) {
588
- ensureImportsForFile ( path . relative ( path . dirname ( sourceFile . fileName ) , externalExportFile . fileName ) ) . push ( node . name . text ) ;
610
+ ensureImportsForFile (
611
+ path . relative (
612
+ path . dirname ( sourceFile . fileName ) ,
613
+ externalExportFile . fileName
614
+ )
615
+ ) . push ( node . name . text ) ;
589
616
return ts . factory . createNotEmittedStatement ( node ) ;
590
617
}
591
618
}
@@ -645,20 +672,34 @@ function dropPrivateApiTransformer(
645
672
for ( let filename in imports ) {
646
673
const importSpecifiers : ts . ImportSpecifier [ ] = [ ] ;
647
674
for ( let identifier of imports [ filename ] ) {
648
- importSpecifiers . push ( ts . factory . createImportSpecifier ( false , undefined , ts . factory . createIdentifier ( identifier ) ) ) ;
675
+ importSpecifiers . push (
676
+ ts . factory . createImportSpecifier (
677
+ false ,
678
+ undefined ,
679
+ ts . factory . createIdentifier ( identifier )
680
+ )
681
+ ) ;
649
682
}
650
683
let outFileName = filename . startsWith ( '.' ) ? filename : `./${ filename } ` ;
651
684
outFileName = outFileName . replace ( '.d.ts' , '' ) ;
652
685
const importDeclaration = ts . factory . createImportDeclaration (
653
686
[ ] ,
654
- ts . factory . createImportClause ( true , undefined , ts . factory . createNamedImports ( importSpecifiers ) ) ,
687
+ ts . factory . createImportClause (
688
+ true ,
689
+ undefined ,
690
+ ts . factory . createNamedImports ( importSpecifiers )
691
+ ) ,
655
692
ts . factory . createStringLiteral ( outFileName , true )
656
- )
693
+ ) ;
657
694
658
695
moreImports . push ( importDeclaration ) ;
659
696
}
660
697
661
- return ts . factory . updateSourceFile ( result , [ ...moreImports , ...result . statements ] , true ) ;
698
+ return ts . factory . updateSourceFile (
699
+ result ,
700
+ [ ...moreImports , ...result . statements ] ,
701
+ true
702
+ ) ;
662
703
} ;
663
704
}
664
705
0 commit comments