fix(ivy): generate type references to a default import (#29146)

This commit refactors and expands ngtsc's support for generating imports of
values from imports of types (this is used for example when importing a
class referenced in a type annotation in a constructor).

Previously, this logic handled "import {Foo} from" and "import * as foo
from" style imports, but failed on imports of default values ("import
Foo from"). This commit moves the type-to-value logic to a separate file and
expands it to cover the default import case. Doing this also required
augmenting the ImportManager to track default as well as non-default import
generation. The APIs were made a little cleaner at the same time.

PR Close #29146
This commit is contained in:
Alex Rickabaugh
2019-03-06 16:35:08 -08:00
committed by Kara Erickson
parent 37c5a26421
commit b6f6b1178f
14 changed files with 349 additions and 156 deletions

View File

@ -19,10 +19,22 @@ export function addImports(
extraStatements: ts.Statement[] = []): ts.SourceFile {
// Generate the import statements to prepend.
const addedImports = importManager.getAllImports(sf.fileName).map(i => {
const qualifier = ts.createIdentifier(i.qualifier);
let importClause: ts.ImportClause;
if (!i.isDefault) {
importClause = ts.createImportClause(
/* name */ undefined,
/* namedBindings */ ts.createNamespaceImport(qualifier));
} else {
importClause = ts.createImportClause(
/* name */ qualifier,
/* namedBindings */ undefined);
}
return ts.createImportDeclaration(
undefined, undefined,
ts.createImportClause(undefined, ts.createNamespaceImport(ts.createIdentifier(i.as))),
ts.createLiteral(i.name));
/* decorators */ undefined,
/* modifiers */ undefined,
/* importClause */ importClause,
/* moduleSpecifier */ ts.createLiteral(i.specifier));
});
// Filter out the existing imports and the source file body. All new statements