Skip to content

Commit f44ea4d

Browse files
committed
code cleanup in prune-dts
1 parent d437b8e commit f44ea4d

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

repo-scripts/prune-dts/extract-public-api.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,9 @@ void generateApi(
240240
path.resolve(argv.rollupDts),
241241
path.resolve(argv.untrimmedRollupDts),
242242
path.resolve(argv.publicDts),
243-
argv.otherExportsPublicDtsFiles ? argv.otherExportsPublicDtsFiles.split(',').map(filePath => path.resolve(filePath)) : []
243+
argv.otherExportsPublicDtsFiles
244+
? argv.otherExportsPublicDtsFiles
245+
.split(',')
246+
.map(filePath => path.resolve(filePath))
247+
: []
244248
);

repo-scripts/prune-dts/prune-dts.ts

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,32 @@ import { ESLint } from 'eslint';
3434
* @param inputLocation The file path to the .d.ts produced by API explorer.
3535
* @param outputLocation The output location for the pruned .d.ts file.
3636
*/
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 {
3842
const compilerOptions = {};
3943
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+
);
4149
const printer: ts.Printer = ts.createPrinter();
4250
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[];
4454

4555
const result: ts.TransformationResult<ts.SourceFile> =
4656
ts.transform<ts.SourceFile>(sourceFile, [
47-
dropPrivateApiTransformer.bind(null, program, host, otherExportSourceFiles )
57+
dropPrivateApiTransformer.bind(
58+
null,
59+
program,
60+
host,
61+
otherExportSourceFiles
62+
)
4863
]);
4964
const transformedSourceFile: ts.SourceFile = result.transformed[0];
5065
let content = printer.printFile(transformedSourceFile);
@@ -424,7 +439,7 @@ function extractJSDocComment(
424439
function extractExportedSymbol(
425440
typeChecker: ts.TypeChecker,
426441
sourceFile: ts.SourceFile,
427-
typeName: ts.Node,
442+
typeName: ts.Node
428443
): ts.Symbol | undefined {
429444
if (!ts.isIdentifier(typeName)) {
430445
return undefined;
@@ -508,15 +523,18 @@ function extractExportedSymbol(
508523
function findExternalExport(
509524
typeChecker: ts.TypeChecker,
510525
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,
512531
otherExportSourceFiles: ts.SourceFile[]
513532
): ts.SourceFile | undefined {
514533
if (!node.name) return undefined;
515534

516535
const localSymbolName = node.name.text;
517536

518537
for (const otherExportSourceFile of otherExportSourceFiles) {
519-
520538
const otherExportedSymbols = typeChecker.getExportsOfModule(
521539
typeChecker.getSymbolAtLocation(otherExportSourceFile)!
522540
);
@@ -539,8 +557,7 @@ function dropPrivateApiTransformer(
539557
program: ts.Program,
540558
host: ts.CompilerHost,
541559
otherExportSourceFiles: ts.SourceFile[],
542-
context: ts.TransformationContext,
543-
560+
context: ts.TransformationContext
544561
): ts.Transformer<ts.SourceFile> {
545562
const typeChecker = program.getTypeChecker();
546563

@@ -583,9 +600,19 @@ function dropPrivateApiTransformer(
583600
ts.isEnumDeclaration(node)
584601
) {
585602
// 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+
);
587609
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);
589616
return ts.factory.createNotEmittedStatement(node);
590617
}
591618
}
@@ -645,20 +672,34 @@ function dropPrivateApiTransformer(
645672
for (let filename in imports) {
646673
const importSpecifiers: ts.ImportSpecifier[] = [];
647674
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+
);
649682
}
650683
let outFileName = filename.startsWith('.') ? filename : `./${filename}`;
651684
outFileName = outFileName.replace('.d.ts', '');
652685
const importDeclaration = ts.factory.createImportDeclaration(
653686
[],
654-
ts.factory.createImportClause(true, undefined, ts.factory.createNamedImports(importSpecifiers)),
687+
ts.factory.createImportClause(
688+
true,
689+
undefined,
690+
ts.factory.createNamedImports(importSpecifiers)
691+
),
655692
ts.factory.createStringLiteral(outFileName, true)
656-
)
693+
);
657694

658695
moreImports.push(importDeclaration);
659696
}
660697

661-
return ts.factory.updateSourceFile(result, [...moreImports, ...result.statements], true);
698+
return ts.factory.updateSourceFile(
699+
result,
700+
[...moreImports, ...result.statements],
701+
true
702+
);
662703
};
663704
}
664705

0 commit comments

Comments
 (0)