Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Fix unnecessarily wrapping modules with return inside arrow functions #302

Merged
merged 5 commits into from Mar 12, 2018

Conversation

MattiasBuelens
Copy link
Contributor

The current version of the plugin incorrectly recognizes return statements inside arrow functions as top-level returns. As such, it adds an unnecessary wrapper around the module.

The bug is due to this line in transform.js:

if ( /^Function/.test( node.type ) ) lexicalDepth += 1;

The node type of an arrow function is 'ArrowFunctionExpression', which does not start with the word Function. 馃槢

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome catch, thanks! Added one minor suggestion, otherwise I'd say this is good to go into the next release.

src/transform.js Outdated
@@ -143,7 +143,7 @@ export function transformCommonjs ( parse, code, id, isEntry, ignoreGlobal, igno
programDepth += 1;

if ( node.scope ) scope = node.scope;
if ( /^Function/.test( node.type ) ) lexicalDepth += 1;
if ( /Function/.test( node.type ) ) lexicalDepth += 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor improvement: If you extract the RegExp into a constant (outside the transform function), then performance will be slightly better as it only needs to be compiled once and we can reuse it for both occurrences .

@MattiasBuelens
Copy link
Contributor Author

Suggestion implemented. I also changed the test to cover all types of functions, instead of just arrow functions.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃憤

@lukastaegert lukastaegert merged commit ef68d8f into rollup:master Mar 12, 2018
@lukastaegert
Copy link
Member

Published!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants