Skip to content

use-named-exports throws on FunctionDeclarations #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
seansfkelley opened this issue Mar 3, 2023 · 2 comments
Open

use-named-exports throws on FunctionDeclarations #17

seansfkelley opened this issue Mar 3, 2023 · 2 comments

Comments

@seansfkelley
Copy link

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 seansfkelley@bb28f9e.

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!

@JamieMason
Copy link
Owner

Looks good Sean, thanks a lot. When I get some time I'll try and get that merged in 👍🏻

@lcswillems
Copy link

Hey @JamieMason , encountering the same issue on my side, making the codemod unusable. 100% of our default exports are this way.

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

No branches or pull requests

3 participants