feat(ivy): enable inheritance of factory functions in definitions (#25392)

This commit creates an API for factory functions which allows them
to be inherited from one another. To do so, it differentiates between
the factory function as a wrapper for a constructor and the factory
function in ngInjectableDefs which is determined by a default
provider.

The new form is:

factory: (t?) => new (t || SomeType)(inject(Dep1), inject(Dep2))

The 't' parameter allows for constructor inheritance. A subclass with
no declared constructor inherits its constructor from the superclass.
With the 't' parameter, a subclass can call the superclass' factory
function and use it to create an instance of the subclass.

For @Injectables with configured providers, the factory function is
of the form:

factory: (t?) => t ? constructorInject(t) : provider();

where constructorInject(t) creates an instance of 't' using the
naturally declared constructor of the type, and where provider()
creates an instance of the base type using the special declared
provider on @Injectable.

PR Close #25392
This commit is contained in:
Alex Rickabaugh
2018-07-16 16:36:31 -07:00
committed by Ben Lesh
parent fba276d3d1
commit 5be186035f
29 changed files with 451 additions and 205 deletions

View File

@ -127,7 +127,7 @@ describe('Renderer', () => {
.toBe(analyzedFile.analyzedClasses[0]);
expect(renderer.addDefinitions.calls.first().args[2])
.toEqual(
`A.ngDirectiveDef = ɵngcc0.ɵdefineDirective({ type: A, selectors: [["", "a", ""]], factory: function A_Factory() { return new A(); }, features: [ɵngcc0.ɵPublicFeature] });`);
`A.ngDirectiveDef = ɵngcc0.ɵdefineDirective({ type: A, selectors: [["", "a", ""]], factory: function A_Factory(t) { return new (t || A)(); }, features: [ɵngcc0.ɵPublicFeature] });`);
});
it('should call removeDecorators with the source code, a map of class decorators that have been analyzed',

View File

@ -38,7 +38,7 @@ export class InjectableDecoratorHandler implements DecoratorHandler<R3Injectable
return {
name: 'ngInjectableDef',
initializer: res.expression,
statements: [],
statements: res.statements,
type: res.type,
};
}
@ -56,6 +56,7 @@ function extractInjectableMetadata(
}
const name = clazz.name.text;
const type = new WrappedNodeExpr(clazz.name);
const ctorDeps = getConstructorDependencies(clazz, reflector, isCore);
if (decorator.args === null) {
throw new Error(`@Injectable must be called`);
}
@ -63,8 +64,7 @@ function extractInjectableMetadata(
return {
name,
type,
providedIn: new LiteralExpr(null),
deps: getConstructorDependencies(clazz, reflector, isCore),
providedIn: new LiteralExpr(null), ctorDeps,
};
} else if (decorator.args.length === 1) {
const metaNode = decorator.args[0];
@ -81,30 +81,49 @@ function extractInjectableMetadata(
if (meta.has('providedIn')) {
providedIn = new WrappedNodeExpr(meta.get('providedIn') !);
}
let userDeps: R3DependencyMetadata[]|undefined = undefined;
if ((meta.has('useClass') || meta.has('useFactory')) && meta.has('deps')) {
const depsExpr = meta.get('deps') !;
if (!ts.isArrayLiteralExpression(depsExpr)) {
throw new Error(`In Ivy, deps metadata must be inline.`);
}
if (depsExpr.elements.length > 0) {
throw new Error(`deps not yet supported`);
}
userDeps = depsExpr.elements.map(dep => getDep(dep, reflector));
}
if (meta.has('useValue')) {
return {name, type, providedIn, useValue: new WrappedNodeExpr(meta.get('useValue') !)};
return {
name,
type,
ctorDeps,
providedIn,
useValue: new WrappedNodeExpr(meta.get('useValue') !)
};
} else if (meta.has('useExisting')) {
return {name, type, providedIn, useExisting: new WrappedNodeExpr(meta.get('useExisting') !)};
return {
name,
type,
ctorDeps,
providedIn,
useExisting: new WrappedNodeExpr(meta.get('useExisting') !)
};
} else if (meta.has('useClass')) {
return {name, type, providedIn, useClass: new WrappedNodeExpr(meta.get('useClass') !)};
return {
name,
type,
ctorDeps,
providedIn,
useClass: new WrappedNodeExpr(meta.get('useClass') !), userDeps
};
} else if (meta.has('useFactory')) {
// useFactory is special - the 'deps' property must be analyzed.
const factory = new WrappedNodeExpr(meta.get('useFactory') !);
const deps: R3DependencyMetadata[] = [];
if (meta.has('deps')) {
const depsExpr = meta.get('deps') !;
if (!ts.isArrayLiteralExpression(depsExpr)) {
throw new Error(`In Ivy, deps metadata must be inline.`);
}
if (depsExpr.elements.length > 0) {
throw new Error(`deps not yet supported`);
}
deps.push(...depsExpr.elements.map(dep => getDep(dep, reflector)));
}
return {name, type, providedIn, useFactory: factory, deps};
return {name, type, providedIn, useFactory: factory, ctorDeps, userDeps};
} else {
const deps = getConstructorDependencies(clazz, reflector, isCore);
return {name, type, providedIn, deps};
return {name, type, providedIn, ctorDeps};
}
} else {
throw new Error(`Too many arguments to @Injectable`);

View File

@ -13,10 +13,17 @@ import {Decorator, ReflectionHost} from '../../host';
import {AbsoluteReference, ImportMode, Reference} from '../../metadata';
export function getConstructorDependencies(
clazz: ts.ClassDeclaration, reflector: ReflectionHost,
isCore: boolean): R3DependencyMetadata[] {
clazz: ts.ClassDeclaration, reflector: ReflectionHost, isCore: boolean): R3DependencyMetadata[]|
null {
const useType: R3DependencyMetadata[] = [];
const ctorParams = reflector.getConstructorParameters(clazz) || [];
let ctorParams = reflector.getConstructorParameters(clazz);
if (ctorParams === null) {
if (reflector.hasBaseClass(clazz)) {
return null;
} else {
ctorParams = [];
}
}
ctorParams.forEach((param, idx) => {
let tokenExpr = param.type;
let optional = false, self = false, skipSelf = false, host = false;

View File

@ -338,4 +338,6 @@ export interface ReflectionHost {
* Check whether the given declaration node actually represents a class.
*/
isClass(node: ts.Declaration): boolean;
hasBaseClass(node: ts.Declaration): boolean;
}

View File

@ -132,6 +132,11 @@ export class TypeScriptReflectionHost implements ReflectionHost {
return ts.isClassDeclaration(node);
}
hasBaseClass(node: ts.Declaration): boolean {
return ts.isClassDeclaration(node) && node.heritageClauses !== undefined &&
node.heritageClauses.some(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
}
getDeclarationOfIdentifier(id: ts.Identifier): Declaration|null {
// Resolve the identifier to a Symbol, and return the declaration of that.
let symbol: ts.Symbol|undefined = this.checker.getSymbolAtLocation(id);

View File

@ -72,7 +72,7 @@ class IvyVisitor extends Visitor {
node.modifiers, node.name, node.typeParameters, node.heritageClauses || [],
// Map over the class members and remove any Angular decorators from them.
members.map(member => this._stripAngularDecorators(member)));
return {node, before: statements};
return {node, after: statements};
}
return {node};

View File

@ -14,7 +14,8 @@ import * as ts from 'typescript';
*/
export type VisitListEntryResult<B extends ts.Node, T extends B> = {
node: T,
before?: B[]
before?: B[],
after?: B[],
};
/**
@ -35,6 +36,11 @@ export abstract class Visitor {
*/
private _before = new Map<ts.Node, ts.Statement[]>();
/**
* Maps statements to an array of statements that should be inserted after them.
*/
private _after = new Map<ts.Node, ts.Statement[]>();
/**
* Visit a class declaration, returning at least the transformed declaration and optionally other
* nodes to insert before the declaration.
@ -52,6 +58,10 @@ export abstract class Visitor {
// parent's _visit call is responsible for performing this insertion.
this._before.set(result.node, result.before);
}
if (result.after !== undefined) {
// Same with nodes that should be inserted after.
this._after.set(result.node, result.after);
}
return result.node;
}
@ -88,8 +98,9 @@ export abstract class Visitor {
private _maybeProcessStatements<T extends ts.Node&{statements: ts.NodeArray<ts.Statement>}>(
node: T): T {
// Shortcut - if every statement doesn't require nodes to be prepended, this is a no-op.
if (node.statements.every(stmt => !this._before.has(stmt))) {
// Shortcut - if every statement doesn't require nodes to be prepended or appended,
// this is a no-op.
if (node.statements.every(stmt => !this._before.has(stmt) && !this._after.has(stmt))) {
return node;
}
@ -104,6 +115,10 @@ export abstract class Visitor {
this._before.delete(stmt);
}
newStatements.push(stmt);
if (this._after.has(stmt)) {
newStatements.push(...(this._after.get(stmt) !as ts.Statement[]));
this._after.delete(stmt);
}
});
clone.statements = ts.createNodeArray(newStatements, node.statements.hasTrailingComma);
return clone;

View File

@ -42,7 +42,8 @@ describe('compiler compliance', () => {
};
// The factory should look like this:
const factory = 'factory: function MyComponent_Factory() { return new MyComponent(); }';
const factory =
'factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
// The template should look like this (where IDENT is a wild card for an identifier):
const template = `
@ -93,7 +94,8 @@ describe('compiler compliance', () => {
};
// The factory should look like this:
const factory = 'factory: function MyComponent_Factory() { return new MyComponent(); }';
const factory =
'factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
// The template should look like this (where IDENT is a wild card for an identifier):
const template = `
@ -143,7 +145,8 @@ describe('compiler compliance', () => {
};
// The factory should look like this:
const factory = 'factory: function MyComponent_Factory() { return new MyComponent(); }';
const factory =
'factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
// The template should look like this (where IDENT is a wild card for an identifier):
const template = `
@ -192,7 +195,8 @@ describe('compiler compliance', () => {
};
// The factory should look like this:
const factory = 'factory: function MyComponent_Factory() { return new MyComponent(); }';
const factory =
'factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
// The template should look like this (where IDENT is a wild card for an identifier):
const template = `
@ -238,7 +242,8 @@ describe('compiler compliance', () => {
}
};
const factory = 'factory: function MyComponent_Factory() { return new MyComponent(); }';
const factory =
'factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
const template = `
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
@ -282,7 +287,8 @@ describe('compiler compliance', () => {
}
};
const factory = 'factory: function MyComponent_Factory() { return new MyComponent(); }';
const factory =
'factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
const template = `
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
@ -327,14 +333,15 @@ describe('compiler compliance', () => {
}
};
const factory = 'factory: function MyComponent_Factory() { return new MyComponent(); }';
const factory =
'factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
const template = `
const _c0 = ["error"];
const _c1 = ["background-color"];
MyComponent.ngComponentDef = i0.ɵdefineComponent({type:MyComponent,selectors:[["my-component"]],
factory: function MyComponent_Factory(){
return new MyComponent();
factory: function MyComponent_Factory(t){
return new (t || MyComponent)();
},
features: [$r3$.ɵPublicFeature],
template:function MyComponent_Template(rf,ctx){
@ -388,7 +395,7 @@ describe('compiler compliance', () => {
ChildComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: ChildComponent,
selectors: [["child"]],
factory: function ChildComponent_Factory() { return new ChildComponent(); },
factory: function ChildComponent_Factory(t) { return new (t || ChildComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function ChildComponent_Template(rf, ctx) {
if (rf & 1) {
@ -402,7 +409,7 @@ describe('compiler compliance', () => {
SomeDirective.ngDirectiveDef = $r3$.ɵdefineDirective({
type: SomeDirective,
selectors: [["", "some-directive", ""]],
factory: function SomeDirective_Factory() {return new SomeDirective(); },
factory: function SomeDirective_Factory(t) {return new (t || SomeDirective)(); },
features: [$r3$.ɵPublicFeature]
});
`;
@ -414,7 +421,7 @@ describe('compiler compliance', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
@ -458,7 +465,7 @@ describe('compiler compliance', () => {
SomeDirective.ngDirectiveDef = $r3$.ɵdefineDirective({
type: SomeDirective,
selectors: [["div", "some-directive", "", 8, "foo", 3, "title", "", 9, "baz"]],
factory: function SomeDirective_Factory() {return new SomeDirective(); },
factory: function SomeDirective_Factory(t) {return new (t || SomeDirective)(); },
features: [$r3$.ɵPublicFeature]
});
`;
@ -468,7 +475,7 @@ describe('compiler compliance', () => {
OtherDirective.ngDirectiveDef = $r3$.ɵdefineDirective({
type: OtherDirective,
selectors: [["", 5, "span", "title", "", 9, "baz"]],
factory: function OtherDirective_Factory() {return new OtherDirective(); },
factory: function OtherDirective_Factory(t) {return new (t || OtherDirective)(); },
features: [$r3$.ɵPublicFeature]
});
`;
@ -501,7 +508,7 @@ describe('compiler compliance', () => {
HostBindingDir.ngDirectiveDef = $r3$.ɵdefineDirective({
type: HostBindingDir,
selectors: [["", "hostBindingDir", ""]],
factory: function HostBindingDir_Factory() { return new HostBindingDir(); },
factory: function HostBindingDir_Factory(t) { return new (t || HostBindingDir)(); },
hostBindings: function HostBindingDir_HostBindings(dirIndex, elIndex) {
$r3$.ɵp(elIndex, "id", $r3$.ɵb($r3$.ɵd(dirIndex).dirId));
},
@ -544,7 +551,7 @@ describe('compiler compliance', () => {
IfDirective.ngDirectiveDef = $r3$.ɵdefineDirective({
type: IfDirective,
selectors: [["", "if", ""]],
factory: function IfDirective_Factory() { return new IfDirective($r3$.ɵinjectTemplateRef()); },
factory: function IfDirective_Factory(t) { return new (t || IfDirective)($r3$.ɵinjectTemplateRef()); },
features: [$r3$.ɵPublicFeature]
});`;
const MyComponentDefinition = `
@ -566,7 +573,7 @@ describe('compiler compliance', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
@ -626,7 +633,7 @@ describe('compiler compliance', () => {
MyApp.ngComponentDef = $r3$.ɵdefineComponent({
type: MyApp,
selectors: [["my-app"]],
factory: function MyApp_Factory() { return new MyApp(); },
factory: function MyApp_Factory(t) { return new (t || MyApp)(); },
features: [$r3$.ɵPublicFeature],
template: function MyApp_Template(rf, ctx) {
if (rf & 1) {
@ -706,7 +713,7 @@ describe('compiler compliance', () => {
MyApp.ngComponentDef = $r3$.ɵdefineComponent({
type: MyApp,
selectors: [["my-app"]],
factory: function MyApp_Factory() { return new MyApp(); },
factory: function MyApp_Factory(t) { return new (t || MyApp)(); },
features: [$r3$.ɵPublicFeature],
template: function MyApp_Template(rf, ctx) {
if (rf & 1) {
@ -768,7 +775,7 @@ describe('compiler compliance', () => {
MyApp.ngComponentDef = $r3$.ɵdefineComponent({
type: MyApp,
selectors: [["my-app"]],
factory: function MyApp_Factory() { return new MyApp(); },
factory: function MyApp_Factory(t) { return new (t || MyApp)(); },
features: [$r3$.ɵPublicFeature],
template: function MyApp_Template(rf, ctx) {
if (rf & 1) {
@ -834,7 +841,7 @@ describe('compiler compliance', () => {
MyApp.ngComponentDef = $r3$.ɵdefineComponent({
type: MyApp,
selectors: [["my-app"]],
factory: function MyApp_Factory() { return new MyApp(); },
factory: function MyApp_Factory(t) { return new (t || MyApp)(); },
features: [$r3$.ɵPublicFeature],
template: function MyApp_Template(rf, ctx) {
if (rf & 1) {
@ -892,7 +899,7 @@ describe('compiler compliance', () => {
SimpleComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: SimpleComponent,
selectors: [["simple"]],
factory: function SimpleComponent_Factory() { return new SimpleComponent(); },
factory: function SimpleComponent_Factory(t) { return new (t || SimpleComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function SimpleComponent_Template(rf, ctx) {
if (rf & 1) {
@ -913,7 +920,7 @@ describe('compiler compliance', () => {
ComplexComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: ComplexComponent,
selectors: [["complex"]],
factory: function ComplexComponent_Factory() { return new ComplexComponent(); },
factory: function ComplexComponent_Factory(t) { return new (t || ComplexComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function ComplexComponent_Template(rf, ctx) {
if (rf & 1) {
@ -979,7 +986,7 @@ describe('compiler compliance', () => {
ViewQueryComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: ViewQueryComponent,
selectors: [["view-query-component"]],
factory: function ViewQueryComponent_Factory() { return new ViewQueryComponent(); },
factory: function ViewQueryComponent_Factory(t) { return new (t || ViewQueryComponent)(); },
features: [$r3$.ɵPublicFeature],
viewQuery: function ViewQueryComponent_Query(rf, ctx) {
if (rf & 1) {
@ -1043,8 +1050,8 @@ describe('compiler compliance', () => {
ContentQueryComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: ContentQueryComponent,
selectors: [["content-query-component"]],
factory: function ContentQueryComponent_Factory() {
return new ContentQueryComponent();
factory: function ContentQueryComponent_Factory(t) {
return new (t || ContentQueryComponent)();
},
contentQueries: function ContentQueryComponent_ContentQueries() {
$r3$.ɵQr($r3$.ɵQ(null, SomeDirective, true));
@ -1119,7 +1126,7 @@ describe('compiler compliance', () => {
MyPipe.ngPipeDef = $r3$.ɵdefinePipe({
name: "myPipe",
type: MyPipe,
factory: function MyPipe_Factory() { return new MyPipe(); },
factory: function MyPipe_Factory(t) { return new (t || MyPipe)(); },
pure: false
});
`;
@ -1128,7 +1135,7 @@ describe('compiler compliance', () => {
MyPurePipe.ngPipeDef = $r3$.ɵdefinePipe({
name: "myPurePipe",
type: MyPurePipe,
factory: function MyPurePipe_Factory() { return new MyPurePipe(); },
factory: function MyPurePipe_Factory(t) { return new (t || MyPurePipe)(); },
pure: true
});`;
@ -1140,7 +1147,7 @@ describe('compiler compliance', () => {
MyApp.ngComponentDef = $r3$.ɵdefineComponent({
type: MyApp,
selectors: [["my-app"]],
factory: function MyApp_Factory() { return new MyApp(); },
factory: function MyApp_Factory(t) { return new (t || MyApp)(); },
features: [$r3$.ɵPublicFeature],
template: function MyApp_Template(rf, ctx) {
if (rf & 1) {
@ -1191,7 +1198,7 @@ describe('compiler compliance', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
@ -1283,7 +1290,7 @@ describe('compiler compliance', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
@ -1426,7 +1433,7 @@ describe('compiler compliance', () => {
LifecycleComp.ngComponentDef = $r3$.ɵdefineComponent({
type: LifecycleComp,
selectors: [["lifecycle-comp"]],
factory: function LifecycleComp_Factory() { return new LifecycleComp(); },
factory: function LifecycleComp_Factory(t) { return new (t || LifecycleComp)(); },
inputs: {nameMin: "name"},
features: [$r3$.ɵPublicFeature, $r3$.ɵNgOnChangesFeature],
template: function LifecycleComp_Template(rf, ctx) {}
@ -1436,7 +1443,7 @@ describe('compiler compliance', () => {
SimpleLayout.ngComponentDef = $r3$.ɵdefineComponent({
type: SimpleLayout,
selectors: [["simple-layout"]],
factory: function SimpleLayout_Factory() { return new SimpleLayout(); },
factory: function SimpleLayout_Factory(t) { return new (t || SimpleLayout)(); },
features: [$r3$.ɵPublicFeature],
template: function SimpleLayout_Template(rf, ctx) {
if (rf & 1) {
@ -1542,8 +1549,8 @@ describe('compiler compliance', () => {
ForOfDirective.ngDirectiveDef = $r3$.ɵdefineDirective({
type: ForOfDirective,
selectors: [["", "forOf", ""]],
factory: function ForOfDirective_Factory() {
return new ForOfDirective($r3$.ɵinjectViewContainerRef(), $r3$.ɵinjectTemplateRef());
factory: function ForOfDirective_Factory(t) {
return new (t || ForOfDirective)($r3$.ɵinjectViewContainerRef(), $r3$.ɵinjectTemplateRef());
},
features: [$r3$.ɵPublicFeature, $r3$.ɵNgOnChangesFeature],
inputs: {forOf: "forOf"}
@ -1564,7 +1571,7 @@ describe('compiler compliance', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx){
if (rf & 1) {
@ -1616,8 +1623,8 @@ describe('compiler compliance', () => {
ForOfDirective.ngDirectiveDef = $r3$.ɵdefineDirective({
type: ForOfDirective,
selectors: [["", "forOf", ""]],
factory: function ForOfDirective_Factory() {
return new ForOfDirective($r3$.ɵinjectViewContainerRef(), $r3$.ɵinjectTemplateRef());
factory: function ForOfDirective_Factory(t) {
return new (t || ForOfDirective)($r3$.ɵinjectViewContainerRef(), $r3$.ɵinjectTemplateRef());
},
features: [$r3$.ɵPublicFeature, $r3$.ɵNgOnChangesFeature],
inputs: {forOf: "forOf"}
@ -1641,7 +1648,7 @@ describe('compiler compliance', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {
@ -1739,7 +1746,7 @@ describe('compiler compliance', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {

View File

@ -48,8 +48,8 @@ describe('compiler compliance: dependency injection', () => {
};
const factory = `
factory: function MyComponent_Factory() {
return new MyComponent(
factory: function MyComponent_Factory(t) {
return new (t || MyComponent)(
$r3$.ɵinjectAttribute('name'),
$r3$.ɵdirectiveInject(MyService),
$r3$.ɵdirectiveInject(MyService, 1),

View File

@ -152,7 +152,7 @@ describe('compiler compliance: listen()', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() { return new MyComponent(); },
factory: function MyComponent_Factory(t) { return new (t || MyComponent)(); },
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
if (rf & 1) {

View File

@ -90,8 +90,8 @@ describe('compiler compliance: styling', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors:[["my-component"]],
factory:function MyComponent_Factory(){
return new MyComponent();
factory:function MyComponent_Factory(t){
return new (t || MyComponent)();
},
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, $ctx$) {
@ -147,8 +147,8 @@ describe('compiler compliance: styling', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors: [["my-component"]],
factory: function MyComponent_Factory() {
return new MyComponent();
factory: function MyComponent_Factory(t) {
return new (t || MyComponent)();
},
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, ctx) {
@ -242,8 +242,8 @@ describe('compiler compliance: styling', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors:[["my-component"]],
factory:function MyComponent_Factory(){
return new MyComponent();
factory:function MyComponent_Factory(t){
return new (t || MyComponent)();
},
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, $ctx$) {
@ -296,8 +296,8 @@ describe('compiler compliance: styling', () => {
MyComponent.ngComponentDef = $r3$.ɵdefineComponent({
type: MyComponent,
selectors:[["my-component"]],
factory:function MyComponent_Factory(){
return new MyComponent();
factory:function MyComponent_Factory(t){
return new (t || MyComponent)();
},
features: [$r3$.ɵPublicFeature],
template: function MyComponent_Template(rf, $ctx$) {

View File

@ -237,7 +237,7 @@ describe('ngtsc behavioral tests', () => {
expect(jsContents)
.toContain(
`TestModule.ngInjectorDef = i0.defineInjector({ factory: ` +
`function TestModule_Factory() { return new TestModule(); }, providers: [{ provide: ` +
`function TestModule_Factory(t) { return new (t || TestModule)(); }, providers: [{ provide: ` +
`Token, useValue: 'test' }], imports: [[OtherModule]] });`);
const dtsContents = getContents('test.d.ts');
@ -328,7 +328,7 @@ describe('ngtsc behavioral tests', () => {
expect(jsContents)
.toContain(
'TestPipe.ngPipeDef = i0.ɵdefinePipe({ name: "test-pipe", type: TestPipe, ' +
'factory: function TestPipe_Factory() { return new TestPipe(); }, pure: false })');
'factory: function TestPipe_Factory(t) { return new (t || TestPipe)(); }, pure: false })');
expect(dtsContents).toContain('static ngPipeDef: i0.ɵPipeDef<TestPipe, \'test-pipe\'>;');
});
@ -353,7 +353,7 @@ describe('ngtsc behavioral tests', () => {
expect(jsContents)
.toContain(
'TestPipe.ngPipeDef = i0.ɵdefinePipe({ name: "test-pipe", type: TestPipe, ' +
'factory: function TestPipe_Factory() { return new TestPipe(); }, pure: true })');
'factory: function TestPipe_Factory(t) { return new (t || TestPipe)(); }, pure: true })');
expect(dtsContents).toContain('static ngPipeDef: i0.ɵPipeDef<TestPipe, \'test-pipe\'>;');
});
@ -378,7 +378,7 @@ describe('ngtsc behavioral tests', () => {
expect(exitCode).toBe(0);
const jsContents = getContents('test.js');
expect(jsContents).toContain('return new TestPipe(i0.ɵdirectiveInject(Dep));');
expect(jsContents).toContain('return new (t || TestPipe)(i0.ɵdirectiveInject(Dep));');
});
it('should include @Pipes in @NgModule scopes', () => {
@ -474,7 +474,7 @@ describe('ngtsc behavioral tests', () => {
const jsContents = getContents('test.js');
expect(jsContents)
.toContain(
`factory: function FooCmp_Factory() { return new FooCmp(i0.ɵinjectAttribute("test"), i0.ɵinjectChangeDetectorRef(), i0.ɵinjectElementRef(), i0.ɵdirectiveInject(i0.INJECTOR), i0.ɵinjectTemplateRef(), i0.ɵinjectViewContainerRef()); }`);
`factory: function FooCmp_Factory(t) { return new (t || FooCmp)(i0.ɵinjectAttribute("test"), i0.ɵinjectChangeDetectorRef(), i0.ɵinjectElementRef(), i0.ɵdirectiveInject(i0.INJECTOR), i0.ɵinjectTemplateRef(), i0.ɵinjectViewContainerRef()); }`);
});
it('should generate queries for components', () => {
@ -657,4 +657,42 @@ describe('ngtsc behavioral tests', () => {
expect(errorSpy).not.toHaveBeenCalled();
expect(exitCode).toBe(0);
});
it('generates inherited factory definitions', () => {
writeConfig();
write(`test.ts`, `
import {Injectable} from '@angular/core';
class Dep {}
@Injectable()
class Base {
constructor(dep: Dep) {}
}
@Injectable()
class Child extends Base {}
@Injectable()
class GrandChild extends Child {
constructor() {
super(null!);
}
}
`);
const exitCode = main(['-p', basePath], errorSpy);
expect(errorSpy).not.toHaveBeenCalled();
expect(exitCode).toBe(0);
const jsContents = getContents('test.js');
expect(jsContents)
.toContain('function Base_Factory(t) { return new (t || Base)(i0.inject(Dep)); }');
expect(jsContents).toContain('var ɵChild_BaseFactory = i0.ɵgetInheritedFactory(Child)');
expect(jsContents)
.toContain('function Child_Factory(t) { return ɵChild_BaseFactory((t || Child)); }');
expect(jsContents)
.toContain('function GrandChild_Factory(t) { return new (t || GrandChild)(); }');
});
});