Lodash-specific linting rules for ESLint.
- Version 2.0.0 was released, and now supports importing single methods! For a list of changes, you can check the changelog. For a detailed rundown of breaking changes and additions, you can check the release notes.
Install ESLint either locally or globally.
$ npm install eslint --save-dev
If you installed ESLint globally, you have to install the Lodash plugin globally too. Otherwise, install it locally.
$ npm install eslint-plugin-lodash --save-dev
Add a plugins section and specify ESLint-Plugin-Lodash as a plugin.
You can additionally add settings for the plugin.
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. If none is specified, the plugin checks what was
imported in ES6 modules orrequired in commonjs. - 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 be3. (on by default in the configv3)
Finally, enable all of the rules that you would like to use.
This plugin exports a recommended configuration that enforces all the rules.
You can configure the plugin as follows:
{
"plugins": ["lodash"],
"extends": ["plugin:lodash/recommended"]
}If you work with the full Lodash object with the same variable name every time, you should use the canonical configuration. This allows rules to run without explicitly importing Lodash in your code, and allows for faster execution for some of the rules:
{
"plugins": ["lodash"],
"extends": ["plugin:lodash/canonical"]
}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"]
}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.
The following rules point out areas where you might have made mistakes.
- callback-binding: Use or avoid
thisArgfor Lodash method callbacks, depending on major version. - collection-method-value: Use value returned from collection methods properly.
- collection-return: Always return a value in iteratees of Lodash collection methods that aren't
forEach. - no-double-unwrap: Do not use
.value()on chains that have already ended (e.g. withmax()orreduce()) (fixable) - no-extra-args: Do not use superfluous arguments on Lodash methods with a specified arity.
- no-unbound-this: Do not use
thisinside callbacks without binding them. - unwrap: Prevent chaining without evaluation via
value()or non-chainable methods likemax().,
These rules are purely matters of style and are quite subjective.
- chain-style: Enforce a specific chain style: explicit, implicit, or explicit only when necessary.
- chaining: Prefer a either a Lodash chain or nested Lodash calls
- collection-ordering: Enforce a specific collection sorting method:
sortByororderBy - consistent-compose: Enforce a specific function composition direction:
floworflowRight. - identity-shorthand: Prefer identity shorthand syntax
- import-scope: Prefer a specific import scope (e.g.
lodash/mapvslodash) - matches-prop-shorthand: Prefer matches property shorthand syntax
- matches-shorthand: Prefer matches shorthand syntax
- no-commit: Do not use
.commit()on chains that should end with.value() - path-style: Enforce a specific path style for methods like
getandproperty: array, string, or arrays only for paths with variables. (fixable) - prefer-compact: Prefer
_.compactover_.filterfor only truthy values. - prefer-filter: Prefer
_.filterover_.forEachwith anifstatement inside. - prefer-find: Prefer
_.findover_.filterfollowed by selecting the first result. - prefer-flat-map: Prefer
_.flatMapover consecutivemapandflatten. - prefer-immutable-method: Prefer using methods that do not mutate the source parameters, e.g.
_.withoutinstead of_.pull. - prefer-invoke-map: Prefer using
_.invokeover_.mapwith a method call inside. - prefer-map: Prefer
_.mapover_.forEachwith apushinside. - prefer-reject: Prefer
_.rejectover filter with!(expression)orx.prop1 !== value - prefer-thru: Prefer using
_.prototype.thruin the chain and not call functions in the initial value, e.g._(x).thru(f).map(g)... - prefer-wrapper-method: Prefer using array and string methods in the chain and not the initial value, e.g.
_(str).split(' ')... - preferred-alias: Prefer using main method names instead of aliases. (fixable)
- prop-shorthand: Use/forbid property shorthand syntax.
These rules are also stylistic choices, but they also recommend using Lodash instead of native functions and constructs.
For example, Lodash collection methods (e.g. map, forEach) are generally faster than native collection methods.
- prefer-constant: Prefer
_.constantover functions returning literals. - prefer-get: Prefer using
_.getor_.hasover expression chains likea && a.b && a.b.c. - prefer-includes: Prefer
_.includesover comparingindexOfto -1. - prefer-is-nil: Prefer
_.isNilover checks for both null and undefined. - 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 overtypeofandinstanceofchecks when applicable. - prefer-matches: Prefer
_.matchesover conditions likea.foo === 1 && a.bar === 2 && a.baz === 3. - prefer-noop: Prefer
_.noopover empty functions. - prefer-over-quantifier: Prefer
_.overSomeand_.overEveryinstead of checks with&&and||for methods that have a boolean check iteratee. - prefer-some: Prefer using
_.someover comparingfindIndexto -1. - prefer-startswith: Prefer
_.startsWithovera.indexOf(b) === 0. - prefer-times: Prefer
_.timesover_.mapwithout using the iteratee's arguments.
Contributions are always welcome! For more info, read our contribution guide.
ESLint-plugin-lodash is licensed under the MIT License.