JamieMason / JamieMason/codemods
use-named-exports throws on FunctionDeclarations
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 64
- Forks
- 8
- PR merge metrics
- No merged PRs in 30d
Description
Description
Running use-named-exports on this:
export default function foo(){}
throws:
ERR file.js Transformation error ({ type: FunctionDeclaration, start: 15, end: 32, loc: null, id: [object Object], generator: false, async: false, params: , body: [object Object], expression: false, defaults: , rest: null, returnType: null, typeParameters: null, predicate: null } does not match field "init": Expression | null of type VariableDeclarator)
Error: { type: FunctionDeclaration, start: 15, end: 32, loc: null, id: [object Object], generator: false, async: false, params: , body: [object Object], expression: false, defaults: , rest: null, returnType: null, typeParameters: null, predicate: null } does not match field "init": Expression | null of type VariableDeclarator
at addParam (/Users/seankelley/code/codemods/node_modules/ast-types/lib/types.js:450:27)
at /Users/seankelley/code/codemods/node_modules/ast-types/lib/types.js:473:25
at Array.forEach (<anonymous>)
at Function.variableDeclarator (/Users/seankelley/code/codemods/node_modules/ast-types/lib/types.js:471:35)
at Collection.exportDefaultAsNamed (/Users/seankelley/code/codemods/transforms/lib/helpers.js:160:73)
at NodePath.<anonymous> (/Users/seankelley/code/codemods/transforms/use-named-exports.js:26:33)
at NodePath.<anonymous> (/Users/seankelley/code/codemods/node_modules/jscodeshift/src/collections/Node.js:156:49)
at /Users/seankelley/code/codemods/node_modules/jscodeshift/src/Collection.js:75:36
at Array.forEach (<anonymous>)
at Collection.forEach (/Users/seankelley/code/codemods/node_modules/jscodeshift/src/Collection.js:74:18)
Suggested Solution
Rewrite to the following:
export function foo(){}
or (assuming the file is called bar.js and we're doing the rename-export-after-file thing):
export function bar(){}
Help Needed
I'm not very familiar with jscodeshift so I hacked this poorly onto a fork of mine at https://github.com/seansfkelley/codemods/commit/bb28f9ed4cb865459ccfc3e22a35029c0c8bb95a.
It performs the second modification above, that is, to rename the exported function after the file.
I'm filing this issue to help anyone else who might stumble upon the same error. Thank you so much for publishing this repo; you saved me a bunch of work!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with transforms/lib/helpers.js at exportDefaultAsNamed and the caller in transforms/use-named-exports.js, then run the provided default FunctionDeclaration example to reproduce the ast-types error. Done means the transform handles the example and produces one of the suggested named-export forms without throwing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100