cjs plugin: ExportNamedDeclaration of arrow function is missing .name
#7194
Labels
outdated
A closed issue/PR that is archived due to age. Recommended to make a new issue
NOTE: this seems to be fixed in 7.x either intentionally or from refactorings. I don't know the maintenance status of 6.x, so I'm opening this anyhow.
Choose one: is this a bug report or feature request? Bug
Input Code
Repro repo: https://github.com/FLGMwt/babel-cjs-bug
Babel/Babylon Configuration (.babelrc, package.json, cli command)
babel test.js | node
Expected Behavior
Output of
babel test.js | node
should beArrow functions should have their
.name
property defined by declarations, as demonstrated with thewithoutExport
function name.Current Behavior
The arrow function that is also exported at the time of declaration is not given its name.
This seems to be because the output of the transform disassociates the arrow function from the variable declaration by injecting a cascading assignment to the module-wrapping code:
and I guess this turns the arrow function into an anonymous one.
Possible Solution
Possible solutions:
exports.withExport
assignment.name
property to the function in a separate statementIf we're capable of fixing this in 6.x, I'm happy to give it a shot : )
Context
I was writing a tutorial for testing React apps using Enzyme. Building on create-react-app, I made some simple, stateless functional components and immediately exported them link. Enzyme uses a jQuery-like selector syntax for traversing a DOM-like structure which accepts string keys for components. React derives these keys/component names from function names, which in this case was missing. When I try to find these components in the test like this, I can't. (shows up as
<Component />
instead of<SpyListItem />
)It's worth pointing out that this is only a problem for node-based unit tests. Dev tools use the same names to identify components, but those work in the browser usually since the
env
preset transforms arrow functions into named functions. The create-react-app config usesnode: 'current'
, which, unless current is <=5.12, leaves arrows alone. After opening this, I'm gonna poke around that repo to see about pinning down the node env target.I can work around this by explicitly naming the component, naming the function, querying by object reference, or exporting differently, but since I'm trying to reach a beginner audience, I don't want to have to do anything exceptional.
Your Environment
The text was updated successfully, but these errors were encountered: