Skip to content

Commit 3ba8c61

Browse files
committed
Fix SyntaxError due to function declarations
1 parent 1950f5e commit 3ba8c61

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/js/instrument/esnstrument.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ if (typeof J$ === 'undefined') {
264264
var visitorReplaceInExpr = {
265265
'Identifier': function (node) {
266266
if (node.name.indexOf(RP) === 0) {
267-
var i = parseInt(node.name.substring(RP.length));
268-
return asts[i];
269-
} else {
270-
return node;
267+
var suffix = node.name.substring(RP.length);
268+
if (!isNaN(suffix)) {
269+
return asts[suffix];
270+
}
271271
}
272+
return node;
272273
},
273274
'BlockStatement': function (node) {
274275
if (node.body[0].type === 'ExpressionStatement' && isArr(node.body[0].expression)) {
@@ -1819,9 +1820,19 @@ if (typeof J$ === 'undefined') {
18191820
}
18201821
}
18211822
for (var i = startIndex; i < ast.body.length; i++) {
1822-
18231823
if (ast.body[i].type === 'FunctionDeclaration') {
1824-
newBody.push(ast.body[i]);
1824+
var name = ast.body[i].id.name;
1825+
var params = ast.body[i].params.map(function (param) { return param.name; }).join(', ');
1826+
var assignStmt;
1827+
if (ast.body[i].body === null) {
1828+
assignStmt = acorn.parse(
1829+
"var " + name + " = function " + name + "(" + params + ") {}").body;
1830+
} else {
1831+
assignStmt = replaceInStatement(
1832+
"var " + name + " = function " + name + "(" + params + ") { " + RP + "1 }",
1833+
ast.body[i].body.body);
1834+
}
1835+
newBody.push(assignStmt[0]);
18251836
if (newBody.length !== i + 1) {
18261837
hoisteredFunctions.push(ast.body[i].id.name);
18271838
}
@@ -1835,9 +1846,7 @@ if (typeof J$ === 'undefined') {
18351846
while (ast.body.length > 0) {
18361847
ast.body.pop();
18371848
}
1838-
for (var i = 0; i < newBody.length; i++) {
1839-
ast.body.push(newBody[i]);
1840-
}
1849+
Array.prototype.push.apply(ast.body, newBody);
18411850
} else {
18421851
//console.log(typeof ast.body);
18431852
}

tests/unit/es2015_syntax.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* This code is syntactically correct,
3+
* but not when placed in a block.
4+
*/
5+
function f() {}
6+
var f = 42;

tests/unit/unitTests.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ issue78
6464
issue78b
6565
catch1
6666
scope
67-
typeofUndef
67+
typeofUndef
68+
es2015_syntax

0 commit comments

Comments
 (0)