Skip to content

Support NewExpression #16

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -136,6 +136,19 @@ module.exports = function (ast, vars) {
else if (node.type === 'TemplateElement') {
return node.value.cooked;
}
else if (node.type === 'NewExpression') {
var callee = walk(node.callee);
if (callee === FAIL) return FAIL;
if (typeof callee !== 'function') return FAIL;

var args = [];
for (var i = 0, l = node.arguments.length; i < l; i++) {
var x = walk(node.arguments[i]);
if (x === FAIL) return FAIL;
args.push(x);
}
return new callee(...args);
}
else return FAIL;
})(ast);

12 changes: 12 additions & 0 deletions test/eval.js
Original file line number Diff line number Diff line change
@@ -62,3 +62,15 @@ test('evaluate this', function(t) {
});
t.equal(res, 101);
});

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

var src = '(new Array(1, v))[1]';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, {
Array: Array,
v: 100
});
t.equal(res, 100);
});