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

@ -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;