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:
Alex Rickabaugh
2018-09-21 12:12:06 -07:00
committed by Jason Aden
parent b0070dfb9a
commit 79466baef8
42 changed files with 279 additions and 252 deletions

View File

@ -313,7 +313,7 @@ export class SelectorScopeRegistry {
return null;
} else if (
def.type === null || !ts.isTypeReferenceNode(def.type) ||
def.type.typeArguments === undefined || def.type.typeArguments.length !== 2) {
def.type.typeArguments === undefined || def.type.typeArguments.length < 2) {
// The type metadata was the wrong shape.
return null;
}
@ -337,7 +337,7 @@ export class SelectorScopeRegistry {
return null;
} else if (
def.type === null || !ts.isTypeReferenceNode(def.type) ||
def.type.typeArguments === undefined || def.type.typeArguments.length !== 2) {
def.type.typeArguments === undefined || def.type.typeArguments.length < 2) {
// The type metadata was the wrong shape.
return null;
}

View File

@ -20,7 +20,7 @@ describe('SelectorScopeRegistry', () => {
{
name: 'node_modules/@angular/core/index.d.ts',
contents: `
export interface NgComponentDef<A, B> {}
export interface NgComponentDefWithMeta<A, B, C, D, E, F> {}
export interface NgModuleDef<A, B, C, D> {}
`
},
@ -38,10 +38,10 @@ describe('SelectorScopeRegistry', () => {
{
name: 'node_modules/some_library/component.d.ts',
contents: `
import {NgComponentDef} from '@angular/core';
import {NgComponentDefWithMeta} from '@angular/core';
export declare class SomeCmp {
static ngComponentDef: NgComponentDef<SomeCmp, 'some-cmp'>;
static ngComponentDef: NgComponentDefWithMeta<SomeCmp, 'some-cmp', never, {}, {}, never>;
}
`
},
@ -84,21 +84,21 @@ describe('SelectorScopeRegistry', () => {
{
name: 'node_modules/@angular/core/index.d.ts',
contents: `
export interface NgComponentDef<A, B> {}
export interface NgComponentDefWithMeta<A, B, C, D, E, F> {}
export interface NgModuleDef<A, B, C, D> {}
`
},
{
name: 'node_modules/some_library/index.d.ts',
contents: `
import {NgComponentDef, NgModuleDef} from '@angular/core';
import {NgComponentDefWithMeta, NgModuleDef} from '@angular/core';
export declare class SomeModule {
static ngModuleDef: NgModuleDef<SomeModule, [typeof SomeCmp], never, [typeof SomeCmp]>;
}
export declare class SomeCmp {
static ngComponentDef: NgComponentDef<SomeCmp, 'some-cmp'>;
static ngComponentDef: NgComponentDefWithMeta<SomeCmp, 'some-cmp', never, {}, {}, never>;
}
`
},

View File

@ -45,7 +45,7 @@ const CORE_SUPPORTED_SYMBOLS = new Set<string>([
'inject',
'ɵInjectableDef',
'ɵInjectorDef',
'ɵNgModuleDef',
'ɵNgModuleDefWithMeta',
'ɵNgModuleFactory',
]);
@ -416,7 +416,16 @@ export class TypeTranslatorVisitor implements ExpressionVisitor, TypeVisitor {
}
visitLiteralMapExpr(ast: LiteralMapExpr, context: Context) {
throw new Error('Method not implemented.');
const entries = ast.entries.map(entry => {
const {key, quoted} = entry;
const value = entry.value.visitExpression(this, context);
if (quoted) {
return `'${key}': ${value}`;
} else {
return `${key}: ${value}`;
}
});
return `{${entries.join(', ')}}`;
}
visitCommaExpr(ast: CommaExpr, context: Context) { throw new Error('Method not implemented.'); }