Skip to content
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

Doesn't handle curried versions of get() and getOr() #24

Open
daveisfera opened this issue Mar 11, 2020 · 3 comments
Open

Doesn't handle curried versions of get() and getOr() #24

daveisfera opened this issue Mar 11, 2020 · 3 comments

Comments

@daveisfera
Copy link
Contributor

One last big thanks for the awesome codemod!

Curried versions of get() and getOr() cause an error:

TypeError: Cannot read property 'type' of undefined
    at skip (/Users/dlj/projects/optional-chaining-codemod/transform.js:82:35)
    at NodePath.<anonymous> (/Users/dlj/projects/optional-chaining-codemod/transform.js:133:9)
    at NodePath.<anonymous> (/usr/local/lib/node_modules/jscodeshift/src/collections/Node.js:142:47)
    at /usr/local/lib/node_modules/jscodeshift/src/Collection.js:75:36
    at Array.forEach (<anonymous>)
    at Collection.forEach (/usr/local/lib/node_modules/jscodeshift/src/Collection.js:74:18)
    at Collection.replaceWith (/usr/local/lib/node_modules/jscodeshift/src/collections/Node.js:140:17)
    at Collection.replaceWith (/usr/local/lib/node_modules/jscodeshift/src/Collection.js:413:43)
    at mangleLodashGets (/Users/dlj/projects/optional-chaining-codemod/transform.js:132:8)
    at module.exports (/Users/dlj/projects/optional-chaining-codemod/transform.js:313:3)

But they could be turned into fat arrow functions using the existing logic, like the following:
get('value') to v => v?.value
getOr(default, 'value') to v => v?.value ?? default

@villesau
Copy link
Owner

Can you give a bit more complete example with imports and such? Does Lodash provide curried versions of functions out of the box? It might be hard to track wether the function is curried or not and what to do and when.

@daveisfera
Copy link
Contributor Author

Something like this

const a = [{ bogus: true }, { count: 100 }, { missing: true }];

const b = flow([
    filter(get('count')),
    get([0, 'count']),
])(a);

Could be changed to:

const b = flow([
    filter(v => v?.count),
    v => v?.[0]?.count,
])(a);

@daveisfera
Copy link
Contributor Author

Also, the number of arguments should be an indication of it being curried (i.e. 2 arguments to get() is a "standard call" and 1 argument to get() is a "curried call")

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

2 participants