Skip to content

Commit 23bc6ca

Browse files
Merge branch 'master' into fix-func-decl-invoke
2 parents 6d79e78 + b9cd982 commit 23bc6ca

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# static-eval Change Log
2+
All notable changes to this project will be documented in this file.
3+
This project adheres to [Semantic Versioning](http://semver.org/).
4+
5+
## 2.0.4
6+
* Short-circuit evaluation in `&&` and `||` expressions. ([@RoboPhred](https://github.com/RoboPhred) in [#28](https://github.com/browserify/static-eval/pull/28))
7+
* Start tracking changes.

index.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,30 @@ module.exports = function (ast, vars) {
4040
}
4141
else if (node.type === 'BinaryExpression' ||
4242
node.type === 'LogicalExpression') {
43+
var op = node.operator;
44+
45+
if (op === '&&') {
46+
var l = walk(node.left);
47+
if (l === FAIL) return FAIL;
48+
if (!l) return l;
49+
var r = walk(node.right);
50+
if (r === FAIL) return FAIL;
51+
return r;
52+
}
53+
else if (op === '||') {
54+
var l = walk(node.left);
55+
if (l === FAIL) return FAIL;
56+
if (l) return l;
57+
var r = walk(node.right);
58+
if (r === FAIL) return FAIL;
59+
return r;
60+
}
61+
4362
var l = walk(node.left, noExecute);
4463
if (l === FAIL) return FAIL;
4564
var r = walk(node.right, noExecute);
4665
if (r === FAIL) return FAIL;
4766

48-
var op = node.operator;
4967
if (op === '==') return l == r;
5068
if (op === '===') return l === r;
5169
if (op === '!=') return l != r;
@@ -62,8 +80,6 @@ module.exports = function (ast, vars) {
6280
if (op === '|') return l | r;
6381
if (op === '&') return l & r;
6482
if (op === '^') return l ^ r;
65-
if (op === '&&') return l && r;
66-
if (op === '||') return l || r;
6783

6884
return FAIL;
6985
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "static-eval",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"description": "evaluate statically-analyzable expressions",
55
"main": "index.js",
66
"dependencies": {

readme.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ evaluate statically-analyzable expressions
44

55
[![testling badge](https://ci.testling.com/substack/static-eval.png)](https://ci.testling.com/substack/static-eval)
66

7-
[![build status](https://secure.travis-ci.org/substack/static-eval.png)](http://travis-ci.org/substack/static-eval)
7+
[![build status](https://secure.travis-ci.org/browserify/static-eval.png)](http://travis-ci.org/browserify/static-eval)
88

99
# security
1010

test/eval.js

+26
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,32 @@ test('constructor at runtime only', function(t) {
136136
t.equal(res, undefined);
137137
});
138138

139+
test('short circuit evaluation AND', function(t) {
140+
t.plan(1);
141+
142+
var variables = {
143+
value: null
144+
};
145+
var src = 'value && value.func()';
146+
var ast = parse(src).body[0].expression;
147+
var res = evaluate(ast, variables);
148+
t.equals(res, null);
149+
})
150+
151+
test('short circuit evaluation OR', function(t) {
152+
t.plan(1);
153+
154+
var fnInvoked = false;
155+
var variables = {
156+
value: true,
157+
fn: function() { fnInvoked = true}
158+
};
159+
var src = 'value || fn()';
160+
var ast = parse(src).body[0].expression;
161+
evaluate(ast, variables);
162+
t.equals(fnInvoked, false);
163+
})
164+
139165
test('function declaration does not invoke CallExpressions', function(t) {
140166
t.plan(1);
141167

0 commit comments

Comments
 (0)