diff --git a/packages/compiler-cli/ngcc/src/rendering/esm_renderer.ts b/packages/compiler-cli/ngcc/src/rendering/esm_renderer.ts index 15fe6f61fb..607595449f 100644 --- a/packages/compiler-cli/ngcc/src/rendering/esm_renderer.ts +++ b/packages/compiler-cli/ngcc/src/rendering/esm_renderer.ts @@ -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(''); diff --git a/packages/compiler-cli/ngcc/src/rendering/renderer.ts b/packages/compiler-cli/ngcc/src/rendering/renderer.ts index b1410eed1a..eb69584086 100644 --- a/packages/compiler-cli/ngcc/src/rendering/renderer.ts +++ b/packages/compiler-cli/ngcc/src/rendering/renderer.ts @@ -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( diff --git a/packages/compiler-cli/ngcc/test/BUILD.bazel b/packages/compiler-cli/ngcc/test/BUILD.bazel index 7aabc69b43..4ed0921b8d 100644 --- a/packages/compiler-cli/ngcc/test/BUILD.bazel +++ b/packages/compiler-cli/ngcc/test/BUILD.bazel @@ -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", diff --git a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts index 3a9d757074..cac283fccd 100644 --- a/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts +++ b/packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts @@ -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: { diff --git a/packages/compiler-cli/src/ngtsc/translator/index.ts b/packages/compiler-cli/src/ngtsc/translator/index.ts index b71e3e7900..44120dae41 100644 --- a/packages/compiler-cli/src/ngtsc/translator/index.ts +++ b/packages/compiler-cli/src/ngtsc/translator/index.ts @@ -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'; diff --git a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts index 94f630ae6b..f1b6e478ff 100644 --- a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts +++ b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts @@ -38,6 +38,27 @@ const BINARY_OPERATORS = new Map([ [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(); 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);