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:
@ -30,14 +30,19 @@ export interface R3ConstructorFactoryMetadata {
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* An expression representing the function (or constructor) which will instantiate the requested
|
||||
* type.
|
||||
*
|
||||
* This could be a reference to a constructor type, or to a user-defined factory function. The
|
||||
* `useNew` property determines whether it will be called as a constructor or not.
|
||||
* An expression representing the interface type being constructed.
|
||||
*/
|
||||
type: o.Expression;
|
||||
|
||||
/**
|
||||
* An expression representing the constructor type, intended for use within a class definition
|
||||
* itself.
|
||||
*
|
||||
* This can differ from the outer `type` if the class is being compiled by ngcc and is inside
|
||||
* an IIFE structure that uses a different name internally.
|
||||
*/
|
||||
internalType: o.Expression;
|
||||
|
||||
/** Number of arguments for the `type`. */
|
||||
typeArgumentCount: number;
|
||||
|
||||
@ -176,8 +181,9 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3FactoryFn {
|
||||
// parameter provided by the user (t) if specified, or the current type if not. If there is a
|
||||
// delegated factory (which is used to create the current type) then this is only the type-to-
|
||||
// create parameter (t).
|
||||
const typeForCtor =
|
||||
!isDelegatedMetadata(meta) ? new o.BinaryOperatorExpr(o.BinaryOperator.Or, t, meta.type) : t;
|
||||
const typeForCtor = !isDelegatedMetadata(meta) ?
|
||||
new o.BinaryOperatorExpr(o.BinaryOperator.Or, t, meta.internalType) :
|
||||
t;
|
||||
|
||||
let ctorExpr: o.Expression|null = null;
|
||||
if (meta.deps !== null) {
|
||||
@ -191,9 +197,8 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3FactoryFn {
|
||||
const baseFactory = o.variable(`ɵ${meta.name}_BaseFactory`);
|
||||
const getInheritedFactory = o.importExpr(R3.getInheritedFactory);
|
||||
const baseFactoryStmt =
|
||||
baseFactory.set(getInheritedFactory.callFn([meta.type])).toDeclStmt(o.INFERRED_TYPE, [
|
||||
o.StmtModifier.Exported, o.StmtModifier.Final
|
||||
]);
|
||||
baseFactory.set(getInheritedFactory.callFn([meta.internalType]))
|
||||
.toDeclStmt(o.INFERRED_TYPE, [o.StmtModifier.Exported, o.StmtModifier.Final]);
|
||||
statements.push(baseFactoryStmt);
|
||||
|
||||
// There is no constructor, use the base class' factory to construct typeForCtor.
|
||||
@ -220,7 +225,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3FactoryFn {
|
||||
if (isDelegatedMetadata(meta) && meta.delegateType === R3FactoryDelegateType.Factory) {
|
||||
const delegateFactory = o.variable(`ɵ${meta.name}_BaseFactory`);
|
||||
const getFactoryOf = o.importExpr(R3.getFactoryOf);
|
||||
if (meta.delegate.isEquivalent(meta.type)) {
|
||||
if (meta.delegate.isEquivalent(meta.internalType)) {
|
||||
throw new Error(`Illegal state: compiling factory that delegates to itself`);
|
||||
}
|
||||
const delegateFactoryStmt =
|
||||
|
@ -31,6 +31,24 @@ export interface R3NgModuleMetadata {
|
||||
*/
|
||||
type: o.Expression;
|
||||
|
||||
/**
|
||||
* An expression representing the module type being compiled, intended for use within a class
|
||||
* definition itself.
|
||||
*
|
||||
* This can differ from the outer `type` if the class is being compiled by ngcc and is inside
|
||||
* an IIFE structure that uses a different name internally.
|
||||
*/
|
||||
internalType: o.Expression;
|
||||
|
||||
/**
|
||||
* An expression intended for use by statements that are adjacent (i.e. tightly coupled) to but
|
||||
* not internal to a class definition.
|
||||
*
|
||||
* This can differ from the outer `type` if the class is being compiled by ngcc and is inside
|
||||
* an IIFE structure that uses a different name internally.
|
||||
*/
|
||||
adjacentType: o.Expression;
|
||||
|
||||
/**
|
||||
* An array of expressions representing the bootstrap components specified by the module.
|
||||
*/
|
||||
@ -77,6 +95,7 @@ export interface R3NgModuleMetadata {
|
||||
*/
|
||||
export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef {
|
||||
const {
|
||||
internalType,
|
||||
type: moduleType,
|
||||
bootstrap,
|
||||
declarations,
|
||||
@ -90,7 +109,7 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef {
|
||||
|
||||
const additionalStatements: o.Statement[] = [];
|
||||
const definitionMap = {
|
||||
type: moduleType
|
||||
type: internalType
|
||||
} as{
|
||||
type: o.Expression,
|
||||
bootstrap: o.Expression,
|
||||
@ -156,7 +175,7 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef {
|
||||
* symbols to become tree-shakeable.
|
||||
*/
|
||||
function generateSetNgModuleScopeCall(meta: R3NgModuleMetadata): o.Statement|null {
|
||||
const {type: moduleType, declarations, imports, exports, containsForwardDecls} = meta;
|
||||
const {adjacentType: moduleType, declarations, imports, exports, containsForwardDecls} = meta;
|
||||
|
||||
const scopeMap = {} as{
|
||||
declarations: o.Expression,
|
||||
@ -198,6 +217,7 @@ export interface R3InjectorDef {
|
||||
export interface R3InjectorMetadata {
|
||||
name: string;
|
||||
type: o.Expression;
|
||||
internalType: o.Expression;
|
||||
deps: R3DependencyMetadata[]|null;
|
||||
providers: o.Expression|null;
|
||||
imports: o.Expression[];
|
||||
@ -207,6 +227,7 @@ export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef {
|
||||
const result = compileFactoryFunction({
|
||||
name: meta.name,
|
||||
type: meta.type,
|
||||
internalType: meta.internalType,
|
||||
typeArgumentCount: 0,
|
||||
deps: meta.deps,
|
||||
injectFn: R3.inject,
|
||||
|
@ -27,6 +27,15 @@ export interface R3PipeMetadata {
|
||||
*/
|
||||
type: o.Expression;
|
||||
|
||||
/**
|
||||
* An expression representing the pipe being compiled, intended for use within a class definition
|
||||
* itself.
|
||||
*
|
||||
* This can differ from the outer `type` if the class is being compiled by ngcc and is inside an
|
||||
* IIFE structure that uses a different name internally.
|
||||
*/
|
||||
internalType: o.Expression;
|
||||
|
||||
/**
|
||||
* Number of generic type parameters of the type itself.
|
||||
*/
|
||||
@ -80,10 +89,12 @@ export function compilePipeFromRender2(
|
||||
return error(`Cannot resolve the name of ${pipe.type}`);
|
||||
}
|
||||
|
||||
const type = outputCtx.importExpr(pipe.type.reference);
|
||||
const metadata: R3PipeMetadata = {
|
||||
name,
|
||||
type,
|
||||
internalType: type,
|
||||
pipeName: pipe.name,
|
||||
type: outputCtx.importExpr(pipe.type.reference),
|
||||
typeArgumentCount: 0,
|
||||
deps: dependenciesFromGlobalMetadata(pipe.type, outputCtx, reflector),
|
||||
pure: pipe.pure,
|
||||
|
@ -28,6 +28,15 @@ export interface R3DirectiveMetadata {
|
||||
*/
|
||||
type: o.Expression;
|
||||
|
||||
/**
|
||||
* An expression representing a reference to the directive being compiled, intended for use within
|
||||
* a class definition itself.
|
||||
*
|
||||
* This can differ from the outer `type` if the class is being compiled by ngcc and is inside
|
||||
* an IIFE structure that uses a different name internally.
|
||||
*/
|
||||
internalType: o.Expression;
|
||||
|
||||
/**
|
||||
* Number of generic type parameters of the type itself.
|
||||
*/
|
||||
|
@ -45,7 +45,7 @@ function baseDirectiveFields(
|
||||
const selectors = core.parseSelectorToR3Selector(meta.selector);
|
||||
|
||||
// e.g. `type: MyDirective`
|
||||
definitionMap.set('type', meta.type);
|
||||
definitionMap.set('type', meta.internalType);
|
||||
|
||||
// e.g. `selectors: [['', 'someDir', '']]`
|
||||
if (selectors.length > 0) {
|
||||
|
Reference in New Issue
Block a user