Skip to content

Commit

Permalink
Merge branch 'evaluate-this' of https://github.com/shawnbot/static-eval
Browse files Browse the repository at this point in the history
  • Loading branch information
substack authored and substack committed Sep 28, 2016
2 parents 12b26f9 + be80efb commit ffee049
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module.exports = function (ast, vars) {
}
else return FAIL;
}
else if (node.type === 'ThisExpression') {
if ({}.hasOwnProperty.call(vars, 'this')) {
return vars['this'];
}
else return FAIL;
}
else if (node.type === 'CallExpression') {
var callee = walk(node.callee);
if (callee === FAIL) return FAIL;
Expand Down
11 changes: 11 additions & 0 deletions test/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ test('array methods with vars', function(t) {
var ast = parse(src).body[0].expression;
t.deepEqual(evaluate(ast, {x: 2}), [2, 4, 6]);
});

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

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.equal(res, 101);
});

0 comments on commit ffee049

Please sign in to comment.