eslint-plugin-lodash
eslint-plugin-lodash copied to clipboard
Rule proposal: forbid use of blacklisted lodash methods
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.
Similar intent to #201 ?
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.
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 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 check out #335 as a proof of concept if you're interested
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 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.