refactor(ivy): extract import rewriting into a separate interface (#27998)

Currently the ImportManager class handles various rewriting actions of
imports when compiling @angular/core. This is required as code compiled
within @angular/core cannot import from '@angular/core'. To work around
this, imports are rewritten to get core symbols from a particular file,
r3_symbols.ts.

In this refactoring, this rewriting logic is moved out of the ImportManager
and put behind an interface, ImportRewriter. There are three implementers
of the interface:

* NoopImportRewriter, used for compiling all non-core packages.
* R3SymbolsImportRewriter, used when ngtsc compiles @angular/core.
* NgccFlatImportRewriter, used when ngcc compiles @angular/core (special
  logic is needed because ngcc has to rewrite imports in flat bundles
  differently than in non-flat bundles).

This is a precursor to using this rewriting logic in other contexts besides
the ImportManager.

PR Close #27998
This commit is contained in:
Alex Rickabaugh
2019-01-08 11:49:58 -08:00
committed by Andrew Kushnir
parent 5a0deb8d69
commit 3cf1b62722
15 changed files with 222 additions and 95 deletions

View File

@ -8,6 +8,7 @@
import * as ts from 'typescript';
import {NoopImportRewriter} from '../../imports';
import {TypeScriptReflectionHost} from '../../reflection';
import {getDeclaration, makeProgram} from '../../testing/in_memory_typescript';
import {ImportManager, translateStatement} from '../../translator';
@ -87,7 +88,7 @@ function compileAndPrint(contents: string): string {
return '';
}
const sf = program.getSourceFile('index.ts') !;
const im = new ImportManager(false, 'i');
const im = new ImportManager(new NoopImportRewriter(), 'i');
const tsStatement = translateStatement(call, im);
const res = ts.createPrinter().printNode(ts.EmitHint.Unspecified, tsStatement, sf);
return res.replace(/\s+/g, ' ');