fix(ivy): remove metadata from *Def and introduce *DefWithMeta types (#26203)
Previously in Ivy, metadata for directives/components/modules/etc was carried in .d.ts files inside type information encoded on the DirectiveDef, ComponentDef, NgModuleDef, etc types of Ivy definition fields. This works well, but has the side effect of complicating Ivy's runtime code as these extra generic type parameters had to be specified as <any> throughout the codebase. *DefInternal types were introduced previously to mitigate this issue, but that's the wrong way to solve the problem. This commit returns *Def types to their original form, with no metadata attached. Instead, new *DefWithMeta types are introduced that alias the plain definition types and add extra generic parameters. This way the only code that needs to deal with the extra metadata parameters is the compiler code that reads and writes them - the existence of this metadata is transparent to the runtime, as it should be. PR Close #26203
This commit is contained in:

committed by
Jason Aden

parent
b0070dfb9a
commit
79466baef8
@ -126,8 +126,8 @@ export class Identifiers {
|
||||
|
||||
static defineComponent: o.ExternalReference = {name: 'ɵdefineComponent', moduleName: CORE};
|
||||
|
||||
static ComponentDef: o.ExternalReference = {
|
||||
name: 'ɵComponentDef',
|
||||
static ComponentDefWithMeta: o.ExternalReference = {
|
||||
name: 'ɵComponentDefWithMeta',
|
||||
moduleName: CORE,
|
||||
};
|
||||
|
||||
@ -136,8 +136,8 @@ export class Identifiers {
|
||||
moduleName: CORE,
|
||||
};
|
||||
|
||||
static DirectiveDef: o.ExternalReference = {
|
||||
name: 'ɵDirectiveDef',
|
||||
static DirectiveDefWithMeta: o.ExternalReference = {
|
||||
name: 'ɵDirectiveDefWithMeta',
|
||||
moduleName: CORE,
|
||||
};
|
||||
|
||||
@ -151,14 +151,14 @@ export class Identifiers {
|
||||
moduleName: CORE,
|
||||
};
|
||||
|
||||
static NgModuleDef: o.ExternalReference = {
|
||||
name: 'ɵNgModuleDef',
|
||||
static NgModuleDefWithMeta: o.ExternalReference = {
|
||||
name: 'ɵNgModuleDefWithMeta',
|
||||
moduleName: CORE,
|
||||
};
|
||||
|
||||
static defineNgModule: o.ExternalReference = {name: 'ɵdefineNgModule', moduleName: CORE};
|
||||
|
||||
static PipeDef: o.ExternalReference = {name: 'ɵPipeDef', moduleName: CORE};
|
||||
static PipeDefWithMeta: o.ExternalReference = {name: 'ɵPipeDefWithMeta', moduleName: CORE};
|
||||
|
||||
static definePipe: o.ExternalReference = {name: 'ɵdefinePipe', moduleName: CORE};
|
||||
|
||||
|
@ -73,7 +73,7 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef {
|
||||
exports: o.literalArr(exports.map(ref => ref.value)),
|
||||
})]);
|
||||
|
||||
const type = new o.ExpressionType(o.importExpr(R3.NgModuleDef, [
|
||||
const type = new o.ExpressionType(o.importExpr(R3.NgModuleDefWithMeta, [
|
||||
new o.ExpressionType(moduleType), tupleTypeOf(declarations), tupleTypeOf(imports),
|
||||
tupleTypeOf(exports)
|
||||
]));
|
||||
|
@ -50,7 +50,7 @@ export function compilePipeFromMetadata(metadata: R3PipeMetadata) {
|
||||
definitionMapValues.push({key: 'pure', value: o.literal(metadata.pure), quoted: false});
|
||||
|
||||
const expression = o.importExpr(R3.definePipe).callFn([o.literalMap(definitionMapValues)]);
|
||||
const type = new o.ExpressionType(o.importExpr(R3.PipeDef, [
|
||||
const type = new o.ExpressionType(o.importExpr(R3.PipeDefWithMeta, [
|
||||
new o.ExpressionType(metadata.type),
|
||||
new o.ExpressionType(new o.LiteralExpr(metadata.pipeName)),
|
||||
]));
|
||||
|
@ -117,10 +117,7 @@ export function compileDirectiveFromMetadata(
|
||||
// string literal, which must be on one line.
|
||||
const selectorForType = (meta.selector || '').replace(/\n/g, '');
|
||||
|
||||
const type = new o.ExpressionType(o.importExpr(R3.DirectiveDef, [
|
||||
typeWithParameters(meta.type, meta.typeArgumentCount),
|
||||
new o.ExpressionType(o.literal(selectorForType))
|
||||
]));
|
||||
const type = createTypeForDef(meta, R3.DirectiveDefWithMeta);
|
||||
return {expression, type, statements};
|
||||
}
|
||||
|
||||
@ -257,10 +254,7 @@ export function compileComponentFromMetadata(
|
||||
const selectorForType = (meta.selector || '').replace(/\n/g, '');
|
||||
|
||||
const expression = o.importExpr(R3.defineComponent).callFn([definitionMap.toLiteralMap()]);
|
||||
const type = new o.ExpressionType(o.importExpr(R3.ComponentDef, [
|
||||
typeWithParameters(meta.type, meta.typeArgumentCount),
|
||||
new o.ExpressionType(o.literal(selectorForType))
|
||||
]));
|
||||
const type = createTypeForDef(meta, R3.ComponentDefWithMeta);
|
||||
|
||||
return {expression, type, statements};
|
||||
}
|
||||
@ -509,6 +503,39 @@ function createContentQueriesRefreshFunction(meta: R3DirectiveMetadata): o.Expre
|
||||
return null;
|
||||
}
|
||||
|
||||
function stringAsType(str: string): o.Type {
|
||||
return o.expressionType(o.literal(str));
|
||||
}
|
||||
|
||||
function stringMapAsType(map: {[key: string]: string}): o.Type {
|
||||
const mapValues = Object.keys(map).map(key => ({
|
||||
key,
|
||||
value: o.literal(map[key]),
|
||||
quoted: true,
|
||||
}));
|
||||
return o.expressionType(o.literalMap(mapValues));
|
||||
}
|
||||
|
||||
function stringArrayAsType(arr: string[]): o.Type {
|
||||
return arr.length > 0 ? o.expressionType(o.literalArr(arr.map(value => o.literal(value)))) :
|
||||
o.NONE_TYPE;
|
||||
}
|
||||
|
||||
function createTypeForDef(meta: R3DirectiveMetadata, typeBase: o.ExternalReference): o.Type {
|
||||
// On the type side, remove newlines from the selector as it will need to fit into a TypeScript
|
||||
// string literal, which must be on one line.
|
||||
const selectorForType = (meta.selector || '').replace(/\n/g, '');
|
||||
|
||||
return o.expressionType(o.importExpr(typeBase, [
|
||||
typeWithParameters(meta.type, meta.typeArgumentCount),
|
||||
stringAsType(selectorForType),
|
||||
meta.exportAs !== null ? stringAsType(meta.exportAs) : o.NONE_TYPE,
|
||||
stringMapAsType(meta.inputs),
|
||||
stringMapAsType(meta.outputs),
|
||||
stringArrayAsType(meta.queries.map(q => q.propertyName)),
|
||||
]));
|
||||
}
|
||||
|
||||
// Define and update any view queries
|
||||
function createViewQueriesFunction(
|
||||
meta: R3ComponentMetadata, constantPool: ConstantPool): o.Expression {
|
||||
|
Reference in New Issue
Block a user