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:
@ -274,6 +274,7 @@ export function extractDirectiveMetadata(
|
||||
outputs: {...outputsFromMeta, ...outputsFromFields}, queries, viewQueries, selector,
|
||||
fullInheritance: !!(flags & HandlerFlags.FULL_INHERITANCE),
|
||||
type: new WrappedNodeExpr(clazz.name),
|
||||
internalType: new WrappedNodeExpr(reflector.getInternalNameOfClass(clazz)),
|
||||
typeArgumentCount: reflector.getGenericArityOfClass(clazz) || 0,
|
||||
typeSourceSpan: EMPTY_SOURCE_SPAN, usesInheritance, exportAs, providers
|
||||
};
|
||||
|
@ -81,6 +81,7 @@ export class InjectableDecoratorHandler implements
|
||||
const factoryRes = compileNgFactoryDefField({
|
||||
name: meta.name,
|
||||
type: meta.type,
|
||||
internalType: meta.internalType,
|
||||
typeArgumentCount: meta.typeArgumentCount,
|
||||
deps: analysis.ctorDeps,
|
||||
injectFn: Identifiers.inject,
|
||||
@ -114,6 +115,7 @@ function extractInjectableMetadata(
|
||||
reflector: ReflectionHost): R3InjectableMetadata {
|
||||
const name = clazz.name.text;
|
||||
const type = new WrappedNodeExpr(clazz.name);
|
||||
const internalType = new WrappedNodeExpr(reflector.getInternalNameOfClass(clazz));
|
||||
const typeArgumentCount = reflector.getGenericArityOfClass(clazz) || 0;
|
||||
if (decorator.args === null) {
|
||||
throw new FatalDiagnosticError(
|
||||
@ -125,6 +127,7 @@ function extractInjectableMetadata(
|
||||
name,
|
||||
type,
|
||||
typeArgumentCount,
|
||||
internalType,
|
||||
providedIn: new LiteralExpr(null),
|
||||
};
|
||||
} else if (decorator.args.length === 1) {
|
||||
@ -159,6 +162,7 @@ function extractInjectableMetadata(
|
||||
name,
|
||||
type,
|
||||
typeArgumentCount,
|
||||
internalType,
|
||||
providedIn,
|
||||
useValue: new WrappedNodeExpr(unwrapForwardRef(meta.get('useValue') !, reflector)),
|
||||
};
|
||||
@ -167,6 +171,7 @@ function extractInjectableMetadata(
|
||||
name,
|
||||
type,
|
||||
typeArgumentCount,
|
||||
internalType,
|
||||
providedIn,
|
||||
useExisting: new WrappedNodeExpr(unwrapForwardRef(meta.get('useExisting') !, reflector)),
|
||||
};
|
||||
@ -175,6 +180,7 @@ function extractInjectableMetadata(
|
||||
name,
|
||||
type,
|
||||
typeArgumentCount,
|
||||
internalType,
|
||||
providedIn,
|
||||
useClass: new WrappedNodeExpr(unwrapForwardRef(meta.get('useClass') !, reflector)),
|
||||
userDeps,
|
||||
@ -186,11 +192,12 @@ function extractInjectableMetadata(
|
||||
name,
|
||||
type,
|
||||
typeArgumentCount,
|
||||
internalType,
|
||||
providedIn,
|
||||
useFactory: factory, userDeps,
|
||||
};
|
||||
} else {
|
||||
return {name, type, typeArgumentCount, providedIn};
|
||||
return {name, type, typeArgumentCount, internalType, providedIn};
|
||||
}
|
||||
} else {
|
||||
throw new FatalDiagnosticError(
|
||||
|
@ -28,7 +28,7 @@ export function generateSetClassMetadataCall(
|
||||
if (!reflection.isClass(clazz)) {
|
||||
return null;
|
||||
}
|
||||
const id = ts.updateIdentifier(clazz.name);
|
||||
const id = ts.updateIdentifier(reflection.getAdjacentNameOfClass(clazz));
|
||||
|
||||
// Reflect over the class decorators. If none are present, or those that are aren't from
|
||||
// Angular, then return null. Otherwise, turn them into metadata.
|
||||
|
@ -198,6 +198,8 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
|
||||
|
||||
const ngModuleDef: R3NgModuleMetadata = {
|
||||
type: new WrappedNodeExpr(node.name),
|
||||
internalType: new WrappedNodeExpr(this.reflector.getInternalNameOfClass(node)),
|
||||
adjacentType: new WrappedNodeExpr(this.reflector.getAdjacentNameOfClass(node)),
|
||||
bootstrap,
|
||||
declarations,
|
||||
exports,
|
||||
@ -227,6 +229,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
|
||||
const ngInjectorDef: R3InjectorMetadata = {
|
||||
name,
|
||||
type: new WrappedNodeExpr(node.name),
|
||||
internalType: new WrappedNodeExpr(this.reflector.getInternalNameOfClass(node)),
|
||||
deps: getValidConstructorDependencies(
|
||||
node, this.reflector, this.defaultImportRecorder, this.isCore),
|
||||
providers,
|
||||
|
@ -51,6 +51,7 @@ export class PipeDecoratorHandler implements DecoratorHandler<PipeHandlerData, D
|
||||
analyze(clazz: ClassDeclaration, decorator: Decorator): AnalysisOutput<PipeHandlerData> {
|
||||
const name = clazz.name.text;
|
||||
const type = new WrappedNodeExpr(clazz.name);
|
||||
const internalType = new WrappedNodeExpr(this.reflector.getInternalNameOfClass(clazz));
|
||||
if (decorator.args === null) {
|
||||
throw new FatalDiagnosticError(
|
||||
ErrorCode.DECORATOR_NOT_CALLED, Decorator.nodeForError(decorator),
|
||||
@ -97,6 +98,7 @@ export class PipeDecoratorHandler implements DecoratorHandler<PipeHandlerData, D
|
||||
meta: {
|
||||
name,
|
||||
type,
|
||||
internalType,
|
||||
typeArgumentCount: this.reflector.getGenericArityOfClass(clazz) || 0, pipeName,
|
||||
deps: getValidConstructorDependencies(
|
||||
clazz, this.reflector, this.defaultImportRecorder, this.isCore),
|
||||
|
Reference in New Issue
Block a user