From 70de912ee17eec6d87935376e763ccff818e66c1 Mon Sep 17 00:00:00 2001 From: christofferqa Date: Wed, 14 Sep 2016 15:37:15 +0200 Subject: [PATCH] Fix SyntaxError due to function declarations --- src/js/instrument/esnstrument.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/js/instrument/esnstrument.js b/src/js/instrument/esnstrument.js index b53fa096..4c0eb24a 100644 --- a/src/js/instrument/esnstrument.js +++ b/src/js/instrument/esnstrument.js @@ -1819,9 +1819,19 @@ if (typeof J$ === 'undefined') { } } for (var i = startIndex; i < ast.body.length; i++) { - if (ast.body[i].type === 'FunctionDeclaration') { - newBody.push(ast.body[i]); + var name = ast.body[i].id.name; + var params = ast.body[i].params.map(function (param) { return param.name; }).join(', '); + var assignStmt; + if (ast.body[i].body === null) { + assignStmt = acorn.parse( + "var " + name + " = function " + name + "(" + params + ") {}").body; + } else { + assignStmt = replaceInStatement( + "var " + name + " = function " + name + "(" + params + ") { " + RP + "1 }", + ast.body[i].body.body); + } + newBody.push(assignStmt[0]); if (newBody.length !== i + 1) { hoisteredFunctions.push(ast.body[i].id.name); } @@ -1835,9 +1845,7 @@ if (typeof J$ === 'undefined') { while (ast.body.length > 0) { ast.body.pop(); } - for (var i = 0; i < newBody.length; i++) { - ast.body.push(newBody[i]); - } + Array.prototype.push.apply(ast.body, newBody); } else { //console.log(typeof ast.body); }