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

Rule proposal: forbid use of blacklisted lodash methods #221

Open
serhalp opened this issue Oct 15, 2019 · 7 comments · May be fixed by #335
Open

Rule proposal: forbid use of blacklisted lodash methods #221

serhalp opened this issue Oct 15, 2019 · 7 comments · May be fixed by #335

Comments

@serhalp
Copy link
Contributor

serhalp commented Oct 15, 2019

forbid-methods

This rule forbids the use of a configured set of lodash methods.

Rule Details

This rule takes one argument:

  • The first (required), which methods to forbid calling

The following patterns are considered warnings:

/* eslint lodash/forbid-methods: [2, ['get'] */
var result = _.get(user, 'name');
/* eslint lodash/forbid-methods: [2, ['get'] */
var result = _(user).omit('id').get('name')
/* eslint lodash/forbid-methods: [2, ['keys', 'values'] */
var result = _.keys(user);

The following patterns are not considered warnings:

/* eslint lodash/forbid-methods: [2, ['keys', 'values'] */
var result = _.get(user, 'name');
/* eslint lodash/forbid-methods: [2, ['get'] */
var result = _.keys(user);

When Not To Use It

If you do not want to forbid the use of any lodash methods, then you can disable this rule.

@robatwilliams
Copy link

Similar intent to #201 ?

@serhalp
Copy link
Contributor Author

serhalp commented Nov 8, 2019

I can imagine #201 being unnecessary if this one is implemented, as this one is a superset. Perhaps this one could even have a flag like forbidMethodsWithNativeEquivalent (someone can come up with a better name...) so you wouldn't have to list them all out if #201 is your use case.

@TSMMark
Copy link
Contributor

TSMMark commented Aug 23, 2021

Would like to rekindle this conversation.

I'm working on a codebase that's old enough that we were using the prefer-lodash-method to avoid relying on browser polyfills to support functions such as array prototype map, but this has not been a valid concern in quite some time now.

I would love a rule like this, e.g. lodash/forbid-methods or lodash/prefer-native-method, so that we could convert all the lodash functions like map, etc back to array prototype functions using an automated linter approach rather than by hand.

@serhalp you mentioned in #201 you may take as tab at a PR, did you ever solve for this? Got a fork lying around or something I could use for a onetime conversion, even if it's incomplete and not ready for merge?

@serhalp
Copy link
Contributor Author

serhalp commented Aug 23, 2021

@TSMMark Sorry, unfortunately no, I never got around to drafting this, since I got no feedback from the maintainers. I only see two PRs merged in the last 16 months in this repo, so I'm a little wary about investing time in a PR that won't get merged. 🤷🏼‍♂️

@TSMMark TSMMark linked a pull request Oct 15, 2021 that will close this issue
@TSMMark
Copy link
Contributor

TSMMark commented Oct 22, 2021

@serhalp check out #335 as a proof of concept if you're interested

@keverw
Copy link

keverw commented Nov 22, 2021

I like this idea since in my own project I would rather use isPlainObject instead of isObject for most cases. Was trying to see if there was a way already, but don't think there is.

@TSMMark
Copy link
Contributor

TSMMark commented Nov 22, 2021

@keverw feel free to try out using my branch https://github.com/wix/eslint-plugin-lodash/pull/335/files#diff-628fae3b7c19f17b44e6e61fe7bb21259a3e218e865baeb01809290acbe2559eR27-R33

You'd only have to change map to isObject and change the autofix code to replace with isPlainObject instead if you want it to autofix. If this seems like a rule that may be accepted we could work towards parameterizing the rule instead of hard coding.

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

Successfully merging a pull request may close this issue.

4 participants