2018-11-21 09:05:27 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
2019-04-28 20:47:57 +01:00
|
|
|
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
|
2019-03-20 13:47:58 +00:00
|
|
|
import {Declaration} from '../../../src/ngtsc/reflection';
|
2018-11-21 09:05:27 +00:00
|
|
|
import {NgccReflectionHost} from '../host/ngcc_host';
|
|
|
|
|
import {hasNameIdentifier, isDefined} from '../utils';
|
2018-12-13 11:52:20 -08:00
|
|
|
import {NgccReferencesRegistry} from './ngcc_references_registry';
|
2018-11-21 09:05:27 +00:00
|
|
|
|
|
|
|
|
export interface ExportInfo {
|
|
|
|
|
identifier: string;
|
2019-04-28 20:47:57 +01:00
|
|
|
from: AbsoluteFsPath;
|
|
|
|
|
dtsFrom?: AbsoluteFsPath|null;
|
2019-02-14 18:59:46 +01:00
|
|
|
alias?: string|null;
|
2018-11-21 09:05:27 +00:00
|
|
|
}
|
|
|
|
|
export type PrivateDeclarationsAnalyses = ExportInfo[];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class will analyze a program to find all the declared classes
|
|
|
|
|
* (i.e. on an NgModule) that are not publicly exported via an entry-point.
|
|
|
|
|
*/
|
|
|
|
|
export class PrivateDeclarationsAnalyzer {
|
2018-12-13 11:52:20 -08:00
|
|
|
constructor(
|
|
|
|
|
private host: NgccReflectionHost, private referencesRegistry: NgccReferencesRegistry) {}
|
2018-11-21 09:05:27 +00:00
|
|
|
|
|
|
|
|
analyzeProgram(program: ts.Program): PrivateDeclarationsAnalyses {
|
|
|
|
|
const rootFiles = this.getRootFiles(program);
|
|
|
|
|
return this.getPrivateDeclarations(rootFiles, this.referencesRegistry.getDeclarationMap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getRootFiles(program: ts.Program): ts.SourceFile[] {
|
|
|
|
|
return program.getRootFileNames().map(f => program.getSourceFile(f)).filter(isDefined);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getPrivateDeclarations(
|
|
|
|
|
rootFiles: ts.SourceFile[],
|
|
|
|
|
declarations: Map<ts.Identifier, Declaration>): PrivateDeclarationsAnalyses {
|
|
|
|
|
const privateDeclarations: Map<ts.Identifier, Declaration> = new Map(declarations);
|
2019-02-14 18:59:46 +01:00
|
|
|
const exportAliasDeclarations: Map<ts.Identifier, string> = new Map();
|
|
|
|
|
|
2018-11-21 09:05:27 +00:00
|
|
|
rootFiles.forEach(f => {
|
|
|
|
|
const exports = this.host.getExportsOfModule(f);
|
|
|
|
|
if (exports) {
|
|
|
|
|
exports.forEach((declaration, exportedName) => {
|
2019-02-14 18:59:46 +01:00
|
|
|
if (hasNameIdentifier(declaration.node)) {
|
|
|
|
|
const privateDeclaration = privateDeclarations.get(declaration.node.name);
|
|
|
|
|
if (privateDeclaration) {
|
|
|
|
|
if (privateDeclaration.node !== declaration.node) {
|
|
|
|
|
throw new Error(`${declaration.node.name.text} is declared multiple times.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (declaration.node.name.text === exportedName) {
|
|
|
|
|
// This declaration is public so we can remove it from the list
|
|
|
|
|
privateDeclarations.delete(declaration.node.name);
|
|
|
|
|
} else if (!this.host.getDtsDeclaration(declaration.node)) {
|
|
|
|
|
// The referenced declaration is exported publicly but via an alias.
|
|
|
|
|
// In some cases the original declaration is missing from the dts program, such as
|
|
|
|
|
// when rolling up (flattening) the dts files.
|
|
|
|
|
// This is because the original declaration gets renamed to the exported alias.
|
|
|
|
|
|
|
|
|
|
// There is a constraint on this which we cannot handle. Consider the following
|
|
|
|
|
// code:
|
|
|
|
|
//
|
|
|
|
|
// /src/entry_point.js:
|
|
|
|
|
// export {MyComponent as aliasedMyComponent} from './a';
|
|
|
|
|
// export {MyComponent} from './b';`
|
|
|
|
|
//
|
|
|
|
|
// /src/a.js:
|
|
|
|
|
// export class MyComponent {}
|
|
|
|
|
//
|
|
|
|
|
// /src/b.js:
|
|
|
|
|
// export class MyComponent {}
|
|
|
|
|
//
|
|
|
|
|
// //typings/entry_point.d.ts:
|
|
|
|
|
// export declare class aliasedMyComponent {}
|
|
|
|
|
// export declare class MyComponent {}
|
|
|
|
|
//
|
|
|
|
|
// In this case we would end up matching the `MyComponent` from `/src/a.js` to the
|
|
|
|
|
// `MyComponent` declared in `/typings/entry_point.d.ts` even though that
|
|
|
|
|
// declaration is actually for the `MyComponent` in `/src/b.js`.
|
|
|
|
|
|
|
|
|
|
exportAliasDeclarations.set(declaration.node.name, exportedName);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-21 09:05:27 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-02-14 18:59:46 +01:00
|
|
|
|
2018-11-21 09:05:27 +00:00
|
|
|
return Array.from(privateDeclarations.keys()).map(id => {
|
2019-04-28 20:47:57 +01:00
|
|
|
const from = AbsoluteFsPath.fromSourceFile(id.getSourceFile());
|
2018-11-21 09:05:27 +00:00
|
|
|
const declaration = privateDeclarations.get(id) !;
|
2019-02-14 18:59:46 +01:00
|
|
|
const alias = exportAliasDeclarations.get(id) || null;
|
2018-11-29 08:26:00 +00:00
|
|
|
const dtsDeclaration = this.host.getDtsDeclaration(declaration.node);
|
2019-04-28 20:47:57 +01:00
|
|
|
const dtsFrom =
|
|
|
|
|
dtsDeclaration && AbsoluteFsPath.fromSourceFile(dtsDeclaration.getSourceFile());
|
2019-02-14 18:59:46 +01:00
|
|
|
|
|
|
|
|
return {identifier: id.text, from, dtsFrom, alias};
|
2018-11-21 09:05:27 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|