eslint-plugin-lodash icon indicating copy to clipboard operation
eslint-plugin-lodash copied to clipboard

Rule proposal: forbid use of blacklisted lodash methods

Open serhalp opened this issue 6 years ago • 7 comments

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.

serhalp avatar Oct 15 '19 22:10 serhalp

Similar intent to #201 ?

robatwilliams avatar Nov 08 '19 15:11 robatwilliams

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.

serhalp avatar Nov 08 '19 15:11 serhalp

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?

TSMMark avatar Aug 23 '21 17:08 TSMMark

@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. 🤷🏼‍♂️

serhalp avatar Aug 23 '21 22:08 serhalp

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

TSMMark avatar Oct 22 '21 17:10 TSMMark

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.

keverw avatar Nov 22 '21 09:11 keverw

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

TSMMark avatar Nov 22 '21 14:11 TSMMark