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

@ -9,6 +9,7 @@ import MagicString from 'magic-string';
import * as ts from 'typescript';
import {PathSegment, AbsoluteFsPath} from '../../../src/ngtsc/path';
import {isDtsPath} from '../../../src/ngtsc/util/src/typescript';
import {Import} from '../../../src/ngtsc/translator';
import {CompiledClass} from '../analysis/decoration_analyzer';
import {ExportInfo} from '../analysis/private_declarations_analyzer';
import {FileSystem} from '../file_system/file_system';
@ -27,9 +28,7 @@ export class EsmRenderer extends Renderer {
/**
* Add the imports at the top of the file
*/
addImports(
output: MagicString, imports: {specifier: string; qualifier: string;}[],
sf: ts.SourceFile): void {
addImports(output: MagicString, imports: Import[], sf: ts.SourceFile): void {
const insertionPoint = findEndOfImports(sf);
const renderedImports =
imports.map(i => `import * as ${i.qualifier} from '${i.specifier}';\n`).join('');

View File

@ -14,8 +14,7 @@ import * as ts from 'typescript';
import {NoopImportRewriter, ImportRewriter, R3SymbolsImportRewriter, NOOP_DEFAULT_IMPORT_RECORDER} from '../../../src/ngtsc/imports';
import {AbsoluteFsPath, PathSegment} from '../../../src/ngtsc/path';
import {CompileResult} from '../../../src/ngtsc/transform';
import {translateStatement, translateType, ImportManager} from '../../../src/ngtsc/translator';
import {translateStatement, translateType, Import, ImportManager} from '../../../src/ngtsc/translator';
import {CompiledClass, CompiledFile, DecorationAnalyses} from '../analysis/decoration_analyzer';
import {ModuleWithProvidersInfo, ModuleWithProvidersAnalyses} from '../analysis/module_with_providers_analyzer';
import {PrivateDeclarationsAnalyses, ExportInfo} from '../analysis/private_declarations_analyzer';
@ -250,9 +249,7 @@ export abstract class Renderer {
protected abstract addConstants(output: MagicString, constants: string, file: ts.SourceFile):
void;
protected abstract addImports(
output: MagicString, imports: {specifier: string, qualifier: string}[],
sf: ts.SourceFile): void;
protected abstract addImports(output: MagicString, imports: Import[], sf: ts.SourceFile): void;
protected abstract addExports(
output: MagicString, entryPointBasePath: AbsoluteFsPath, exports: ExportInfo[]): void;
protected abstract addDefinitions(

View File

@ -18,6 +18,7 @@ ts_library(
"//packages/compiler-cli/src/ngtsc/reflection",
"//packages/compiler-cli/src/ngtsc/testing",
"//packages/compiler-cli/src/ngtsc/transform",
"//packages/compiler-cli/src/ngtsc/translator",
"//packages/compiler-cli/test:test_utils",
"@npm//@types/convert-source-map",
"@npm//@types/mock-fs",

View File

@ -9,6 +9,7 @@ import MagicString from 'magic-string';
import * as ts from 'typescript';
import {fromObject, generateMapFileComment} from 'convert-source-map';
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
import {Import} from '../../../src/ngtsc/translator';
import {CompiledClass, DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
import {ModuleWithProvidersAnalyzer} from '../../src/analysis/module_with_providers_analyzer';
@ -31,8 +32,7 @@ class TestRenderer extends Renderer {
bundle: EntryPointBundle) {
super(fs, logger, host, isCore, bundle);
}
addImports(
output: MagicString, imports: {specifier: string, qualifier: string}[], sf: ts.SourceFile) {
addImports(output: MagicString, imports: Import[], sf: ts.SourceFile) {
output.prepend('\n// ADD IMPORTS\n');
}
addExports(output: MagicString, baseEntryPointPath: string, exports: {

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);