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 the Lodash plugin globally too. Otherwise, install it locally.
$ npm install eslint-plugin-lodash
Configuration
Add a plugins
section and specify ESLint-Plugin-Lodash as a plugin.
You can additionally add settings for the plugin.
Shared Rule Settings
These are settings that can be shared by all of the rules. All settings are under the lodash
inside the general settings
object. For more info about shared settings, read the ESLint Configuration Guide.
- pragma: specifies the name you use for the Lodash variable in your code. Default is
_
. - version: specifies the major Lodash Version you are using (default is
4
).
If you wish to use this plugin with Lodash v3, this value should be 3
. (on by default in the config v3
)
Finally, enable all of the rules that you would like to use.
Recommended configuration
This plugin exports a recommended
configuration that enforces all the rules. You can configure the plugin as follows:
{
"plugins": ["lodash"],
"extends": ["plugin:lodash/recommended"]
}
Configuration for Using with Lodash v3
Out of the box, this plugin supports the use of Lodash v4. To use with Lodash v3, the config needs to specify the version in the settings
, and can't use some rules.
The plugin also exports a v3
config for ease of use.
{
"plugins": ["lodash"],
"extends": ["plugin:lodash/v3"]
}
List of provided rules
Rules are divided into categories for your convenience. All rules are off by default, unless you use one of the plugin's configurations which turn all relevant rules on.
Possible Errors
The following rules point out areas where you might have made mistakes.
- callback-binding: Use or avoid
thisArg
for Lodash method callbacks, depending on major version. - unwrap: Prevent chaining without evaluation via
value()
or non-chainable methods like max()
., - no-double-unwrap: Do not use
.value()
on chains that have already ended (e.g. with max()
or reduce()
) (fixable) - collection-return: Always return a value in iteratees of Lodash collection methods that aren't
forEach
. - no-extra-args: Do not use superfluous arguments on Lodash methods with a specified arity.
Stylistic Issues
These rules are purely matters of style and are quite subjective.
- prop-shorthand: Use/forbid property shorthand syntax.
- matches-prop-shorthand: Prefer matches property shorthand syntax
- matches-shorthand: Prefer matches shorthand syntax
- identity-shorthand: Prefer identity shorthand syntax
- preferred-alias: Prefer using main method names instead of aliases. (fixable)
- prefer-chain: Prefer a Lodash chain over nested Lodash calls
- no-single-chain: Prevent chaining syntax for single method, e.g.
_(x).map().value()
- prefer-reject: Prefer
_.reject
over filter with !(expression)
or x.prop1 !== value
- prefer-filter: Prefer
_.filter
over _.forEach
with an if
statement inside. - prefer-compact: Prefer
_.compact
over _.filter
for only truthy values. - prefer-map: Prefer
_.map
over _.forEach
with a push
inside. - prefer-wrapper-method: Prefer using array and string methods in the chain and not the initial value, e.g.
_(str).split(' ')...
- prefer-invoke-map: Prefer using
_.invoke
over _.map
with a method call inside. - prefer-thru: Prefer using
_.prototype.thru
in the chain and not call functions in the initial value, e.g. _(x).thru(f).map(g)...
- no-commit: Do not use
.commit()
on chains that should end with .value()
- chain-style: Enforce a specific chain style: explicit, implicit, or explicit only when necessary.
- prefer-flat-map: Prefer
_.flatMap
over consecutive map
and flatten
. - path-style: Enforce a specific path style for methods like
get
and property
: array, string, or arrays only for deep paths.
Preference over native
These rules are also stylistic choices, but they also recommend using Lodash instead of native functions and constructs.
- prefer-lodash-chain: Prefer using Lodash chains (e.g.
_.map
) over native and mixed chains. - prefer-lodash-method: Prefer using Lodash collection methods (e.g.
_.map
) over native array methods. - prefer-lodash-typecheck: Prefer using
_.is*
methods over typeof
and instanceof
checks when applicable. - prefer-get: Prefer using
_.get
or _.has
over expression chains like a && a.b && a.b.c
. - prefer-matches: Prefer
_.matches
over conditions like a.foo === 1 && a.bar === 2 && a.baz === 3
. - prefer-times: Prefer
_.times
over _.map
without using the iteratee's arguments. - prefer-startswith: Prefer
_.startsWith
over a.indexOf(b) === 0
. - prefer-noop: Prefer
_.noop
over empty functions. - prefer-constant: Prefer
_.constant
over functions returning literals. - prefer-is-nil: Prefer
_.isNil
over checks for both null and undefined. - prefer-over-quantifier: Prefer
_.overSome
and _.overEvery
instead of checks with &&
and ||
for methods that have a boolean check iteratee. - prefer-includes: Prefer
_.includes
over comparing indexOf
to 1-11.
Contributing
Contributions are always welcome! For more info, read our contribution guide.
License
ESLint-plugin-lodash is licensed under the MIT License.