From 2141439c9c65ad3e2881fb9933f932b12868458a Mon Sep 17 00:00:00 2001 From: Guillaume Leclerc Date: Wed, 5 Oct 2016 16:22:36 +0200 Subject: [PATCH] Support NewExpression - One test added - Supports constructor arguments (any number) - resolves #14 --- index.js | 13 +++++++++++++ test/eval.js | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/index.js b/index.js index fef382d..8585fd0 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/test/eval.js b/test/eval.js index 443b532..611aa95 100644 --- a/test/eval.js +++ b/test/eval.js @@ -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); +});