Skip to content

Commit

Permalink
Merge pull request #8926 from noelebrun/bugfix/8874
Browse files Browse the repository at this point in the history
fixes #8874
  • Loading branch information
sokra committed Mar 25, 2019
2 parents 34ed7b4 + 1e0af56 commit dc26688
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/Parser.js
Expand Up @@ -1593,7 +1593,14 @@ class Parser extends Tapable {
walkFunctionExpression(expression) {
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
this.inScope(expression.params, () => {
const scopeParams = expression.params;

// Add function name in scope for recursive calls
if (expression.id) {
scopeParams.push(expression.id.name);
}

this.inScope(scopeParams, () => {
for (const param of expression.params) {
this.walkPattern(param);
}
Expand Down Expand Up @@ -1777,7 +1784,14 @@ class Parser extends Tapable {
const args = options.map(renameArgOrThis);
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
this.inScope(params.filter((identifier, idx) => !args[idx]), () => {
const scopeParams = params.filter((identifier, idx) => !args[idx]);

// Add function name in scope for recursive calls
if (functionExpression.id) {
scopeParams.push(functionExpression.id.name);
}

this.inScope(scopeParams, () => {
if (renameThis) {
this.scope.renames.set("this", renameThis);
}
Expand Down
20 changes: 20 additions & 0 deletions test/cases/parsing/issue-8874/index.js
@@ -0,0 +1,20 @@
import myFunction from './module';
import myFunctionDefaultParameter from './moduleDefaultParameter';
import myFunctionExportedFunctionExpression from './moduleExportedFunctionExpression';
import myFunctionExportedFunctionExpressionDefaultParameter from './moduleExportedFunctionExpressionDefaultParameter';

it('should execute IIFE twice', () => {
expect(myFunction()).toBe(2);
});

it('should execute IIFE twice when using IIFE function name as default parameter', () => {
expect(myFunctionDefaultParameter()).toBe(2);
});

it('should execute Function Expression twice', () => {
expect(myFunctionExportedFunctionExpression()).toBe(2);
});

it('should execute Function Expression twice when using IIFE function name as default parameter', () => {
expect(myFunctionExportedFunctionExpressionDefaultParameter()).toBe(2);
});
15 changes: 15 additions & 0 deletions test/cases/parsing/issue-8874/module.js
@@ -0,0 +1,15 @@
import someFunction from './someFunction';

export default function myFunction() {
let iifeExecutionCount = 0;

(function someFunction (recurse) {
iifeExecutionCount++;

if (recurse) {
someFunction(false);
}
})(true);

return iifeExecutionCount;
}
13 changes: 13 additions & 0 deletions test/cases/parsing/issue-8874/moduleDefaultParameter.js
@@ -0,0 +1,13 @@
export default function myFunction() {
let iifeExecutionCount = 0;

(function someFunction (recurse, recurseFunction = someFunction) {
iifeExecutionCount++;

if (recurse) {
recurseFunction(false);
}
})(true);

return iifeExecutionCount;
}
@@ -0,0 +1,9 @@
import someFunction from './someFunction';

export default (function someFunction (recurse = true) {
if (recurse) {
return 1 + someFunction(false);
}

return 1;
});
@@ -0,0 +1,10 @@

import someFunction from './someFunction';

export default (function someFunction (recurse = true, recurseFunction = someFunction) {
if (recurse) {
return 1 + recurseFunction(false);
}

return 1;
});
3 changes: 3 additions & 0 deletions test/cases/parsing/issue-8874/someFunction.js
@@ -0,0 +1,3 @@
export default function someFunction () {
return -1;
}

0 comments on commit dc26688

Please sign in to comment.