-
Notifications
You must be signed in to change notification settings - Fork 116
Fix unnecessarily wrapping modules with return inside arrow functions #302
Fix unnecessarily wrapping modules with return inside arrow functions #302
Conversation
There was a problem hiding this 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; |
There was a problem hiding this comment.
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 .
Suggestion implemented. I also changed the test to cover all types of functions, instead of just arrow functions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Published! |
The current version of the plugin incorrectly recognizes
return
statements inside arrow functions as top-levelreturn
s. As such, it adds an unnecessary wrapper around the module.The bug is due to this line in transform.js:
The node type of an arrow function is
'ArrowFunctionExpression'
, which does not start with the wordFunction
. 😛