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

Commit e1ecc34

Browse files
dmanekIgorMinar
authored andcommitted
fix(parser): Fix short circuit of logical AND and OR operators
Closes #433
1 parent 29d36e9 commit e1ecc34

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/parser.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ var OPERATORS = {
55
'true':function(self){return true;},
66
'false':function(self){return false;},
77
$undefined:noop,
8-
'+':function(self, a,b){return (isDefined(a)?a:0)+(isDefined(b)?b:0);},
9-
'-':function(self, a,b){return (isDefined(a)?a:0)-(isDefined(b)?b:0);},
10-
'*':function(self, a,b){return a*b;},
11-
'/':function(self, a,b){return a/b;},
12-
'%':function(self, a,b){return a%b;},
13-
'^':function(self, a,b){return a^b;},
8+
'+':function(self, a,b){a=a(self); b=b(self); return (isDefined(a)?a:0)+(isDefined(b)?b:0);},
9+
'-':function(self, a,b){a=a(self); b=b(self); return (isDefined(a)?a:0)-(isDefined(b)?b:0);},
10+
'*':function(self, a,b){return a(self)*b(self);},
11+
'/':function(self, a,b){return a(self)/b(self);},
12+
'%':function(self, a,b){return a(self)%b(self);},
13+
'^':function(self, a,b){return a(self)^b(self);},
1414
'=':noop,
15-
'==':function(self, a,b){return a==b;},
16-
'!=':function(self, a,b){return a!=b;},
17-
'<':function(self, a,b){return a<b;},
18-
'>':function(self, a,b){return a>b;},
19-
'<=':function(self, a,b){return a<=b;},
20-
'>=':function(self, a,b){return a>=b;},
21-
'&&':function(self, a,b){return a&&b;},
22-
'||':function(self, a,b){return a||b;},
23-
'&':function(self, a,b){return a&b;},
15+
'==':function(self, a,b){return a(self)==b(self);},
16+
'!=':function(self, a,b){return a(self)!=b(self);},
17+
'<':function(self, a,b){return a(self)<b(self);},
18+
'>':function(self, a,b){return a(self)>b(self);},
19+
'<=':function(self, a,b){return a(self)<=b(self);},
20+
'>=':function(self, a,b){return a(self)>=b(self);},
21+
'&&':function(self, a,b){return a(self)&&b(self);},
22+
'||':function(self, a,b){return a(self)||b(self);},
23+
'&':function(self, a,b){return a(self)&b(self);},
2424
// '|':function(self, a,b){return a|b;},
25-
'|':function(self, a,b){return b(self, a);},
26-
'!':function(self, a){return !a;}
25+
'|':function(self, a,b){return b(self)(self, a(self));},
26+
'!':function(self, a){return !a(self);}
2727
};
2828
var ESCAPE = {"n":"\n", "f":"\f", "r":"\r", "t":"\t", "v":"\v", "'":"'", '"':'"'};
2929

@@ -308,13 +308,13 @@ function parser(text, json){
308308

309309
function unaryFn(fn, right) {
310310
return function(self) {
311-
return fn(self, right(self));
311+
return fn(self, right);
312312
};
313313
}
314314

315315
function binaryFn(left, fn, right) {
316316
return function(self) {
317-
return fn(self, left(self), right(self));
317+
return fn(self, left, right);
318318
};
319319
}
320320

test/ParserSpec.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,22 +391,38 @@ describe('parser', function() {
391391
expect(scope.a).not.toBeDefined();
392392
});
393393

394-
it('should allow assignment after array dereference', function(){
394+
it('should allow assignment after array dereference', function() {
395395
scope = angular.scope();
396396
scope.obj = [{}];
397397
scope.$eval('obj[0].name=1');
398398
expect(scope.obj.name).toBeUndefined();
399399
expect(scope.obj[0].name).toEqual(1);
400400
});
401401

402-
describe('formatter', function(){
402+
it('should short-circuit AND operator', function() {
403+
var scope = angular.scope();
404+
scope.run = function() {
405+
throw "IT SHOULD NOT HAVE RUN";
406+
};
407+
expect(scope.$eval('false && run()')).toBe(false);
408+
});
409+
410+
it('should short-circuit OR operator', function() {
411+
var scope = angular.scope();
412+
scope.run = function() {
413+
throw "IT SHOULD NOT HAVE RUN";
414+
};
415+
expect(scope.$eval('true || run()')).toBe(true);
416+
});
417+
418+
describe('formatter', function() {
403419
it('should return no argument function', function() {
404420
var noop = parser('noop').formatter()();
405421
expect(noop.format(null, 'abc')).toEqual('abc');
406422
expect(noop.parse(null, '123')).toEqual('123');
407423
});
408424

409-
it('should delegate arguments', function(){
425+
it('should delegate arguments', function() {
410426
angularFormatter.myArgs = {
411427
parse: function(a, b){ return [a, b]; },
412428
format: function(a, b){ return [a, b]; }

0 commit comments

Comments
 (0)