Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ed1243f

Browse files
committed
fix(parse): fix operators associativity
Make the operators `&&`, `==`, `!=`, `===`, `!==`, `<`, `>`, `<=`, `>=` follow Javascript left-to-right associativity
1 parent 3aa5752 commit ed1243f

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/ng/parse.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -598,26 +598,26 @@ Parser.prototype = {
598598
logicalAND: function() {
599599
var left = this.equality();
600600
var token;
601-
if ((token = this.expect('&&'))) {
602-
left = this.binaryFn(left, token.text, this.logicalAND(), true);
601+
while ((token = this.expect('&&'))) {
602+
left = this.binaryFn(left, token.text, this.equality(), true);
603603
}
604604
return left;
605605
},
606606

607607
equality: function() {
608608
var left = this.relational();
609609
var token;
610-
if ((token = this.expect('==','!=','===','!=='))) {
611-
left = this.binaryFn(left, token.text, this.equality());
610+
while ((token = this.expect('==','!=','===','!=='))) {
611+
left = this.binaryFn(left, token.text, this.relational());
612612
}
613613
return left;
614614
},
615615

616616
relational: function() {
617617
var left = this.additive();
618618
var token;
619-
if ((token = this.expect('<', '>', '<=', '>='))) {
620-
left = this.binaryFn(left, token.text, this.relational());
619+
while ((token = this.expect('<', '>', '<=', '>='))) {
620+
left = this.binaryFn(left, token.text, this.additive());
621621
}
622622
return left;
623623
},

test/ng/parseSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ describe('parser', function() {
276276
expect(scope.$eval("2>=1")).toEqual(2 >= 1);
277277
expect(scope.$eval("true==2<3")).toEqual(true == 2 < 3);
278278
expect(scope.$eval("true===2<3")).toEqual(true === 2 < 3);
279+
280+
expect(scope.$eval("true===3===3")).toEqual(true === 3 === 3);
281+
expect(scope.$eval("3===3===true")).toEqual(3 === 3 === true);
282+
expect(scope.$eval("3 >= 3 > 2")).toEqual(3 >= 3 > 2);
279283
});
280284

281285
it('should parse logical', function() {
@@ -703,13 +707,15 @@ describe('parser', function() {
703707
throw "IT SHOULD NOT HAVE RUN";
704708
};
705709
expect(scope.$eval('false && run()')).toBe(false);
710+
expect(scope.$eval('false && true && run()')).toBe(false);
706711
});
707712

708713
it('should short-circuit OR operator', function() {
709714
scope.run = function() {
710715
throw "IT SHOULD NOT HAVE RUN";
711716
};
712717
expect(scope.$eval('true || run()')).toBe(true);
718+
expect(scope.$eval('true || false || run()')).toBe(true);
713719
});
714720

715721

0 commit comments

Comments
 (0)