Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .success property to check if last call was resolved #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var unparse = require('escodegen').generate;

module.exports = function (ast, vars) {
module.exports = function staticEval (ast, vars) {
if (!vars) vars = {};
var FAIL = {};

Expand Down Expand Up @@ -172,5 +172,6 @@ module.exports = function (ast, vars) {
else return FAIL;
})(ast);

staticEval.success = result !== FAIL
return result === FAIL ? undefined : result;
};
3 changes: 2 additions & 1 deletion readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ tree object `ast` with an optional collection of variables `vars` to use in the
static expression resolution.

If the expression contained in `ast` can't be statically resolved, `evaluate()`
returns undefined.
returns undefined. You can also use the `evaluate.success` property to check if
the most recent call succeeded.

# install

Expand Down
17 changes: 11 additions & 6 deletions test/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var evaluate = require('../');
var parse = require('esprima').parse;

test('resolved', function (t) {
t.plan(1);
t.plan(2);

var src = '[1,2,3+4*10+(n||6),foo(3+5),obj[""+"x"].y]';
var ast = parse(src).body[0].expression;
Expand All @@ -12,11 +12,12 @@ test('resolved', function (t) {
foo: function (x) { return x * 100 },
obj: { x: { y: 555 } }
});
t.ok(evaluate.success);
t.deepEqual(res, [ 1, 2, 49, 800, 555 ]);
});

test('unresolved', function (t) {
t.plan(1);
t.plan(2);

var src = '[1,2,3+4*10*z+n,foo(3+5),obj[""+"x"].y]';
var ast = parse(src).body[0].expression;
Expand All @@ -25,6 +26,7 @@ test('unresolved', function (t) {
foo: function (x) { return x * 100 },
obj: { x: { y: 555 } }
});
t.notOk(evaluate.success);
t.equal(res, undefined);
});

Expand Down Expand Up @@ -53,30 +55,33 @@ test('array methods with vars', function(t) {
});

test('evaluate this', function(t) {
t.plan(1);
t.plan(2);

var src = 'this.x + this.y.z';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, {
'this': { x: 1, y: { z: 100 } }
});
t.ok(evaluate.success);
t.equal(res, 101);
});

test('FunctionExpression unresolved', function(t) {
t.plan(1);
t.plan(2);

var src = '(function(){console.log("Not Good")})';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, {});
t.notOk(evaluate.success);
t.equal(res, undefined);
});

test('MemberExpressions from Functions unresolved', function(t) {
t.plan(1);
t.plan(2);

var src = '(function () {}).constructor';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, {});
t.notOk(evaluate.success);
t.equal(res, undefined);
});
});
3 changes: 2 additions & 1 deletion test/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var evaluate = require('../');
var parse = require('esprima').parse;

test('function property', function (t) {
t.plan(1);
t.plan(2);

var src = '[1,2,3+4*10+n,beep.boop(3+5),obj[""+"x"].y]';
var ast = parse(src).body[0].expression;
Expand All @@ -12,5 +12,6 @@ test('function property', function (t) {
beep: { boop: function (x) { return x * 100 } },
obj: { x: { y: 555 } }
});
t.ok(evaluate.success);
t.deepEqual(res, [ 1, 2, 49, 800, 555 ]);
});
6 changes: 4 additions & 2 deletions test/template-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ var evaluate = require('../');
var parse = require('esprima').parse;

test('untagged template strings', function (t) {
t.plan(1);
t.plan(2);

var src = '`${1},${2 + n},${`4,5`}`';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, {
n: 6
});
t.ok(evaluate.success);
t.deepEqual(res, '1,8,4,5');
});

test('tagged template strings', function (t) {
t.plan(3);
t.plan(4);

var src = 'template`${1},${2 + n},${`4,5`}`';
var ast = parse(src).body[0].expression;
Expand All @@ -29,5 +30,6 @@ test('tagged template strings', function (t) {
},
n: 6
});
t.ok(evaluate.success);
t.deepEqual(res, 'foo');
})