Skip to content

Commit

Permalink
Support NewExpression
Browse files Browse the repository at this point in the history
- One test added
- Supports constructor arguments (any number)
- resolves browserify#14
  • Loading branch information
GuillaumeLeclerc committed Oct 5, 2016
1 parent ab81134 commit 2141439
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions test/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 2141439

Please sign in to comment.