refactor(ivy): split type into type, internalType and adjacentType (#33533)

When compiling an Angular decorator (e.g. Directive), @angular/compiler
generates an 'expression' to be added as a static definition field
on the class, a 'type' which will be added for that field to the .d.ts
file, and a statement adjacent to the class that calls `setClassMetadata()`.

Previously, the same WrappedNodeExpr of the class' ts.Identifier was used
within each of this situations.

In the ngtsc case, this is proper. In the ngcc case, if the class being
compiled is within an ES5 IIFE, the outer name of the class may have
changed. Thus, the class has both an inner and outer name. The outer name
should continue to be used elsewhere in the compiler and in 'type'.

The 'expression' will live within the IIFE, the `internalType` should be used.
The adjacent statement will also live within the IIFE, the `adjacentType` should be used.

This commit introduces `ReflectionHost.getInternalNameOfClass()` and
`ReflectionHost.getAdjacentNameOfClass()`, which the compiler can use to
query for the correct name to use.

PR Close #33533
This commit is contained in:
Alex Rickabaugh
2019-11-01 16:55:09 +00:00
committed by atscott
parent d9a38928f5
commit 8d0de89ece
14 changed files with 116 additions and 25 deletions

View File

@ -8,7 +8,7 @@
import {Identifiers} from './identifiers';
import * as o from './output/output_ast';
import {R3DependencyMetadata, R3FactoryDelegateType, R3FactoryTarget, compileFactoryFunction} from './render3/r3_factory';
import {R3DependencyMetadata, R3FactoryDelegateType, R3FactoryMetadata, R3FactoryTarget, compileFactoryFunction} from './render3/r3_factory';
import {mapToMapExpression, typeWithParameters} from './render3/util';
export interface InjectableDef {
@ -20,6 +20,7 @@ export interface InjectableDef {
export interface R3InjectableMetadata {
name: string;
type: o.Expression;
internalType: o.Expression;
typeArgumentCount: number;
providedIn: o.Expression;
useClass?: o.Expression;
@ -32,9 +33,10 @@ export interface R3InjectableMetadata {
export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
let result: {factory: o.Expression, statements: o.Statement[]}|null = null;
const factoryMeta = {
const factoryMeta: R3FactoryMetadata = {
name: meta.name,
type: meta.type,
internalType: meta.internalType,
typeArgumentCount: meta.typeArgumentCount,
deps: [],
injectFn: Identifiers.inject,
@ -49,7 +51,7 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
// A special case exists for useClass: Type where Type is the injectable type itself and no
// deps are specified, in which case 'useClass' is effectively ignored.
const useClassOnSelf = meta.useClass.isEquivalent(meta.type);
const useClassOnSelf = meta.useClass.isEquivalent(meta.internalType);
let deps: R3DependencyMetadata[]|undefined = undefined;
if (meta.userDeps !== undefined) {
deps = meta.userDeps;
@ -97,10 +99,10 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
expression: o.importExpr(Identifiers.inject).callFn([meta.useExisting]),
});
} else {
result = delegateToFactory(meta.type);
result = delegateToFactory(meta.internalType);
}
const token = meta.type;
const token = meta.internalType;
const providedIn = meta.providedIn;
const expression = o.importExpr(Identifiers.ɵɵdefineInjectable).callFn([mapToMapExpression(
@ -118,7 +120,7 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
function delegateToFactory(type: o.Expression) {
return {
statements: [],
// () => meta.type.ɵfac(t)
// () => type.ɵfac(t)
factory: o.fn([new o.FnParam('t', o.DYNAMIC_TYPE)], [new o.ReturnStatement(type.callMethod(
'ɵfac', [o.variable('t')]))])
};