From ed1243ffc7c2cb4bd5b4dece597597db8eb08e34 Mon Sep 17 00:00:00 2001 From: Lucas Galfaso Date: Sun, 23 Nov 2014 14:06:45 +0100 Subject: [PATCH] fix(parse): fix operators associativity Make the operators `&&`, `==`, `!=`, `===`, `!==`, `<`, `>`, `<=`, `>=` follow Javascript left-to-right associativity --- src/ng/parse.js | 12 ++++++------ test/ng/parseSpec.js | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/ng/parse.js b/src/ng/parse.js index 0d30d1ff..68c832b8 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -598,8 +598,8 @@ Parser.prototype = { logicalAND: function() { var left = this.equality(); var token; - if ((token = this.expect('&&'))) { - left = this.binaryFn(left, token.text, this.logicalAND(), true); + while ((token = this.expect('&&'))) { + left = this.binaryFn(left, token.text, this.equality(), true); } return left; }, @@ -607,8 +607,8 @@ Parser.prototype = { equality: function() { var left = this.relational(); var token; - if ((token = this.expect('==','!=','===','!=='))) { - left = this.binaryFn(left, token.text, this.equality()); + while ((token = this.expect('==','!=','===','!=='))) { + left = this.binaryFn(left, token.text, this.relational()); } return left; }, @@ -616,8 +616,8 @@ Parser.prototype = { relational: function() { var left = this.additive(); var token; - if ((token = this.expect('<', '>', '<=', '>='))) { - left = this.binaryFn(left, token.text, this.relational()); + while ((token = this.expect('<', '>', '<=', '>='))) { + left = this.binaryFn(left, token.text, this.additive()); } return left; }, diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index b3b16a4a..dfe5de69 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -276,6 +276,10 @@ describe('parser', function() { expect(scope.$eval("2>=1")).toEqual(2 >= 1); expect(scope.$eval("true==2<3")).toEqual(true == 2 < 3); expect(scope.$eval("true===2<3")).toEqual(true === 2 < 3); + + expect(scope.$eval("true===3===3")).toEqual(true === 3 === 3); + expect(scope.$eval("3===3===true")).toEqual(3 === 3 === true); + expect(scope.$eval("3 >= 3 > 2")).toEqual(3 >= 3 > 2); }); it('should parse logical', function() { @@ -703,6 +707,7 @@ describe('parser', function() { throw "IT SHOULD NOT HAVE RUN"; }; expect(scope.$eval('false && run()')).toBe(false); + expect(scope.$eval('false && true && run()')).toBe(false); }); it('should short-circuit OR operator', function() { @@ -710,6 +715,7 @@ describe('parser', function() { throw "IT SHOULD NOT HAVE RUN"; }; expect(scope.$eval('true || run()')).toBe(true); + expect(scope.$eval('true || false || run()')).toBe(true); });