What is eslint-plugin-lodash?
eslint-plugin-lodash is an ESLint plugin that provides linting rules for Lodash, a popular JavaScript utility library. The plugin helps ensure that Lodash is used in an optimal and consistent manner, promoting best practices and preventing common mistakes.
What are eslint-plugin-lodash's main functionalities?
Prefer Lodash methods over native
This rule enforces the use of Lodash methods over native JavaScript methods. It ensures consistency and leverages Lodash's optimizations.
/* eslint lodash/prefer-lodash-method: [2] */
const _ = require('lodash');
const arr = [1, 2, 3];
// Bad: using native method
const doubled = arr.map(x => x * 2);
// Good: using Lodash method
const doubled = _.map(arr, x => x * 2);
Prefer _.get over direct property access
This rule encourages the use of _.get for property access, which is safer and avoids potential errors when accessing deeply nested properties.
/* eslint lodash/prefer-get: [2] */
const _ = require('lodash');
const obj = { a: { b: 2 } };
// Bad: direct property access
const value = obj.a.b;
// Good: using _.get
const value = _.get(obj, 'a.b');
Prefer _.isNil over _.isNull and _.isUndefined
This rule prefers the use of _.isNil, which checks for both null and undefined, over using _.isNull and _.isUndefined separately.
/* eslint lodash/prefer-is-nil: [2] */
const _ = require('lodash');
const value = null;
// Bad: using _.isNull and _.isUndefined
if (_.isNull(value) || _.isUndefined(value)) {
// do something
}
// Good: using _.isNil
if (_.isNil(value)) {
// do something
}
Other packages similar to eslint-plugin-lodash
eslint-plugin-underscore
eslint-plugin-underscore is an ESLint plugin that provides linting rules for Underscore.js, another popular JavaScript utility library. It offers similar functionality to eslint-plugin-lodash but is tailored for Underscore.js instead of Lodash.
eslint-plugin-you-dont-need-lodash-underscore
eslint-plugin-you-dont-need-lodash-underscore is an ESLint plugin that helps developers identify places where native JavaScript can be used instead of Lodash or Underscore.js. It promotes the use of native methods over utility libraries, contrasting with eslint-plugin-lodash which encourages the use of Lodash.
ESLint-plugin-lodash
lodash specific linting rules for ESLint
Installation
Install ESLint either locally or globally.
npm install eslint
If you installed ESLint
globally, you have to install lodash plugin globally too. Otherwise, install it locally.
$ npm install eslint-plugin-lodash
The rules are specifically written to target ES6 JavaScript, so you'll want to
use the babel-eslint parser.
Configuration
Add plugins
section and specify ESLint-plugin-lodash as a plugin.
{
"parser": "babel-eslint",
"plugins": [
"lodash"
]
}
Finally, enable all of the rules that you would like to use.
{
"rules": {
"lodash/import": 1,
}
}
List of supported rules
- import: Prevent importing the entire lodash library.
To Do
- Add Contributing Guide, until then PRs welcome!
Any rule idea is welcome !
License
ESLint-plugin-lodash is licensed under the MIT License.