refactor(ivy): use a named type for ImportManager import structures (#25445)

Previously we were using an anonymous type `{specifier: string; qualifier: string;}`
throughout the code base. This commit gives this type a name and ensures it
is only defined in one place.

PR Close #25445
This commit is contained in:
Pete Bacon Darwin
2019-04-28 20:48:33 +01:00
committed by Jason Aden
parent 8e201f713a
commit 95c5b1a7f6
6 changed files with 31 additions and 14 deletions

View File

@ -6,4 +6,4 @@
* found in the LICENSE file at https://angular.io/license
*/
export {ImportManager, translateExpression, translateStatement, translateType} from './src/translator';
export {Import, ImportManager, NamedImport, translateExpression, translateStatement, translateType} from './src/translator';

View File

@ -38,6 +38,27 @@ const BINARY_OPERATORS = new Map<BinaryOperator, ts.BinaryOperator>([
[BinaryOperator.Plus, ts.SyntaxKind.PlusToken],
]);
/**
* Information about an import that has been added to a module.
*/
export interface Import {
/** The name of the module that has been imported. */
specifier: string;
/** The alias of the imported module. */
qualifier: string;
}
/**
* The symbol name and import namespace of an imported symbol,
* which has been registered through the ImportManager.
*/
export interface NamedImport {
/** The import namespace containing this imported symbol. */
moduleImport: string|null;
/** The (possibly rewritten) name of the imported symbol. */
symbol: string;
}
export class ImportManager {
private specifierToIdentifier = new Map<string, string>();
private nextIndex = 0;
@ -45,8 +66,7 @@ export class ImportManager {
constructor(protected rewriter: ImportRewriter = new NoopImportRewriter(), private prefix = 'i') {
}
generateNamedImport(moduleName: string, originalSymbol: string):
{moduleImport: string | null, symbol: string} {
generateNamedImport(moduleName: string, originalSymbol: string): NamedImport {
// First, rewrite the symbol name.
const symbol = this.rewriter.rewriteSymbol(originalSymbol, moduleName);
@ -67,7 +87,7 @@ export class ImportManager {
return {moduleImport, symbol};
}
getAllImports(contextPath: string): {specifier: string, qualifier: string}[] {
getAllImports(contextPath: string): Import[] {
const imports: {specifier: string, qualifier: string}[] = [];
this.specifierToIdentifier.forEach((qualifier, specifier) => {
specifier = this.rewriter.rewriteSpecifier(specifier, contextPath);