diff --git a/src/js/instrument/esnstrument.js b/src/js/instrument/esnstrument.js index b53fa096..34ad58a9 100644 --- a/src/js/instrument/esnstrument.js +++ b/src/js/instrument/esnstrument.js @@ -264,11 +264,12 @@ if (typeof J$ === 'undefined') { var visitorReplaceInExpr = { 'Identifier': function (node) { if (node.name.indexOf(RP) === 0) { - var i = parseInt(node.name.substring(RP.length)); - return asts[i]; - } else { - return node; + var suffix = node.name.substring(RP.length); + if (!isNaN(suffix)) { + return asts[suffix]; + } } + return node; }, 'BlockStatement': function (node) { if (node.body[0].type === 'ExpressionStatement' && isArr(node.body[0].expression)) { @@ -1819,9 +1820,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 +1846,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); } diff --git a/tests/unit/es2015_syntax.js b/tests/unit/es2015_syntax.js new file mode 100644 index 00000000..47d25107 --- /dev/null +++ b/tests/unit/es2015_syntax.js @@ -0,0 +1,6 @@ +/** + * This code is syntactically correct, + * but not when placed in a block. + */ +function f() {} +var f = 42; diff --git a/tests/unit/unitTests.txt b/tests/unit/unitTests.txt index 0eb65c86..8730cddd 100644 --- a/tests/unit/unitTests.txt +++ b/tests/unit/unitTests.txt @@ -64,4 +64,5 @@ issue78 issue78b catch1 scope -typeofUndef \ No newline at end of file +typeofUndef +es2015_syntax \ No newline at end of file