Skip to content

Commit 7bd682b

Browse files
committed
feat(parser): changed parser to parse pipes in the middle of a binding
1 parent e927342 commit 7bd682b

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

modules/angular2/src/change_detection/parser/parser.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,11 @@ class _ParseAST {
211211

212212
parsePipe() {
213213
var result = this.parseExpression();
214-
while (this.optionalOperator("|")) {
215-
if (this.parseAction) {
216-
this.error("Cannot have a pipe in an action expression");
217-
}
218-
var name = this.expectIdentifierOrKeyword();
219-
var args = ListWrapper.create();
220-
while (this.optionalCharacter($COLON)) {
221-
ListWrapper.push(args, this.parseExpression());
222-
}
223-
result = new Pipe(result, name, args, true);
214+
if (this.optionalOperator("|")) {
215+
return this.parseInlinedPipe(result);
216+
} else {
217+
return result;
224218
}
225-
return result;
226219
}
227220

228221
parseExpression() {
@@ -464,10 +457,32 @@ class _ParseAST {
464457
} else {
465458
var getter = this.reflector.getter(id);
466459
var setter = this.reflector.setter(id);
467-
return new AccessMember(receiver, id, getter, setter);
460+
var am = new AccessMember(receiver, id, getter, setter);
461+
462+
if (this.optionalOperator("|")) {
463+
return this.parseInlinedPipe(am);
464+
} else {
465+
return am;
466+
}
468467
}
469468
}
470469

470+
parseInlinedPipe(result) {
471+
do {
472+
if (this.parseAction) {
473+
this.error("Cannot have a pipe in an action expression");
474+
}
475+
var name = this.expectIdentifierOrKeyword();
476+
var args = ListWrapper.create();
477+
while (this.optionalCharacter($COLON)) {
478+
ListWrapper.push(args, this.parseExpression());
479+
}
480+
result = new Pipe(result, name, args, true);
481+
} while(this.optionalOperator("|"));
482+
483+
return result;
484+
}
485+
471486
parseCallArguments() {
472487
if (this.next.isCharacter($RPAREN)) return [];
473488
var positionals = [];

modules/angular2/test/change_detection/parser/parser_spec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,29 @@ export function main() {
381381
expect(exp.name).toEqual("uppercase");
382382
});
383383

384+
it("should parse pipes in the middle of a binding", () => {
385+
var exp = parseBinding("user|a|b.name").ast;
386+
387+
expect(exp.name).toEqual("name");
388+
expect(exp.receiver).toBeAnInstanceOf(Pipe);
389+
expect(exp.receiver.name).toEqual("b");
390+
391+
expect(exp.receiver.exp).toBeAnInstanceOf(Pipe);
392+
expect(exp.receiver.exp.name).toEqual("a");
393+
});
394+
384395
it("should parse pipes with args", () => {
385-
var exp = parseBinding("1|increment:2").ast;
396+
var exp = parseBinding("(1|a:2)|b:3").ast;
397+
386398
expect(exp).toBeAnInstanceOf(Pipe);
387-
expect(exp.name).toEqual("increment");
399+
expect(exp.name).toEqual("b");
388400
expect(exp.args[0]).toBeAnInstanceOf(LiteralPrimitive);
401+
402+
expect(exp.exp).toBeAnInstanceOf(Pipe);
403+
expect(exp.exp.name).toEqual("a");
404+
expect(exp.exp.args[0]).toBeAnInstanceOf(LiteralPrimitive);
405+
406+
expect(exp.exp.exp).toBeAnInstanceOf(LiteralPrimitive);
389407
});
390408

391409
it('should only allow identifier or keyword as formatter names', () => {

0 commit comments

Comments
 (0)