From 0730ea426c9e99abb76318ca4a174c0d614c5592 Mon Sep 17 00:00:00 2001 From: Martin Rauscher Date: Mon, 8 Nov 2021 18:21:33 +0100 Subject: [PATCH 1/2] Support arrow functions Arrow functions are like normal functions but con only have a single body --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 92f4687..68e26c3 100644 --- a/index.js +++ b/index.js @@ -149,8 +149,8 @@ module.exports = function (ast, vars, opts) { else if (node.type === 'ReturnStatement') { return walk(node.argument, noExecute) } - else if (node.type === 'FunctionExpression') { - var bodies = node.body.body; + else if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') { + var bodies = node.body.body || [node.body]; // Create a "scope" for our arguments var oldVars = {}; From 482d1fd6608b2451eb8942fdbf7eb91d4f71c5e4 Mon Sep 17 00:00:00 2001 From: Martin Rauscher Date: Mon, 8 Nov 2021 18:27:00 +0100 Subject: [PATCH 2/2] add test for arrow functions --- test/eval.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/eval.js b/test/eval.js index ed5cf90..0c3b026 100644 --- a/test/eval.js +++ b/test/eval.js @@ -44,6 +44,14 @@ test('array methods', function(t) { t.deepEqual(evaluate(ast), [2, 4, 6]); }); +test('array methods with arrow function', function(t) { + t.plan(1); + + var src = '[1, 2, 3].map(n => n * 2 )'; + var ast = parse(src).body[0].expression; + t.deepEqual(evaluate(ast), [2, 4, 6]); +}); + test('array methods invocation count', function(t) { t.plan(2);