Skip to content

Commit 4639f44

Browse files
committed
feat(Parser): associate pipes right to left
closes #4605 BREAKING CHANGE: Before: `1 + 1 | pipe:a | pipe:b` was parsed as `(1 + 1) | pipe:(a | pipe:b)` After: `1 + 1 | pipe:a | pipe:b` is parsed as `((1 + 1) | pipe:a) | pipe:b` Closes #4716
1 parent 77604b8 commit 4639f44

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

modules/angular2/src/core/change_detection/parser/parser.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ export class _ParseAST {
216216
return n.toString();
217217
}
218218

219+
parseSimpleBinding(): AST {
220+
var ast = this.parseChain();
221+
if (!SimpleExpressionChecker.check(ast)) {
222+
this.error(`Simple binding expression can only contain field access and constants'`);
223+
}
224+
return ast;
225+
}
226+
219227
parseChain(): AST {
220228
var exprs = [];
221229
while (this.index < this.tokens.length) {
@@ -237,14 +245,6 @@ export class _ParseAST {
237245
return new Chain(exprs);
238246
}
239247

240-
parseSimpleBinding(): AST {
241-
var ast = this.parseChain();
242-
if (!SimpleExpressionChecker.check(ast)) {
243-
this.error(`Simple binding expression can only contain field access and constants'`);
244-
}
245-
return ast;
246-
}
247-
248248
parsePipe(): AST {
249249
var result = this.parseExpression();
250250
if (this.optionalOperator("|")) {
@@ -256,7 +256,7 @@ export class _ParseAST {
256256
var name = this.expectIdentifierOrKeyword();
257257
var args = [];
258258
while (this.optionalCharacter($COLON)) {
259-
args.push(this.parsePipe());
259+
args.push(this.parseExpression());
260260
}
261261
result = new BindingPipe(result, name, args);
262262
} while (this.optionalOperator("|"));

modules/angular2/test/core/change_detection/change_detector_config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ var _availableDefinitions = [
408408
'name | pipe',
409409
'(name | pipe).length',
410410
"name | pipe:'one':address.city",
411+
"name | pipe:'a':'b' | pipe:0:1:2",
411412
'value',
412413
'a',
413414
'address.city',

modules/angular2/test/core/change_detection/change_detector_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ export function main() {
354354
expect(val.dispatcher.loggedValues).toEqual(['value one two default']);
355355
});
356356

357+
it('should associate pipes right-to-left', () => {
358+
var registry = new FakePipes('pipe', () => new MultiArgPipe());
359+
var person = new Person('value');
360+
var val = _createChangeDetector("name | pipe:'a':'b' | pipe:0:1:2", person, registry);
361+
val.changeDetector.detectChanges();
362+
expect(val.dispatcher.loggedValues).toEqual(['value a b default 0 1 2']);
363+
});
364+
357365
it('should not reevaluate pure pipes unless its context or arg changes', () => {
358366
var pipe = new CountingPipe();
359367
var registry = new FakePipes('pipe', () => pipe, {pure: true});

modules/angular2/test/core/change_detection/parser/parser_spec.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ import {Unparser} from './unparser';
66
import {Lexer} from 'angular2/src/core/change_detection/parser/lexer';
77
import {BindingPipe, LiteralPrimitive, AST} from 'angular2/src/core/change_detection/parser/ast';
88

9-
class TestData {
10-
constructor(public a?: any, public b?: any, public fnReturnValue?: any) {}
11-
12-
fn() { return this.fnReturnValue; }
13-
14-
add(a, b) { return a + b; }
15-
}
16-
179
export function main() {
1810
function createParser() { return new Parser(new Lexer(), reflector); }
1911

@@ -229,8 +221,8 @@ export function main() {
229221
checkBinding('a[b] | c', '(a[b] | c)');
230222
checkBinding('a?.b | c', '(a?.b | c)');
231223
checkBinding('true | a', '(true | a)');
232-
checkBinding('a | b:c | d', '(a | b:(c | d))');
233-
checkBinding('(a | b:c) | d', '((a | b:c) | d)');
224+
checkBinding('a | b:c | d', '((a | b:c) | d)');
225+
checkBinding('a | b:(c | d)', '(a | b:(c | d))');
234226
});
235227

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

0 commit comments

Comments
 (0)