fix(ivy): force new imports for .d.ts files (#25080)

When ngtsc encounters a reference to a type (for example, a Component
type listed in an NgModule declarations array), it traces the import
of that type and attempts to determine the best way to refer to it.

In the event the type is defined in the same file where a reference
is being generated, the identifier of the type is used. If the type
was imported, ngtsc has a choice. It can use the identifier from the
original import, or it can write a new import to the module where the
type came from.

ngtsc has a bug currently when it elects to rely on the user's import.
When writing a .d.ts file, the user's import may have been elided as
the type was not referred to from the type side of the program. Thus,
in .d.ts files ngtsc must always assume the import may not exist, and
generate a new one.

In .js output the import is guaranteed to still exist, so it's
preferable for ngtsc to continue using the existing import if one is
available.

This commit changes how @angular/compiler writes type definitions, and
allows it to use a different expression to write a type definition than
is used to write the value. This allows ngtsc to specify that types in
type definitions should always be imported. A corresponding change to
the staticallyResolve() Reference system allows the choice of which
type of import to use when generating an Expression from a Reference.

PR Close #25080
This commit is contained in:
Alex Rickabaugh
2018-07-24 16:10:15 -07:00
committed by Igor Minar
parent f902b5ec59
commit ed7aa1c3e5
10 changed files with 136 additions and 34 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Expression, R3InjectorMetadata, R3NgModuleMetadata, WrappedNodeExpr, compileInjector, compileNgModule as compileR3NgModule, jitExpression} from '@angular/compiler';
import {Expression, R3InjectorMetadata, R3NgModuleMetadata, R3Reference, WrappedNodeExpr, compileInjector, compileNgModule as compileR3NgModule, jitExpression} from '@angular/compiler';
import {ModuleWithProviders, NgModule, NgModuleDefInternal, NgModuleTransitiveScopes} from '../../metadata/ng_module';
import {Type} from '../../type';
@ -28,11 +28,13 @@ export function compileNgModule(type: Type<any>, ngModule: NgModule): void {
const meta: R3NgModuleMetadata = {
type: wrap(type),
bootstrap: flatten(ngModule.bootstrap || EMPTY_ARRAY).map(wrap),
declarations: declarations.map(wrap),
imports:
flatten(ngModule.imports || EMPTY_ARRAY).map(expandModuleWithProviders).map(wrap),
exports:
flatten(ngModule.exports || EMPTY_ARRAY).map(expandModuleWithProviders).map(wrap),
declarations: declarations.map(wrapReference),
imports: flatten(ngModule.imports || EMPTY_ARRAY)
.map(expandModuleWithProviders)
.map(wrapReference),
exports: flatten(ngModule.exports || EMPTY_ARRAY)
.map(expandModuleWithProviders)
.map(wrapReference),
emitInline: true,
};
const res = compileR3NgModule(meta);
@ -210,6 +212,11 @@ function wrap(value: Type<any>): Expression {
return new WrappedNodeExpr(value);
}
function wrapReference(value: Type<any>): R3Reference {
const wrapped = wrap(value);
return {value: wrapped, type: wrapped};
}
function isModuleWithProviders(value: any): value is ModuleWithProviders {
return (value as{ngModule?: any}).ngModule !== undefined;
}