Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SyntaxError due to function declarations #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/js/instrument/esnstrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/es2015_syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This code is syntactically correct,
* but not when placed in a block.
*/
function f() {}
var f = 42;
3 changes: 2 additions & 1 deletion tests/unit/unitTests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ issue78
issue78b
catch1
scope
typeofUndef
typeofUndef
es2015_syntax