Skip to content

Commit

Permalink
Handle @internal APIs.
Browse files Browse the repository at this point in the history
With stripInternal set, TypeScript does not generate exports for APIs
marked as @internal, which tsickle should respect when generating Clutz
aliases.
  • Loading branch information
mprobst committed Oct 21, 2019
1 parent 2d304a3 commit 52dfedb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/tsickle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function emit(
sourceFiles.map(sf => sf.fileName)}`);
}
const originalSource = sourceFiles[0];
content = addClutzAliases(content, originalSource, typeChecker, host);
content = addClutzAliases(content, originalSource, typeChecker, host, tsOptions);
}
writeFile(fileName, content, writeByteOrderMark, onError, sourceFiles);
};
Expand Down Expand Up @@ -229,7 +229,7 @@ function stringCompare(a: string, b: string): number {
*/
function addClutzAliases(
dtsFileContent: string, sourceFile: ts.SourceFile, typeChecker: ts.TypeChecker,
host: TsickleHost): string {
host: TsickleHost, options: ts.CompilerOptions): string {
const moduleSymbol = typeChecker.getSymbolAtLocation(sourceFile);
const moduleExports = moduleSymbol && typeChecker.getExportsOfModule(moduleSymbol);
if (!moduleExports) return dtsFileContent;
Expand Down Expand Up @@ -278,6 +278,14 @@ function addClutzAliases(
return false;
}

// @internal marked APIs are not exported, so must not get aliases.
// This uses an internal TS API, assuming that accessing this will be more stable compared to
// implementing our own version.
// tslint:disable-next-line: no-any
if (options.stripInternal && (ts as any).isInternalDeclaration(d, origSourceFile)) {
return false;
}

if (!ts.isExportSpecifier(d)) {
// we have a pure export (case 1) thus safe to emit clutz alias.
return true;
Expand Down
1 change: 1 addition & 0 deletions test/test_support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const baseCompilerOptions: ts.CompilerOptions = {
allowJs: false,
importHelpers: true,
noEmitHelpers: true,
stripInternal: true,
baseUrl: '.',
paths: {
// The compiler builtin 'tslib' library is looked up by name,
Expand Down
5 changes: 5 additions & 0 deletions test_files/internal.declaration/internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @fileoverview Test to reproduce that \@internal functions are not re-exported for Clutz. There
* should not be any `.d.ts` aliases generated for the function below.
*/
export {};
Empty file.
9 changes: 9 additions & 0 deletions test_files/internal.declaration/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @fileoverview Test to reproduce that \@internal functions are not re-exported for Clutz. There
* should not be any `.d.ts` aliases generated for the function below.
*/

/** @internal */
export function internalFunction() {
return 42;
}

0 comments on commit 52dfedb

Please sign in to comment.