Skip to content

Commit

Permalink
Merge branch 'master' into fix-func-decl-invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Mar 7, 2020
2 parents 6d79e78 + b9cd982 commit 23bc6ca
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# static-eval Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.4
* Short-circuit evaluation in `&&` and `||` expressions. ([@RoboPhred](https://github.com/RoboPhred) in [#28](https://github.com/browserify/static-eval/pull/28))
* Start tracking changes.
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,30 @@ module.exports = function (ast, vars) {
}
else if (node.type === 'BinaryExpression' ||
node.type === 'LogicalExpression') {
var op = node.operator;

if (op === '&&') {
var l = walk(node.left);
if (l === FAIL) return FAIL;
if (!l) return l;
var r = walk(node.right);
if (r === FAIL) return FAIL;
return r;
}
else if (op === '||') {
var l = walk(node.left);
if (l === FAIL) return FAIL;
if (l) return l;
var r = walk(node.right);
if (r === FAIL) return FAIL;
return r;
}

var l = walk(node.left, noExecute);
if (l === FAIL) return FAIL;
var r = walk(node.right, noExecute);
if (r === FAIL) return FAIL;

var op = node.operator;
if (op === '==') return l == r;
if (op === '===') return l === r;
if (op === '!=') return l != r;
Expand All @@ -62,8 +80,6 @@ module.exports = function (ast, vars) {
if (op === '|') return l | r;
if (op === '&') return l & r;
if (op === '^') return l ^ r;
if (op === '&&') return l && r;
if (op === '||') return l || r;

return FAIL;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "static-eval",
"version": "2.0.3",
"version": "2.0.4",
"description": "evaluate statically-analyzable expressions",
"main": "index.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ evaluate statically-analyzable expressions

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

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

# security

Expand Down
26 changes: 26 additions & 0 deletions test/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,32 @@ test('constructor at runtime only', function(t) {
t.equal(res, undefined);
});

test('short circuit evaluation AND', function(t) {
t.plan(1);

var variables = {
value: null
};
var src = 'value && value.func()';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, variables);
t.equals(res, null);
})

test('short circuit evaluation OR', function(t) {
t.plan(1);

var fnInvoked = false;
var variables = {
value: true,
fn: function() { fnInvoked = true}
};
var src = 'value || fn()';
var ast = parse(src).body[0].expression;
evaluate(ast, variables);
t.equals(fnInvoked, false);
})

test('function declaration does not invoke CallExpressions', function(t) {
t.plan(1);

Expand Down

0 comments on commit 23bc6ca

Please sign in to comment.