fix(compiler): allow to use pipes inside of `*ngIf` (#10452)

Fixes #9746
This commit is contained in:
Tobias Bosch 2016-08-02 10:34:41 -07:00
parent 91c64d2b8d
commit 8efbcc996a
4 changed files with 17 additions and 29 deletions

View File

@ -38,6 +38,10 @@ export class CompWithProviders {
export class CompWithReferences { export class CompWithReferences {
} }
@Component({selector: 'cmp-pipes', template: `<div *ngIf>{{test | somePipe}}</div>`})
export class CompUsingPipes {
}
@Component({ @Component({
selector: 'cmp-custom-els', selector: 'cmp-custom-els',
template: ` template: `

View File

@ -13,7 +13,7 @@ import {BrowserModule} from '@angular/platform-browser';
import {AnimateCmp} from './animate'; import {AnimateCmp} from './animate';
import {BasicComp} from './basic'; import {BasicComp} from './basic';
import {CompWithAnalyzeEntryComponentsProvider, CompWithEntryComponents} from './entry_components'; import {CompWithAnalyzeEntryComponentsProvider, CompWithEntryComponents} from './entry_components';
import {CompWithProviders, CompWithReferences, ModuleUsingCustomElements} from './features'; import {CompUsingPipes, CompWithProviders, CompWithReferences, ModuleUsingCustomElements} from './features';
import {CompUsingRootModuleDirectiveAndPipe, SomeDirectiveInRootModule, SomePipeInRootModule, SomeService, someLibModuleWithProviders} from './module_fixtures'; import {CompUsingRootModuleDirectiveAndPipe, SomeDirectiveInRootModule, SomePipeInRootModule, SomeService, someLibModuleWithProviders} from './module_fixtures';
import {ProjectingComp} from './projection'; import {ProjectingComp} from './projection';
import {CompWithChildQuery, CompWithDirectiveChild} from './queries'; import {CompWithChildQuery, CompWithDirectiveChild} from './queries';
@ -23,7 +23,7 @@ import {CompWithChildQuery, CompWithDirectiveChild} from './queries';
SomeDirectiveInRootModule, SomePipeInRootModule, AnimateCmp, BasicComp, CompWithEntryComponents, SomeDirectiveInRootModule, SomePipeInRootModule, AnimateCmp, BasicComp, CompWithEntryComponents,
CompWithAnalyzeEntryComponentsProvider, ProjectingComp, CompWithChildQuery, CompWithAnalyzeEntryComponentsProvider, ProjectingComp, CompWithChildQuery,
CompWithDirectiveChild, CompUsingRootModuleDirectiveAndPipe, CompWithProviders, CompWithDirectiveChild, CompUsingRootModuleDirectiveAndPipe, CompWithProviders,
CompWithReferences CompWithReferences, CompUsingPipes
], ],
imports: [BrowserModule, FormsModule, someLibModuleWithProviders(), ModuleUsingCustomElements], imports: [BrowserModule, FormsModule, someLibModuleWithProviders(), ModuleUsingCustomElements],
providers: [SomeService], providers: [SomeService],

View File

@ -15,10 +15,6 @@ import * as o from '../output/output_ast';
import {CompileView} from './compile_view'; import {CompileView} from './compile_view';
import {createPureProxy, getPropertyInView, injectFromViewParentInjector} from './util'; import {createPureProxy, getPropertyInView, injectFromViewParentInjector} from './util';
class _PurePipeProxy {
constructor(public view: CompileView, public instance: o.ReadPropExpr, public argCount: number) {}
}
export class CompilePipe { export class CompilePipe {
static call(view: CompileView, name: string, args: o.Expression[]): o.Expression { static call(view: CompileView, name: string, args: o.Expression[]): o.Expression {
var compView = view.componentView; var compView = view.componentView;
@ -41,15 +37,10 @@ export class CompilePipe {
} }
instance: o.ReadPropExpr; instance: o.ReadPropExpr;
private _purePipeProxies: _PurePipeProxy[] = []; private _purePipeProxyCount = 0;
constructor(public view: CompileView, public meta: CompilePipeMetadata) { constructor(public view: CompileView, public meta: CompilePipeMetadata) {
this.instance = o.THIS_EXPR.prop(`_pipe_${meta.name}_${view.pipeCount++}`); this.instance = o.THIS_EXPR.prop(`_pipe_${meta.name}_${view.pipeCount++}`);
}
get pure(): boolean { return this.meta.pure; }
create(): void {
var deps = this.meta.type.diDeps.map((diDep) => { var deps = this.meta.type.diDeps.map((diDep) => {
if (diDep.token.equalsTo(identifierToken(Identifiers.ChangeDetectorRef))) { if (diDep.token.equalsTo(identifierToken(Identifiers.ChangeDetectorRef))) {
return getPropertyInView(o.THIS_EXPR.prop('ref'), this.view, this.view.componentView); return getPropertyInView(o.THIS_EXPR.prop('ref'), this.view, this.view.componentView);
@ -61,28 +52,22 @@ export class CompilePipe {
this.view.createMethod.addStmt(o.THIS_EXPR.prop(this.instance.name) this.view.createMethod.addStmt(o.THIS_EXPR.prop(this.instance.name)
.set(o.importExpr(this.meta.type).instantiate(deps)) .set(o.importExpr(this.meta.type).instantiate(deps))
.toStmt()); .toStmt());
this._purePipeProxies.forEach((purePipeProxy) => {
var pipeInstanceSeenFromPureProxy =
getPropertyInView(this.instance, purePipeProxy.view, this.view);
createPureProxy(
pipeInstanceSeenFromPureProxy.prop('transform')
.callMethod(o.BuiltinMethod.bind, [pipeInstanceSeenFromPureProxy]),
purePipeProxy.argCount, purePipeProxy.instance, purePipeProxy.view);
});
} }
get pure(): boolean { return this.meta.pure; }
private _call(callingView: CompileView, args: o.Expression[]): o.Expression { private _call(callingView: CompileView, args: o.Expression[]): o.Expression {
if (this.meta.pure) { if (this.meta.pure) {
// PurePipeProxies live on the view that called them. // PurePipeProxies live on the view that called them.
var purePipeProxy = new _PurePipeProxy( var purePipeProxyInstance =
callingView, o.THIS_EXPR.prop(`${this.instance.name}_${this._purePipeProxies.length}`), o.THIS_EXPR.prop(`${this.instance.name}_${this._purePipeProxyCount++}`);
args.length); var pipeInstanceSeenFromPureProxy = getPropertyInView(this.instance, callingView, this.view);
this._purePipeProxies.push(purePipeProxy); createPureProxy(
pipeInstanceSeenFromPureProxy.prop('transform')
.callMethod(o.BuiltinMethod.bind, [pipeInstanceSeenFromPureProxy]),
args.length, purePipeProxyInstance, callingView);
return o.importExpr(Identifiers.castByValue) return o.importExpr(Identifiers.castByValue)
.callFn([ .callFn([purePipeProxyInstance, pipeInstanceSeenFromPureProxy.prop('transform')])
purePipeProxy.instance,
getPropertyInView(this.instance.prop('transform'), callingView, this.view)
])
.callFn(args); .callFn(args);
} else { } else {
return getPropertyInView(this.instance, callingView, this.view).callMethod('transform', args); return getPropertyInView(this.instance, callingView, this.view).callMethod('transform', args);

View File

@ -191,7 +191,6 @@ export class CompileView implements NameResolver {
} }
afterNodes() { afterNodes() {
this.pipes.forEach((pipe) => pipe.create());
this.viewQueries.values().forEach( this.viewQueries.values().forEach(
(queries) => queries.forEach( (queries) => queries.forEach(
(query) => query.afterChildren(this.createMethod, this.updateViewQueriesMethod))); (query) => query.afterChildren(this.createMethod, this.updateViewQueriesMethod)));