Package Exports
- eslint-plugin-lodash
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eslint-plugin-lodash) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ESLint-Plugin-Lodash
Lodash-specific linting rules for ESLint.
Installation
Install ESLint either locally or globally.
$ npm install eslintIf you installed ESLint globally, you have to install the Lodash plugin globally too. Otherwise, install it locally.
$ npm install eslint-plugin-lodashConfiguration
Add 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, change this value to 3.
Finally, enable all of the rules that you would like to use.
{
"plugins": ["lodash"],
"rules": {
"lodash/callback-binding": 2,
"lodash/chain-style": [2, "as-needed"],
"lodash/collection-return": 2,
"lodash/identity-shorthand": [2, "always"],
"lodash/matches-prop-shorthand": [2, "always"],
"lodash/matches-shorthand": [2, "always", 3],
"lodash/no-commit": 2,
"lodash/no-double-unwrap": 2,
"lodash/no-extra-args": 2,
"lodash/no-single-chain": 2,
"lodash/path-style": [2, "as-needed"],
"lodash/prefer-chain": [2, 3],
"lodash/prefer-compact": 2,
"lodash/prefer-constant": 2,
"lodash/prefer-filter": [2, 3],
"lodash/prefer-flat-map": "2",
"lodash/prefer-get": [2, 3],
"lodash/prefer-invoke-map": 2,
"lodash/prefer-is-nil": 2,
"lodash/prefer-lodash-chain": 2,
"lodash/prefer-lodash-method": 2,
"lodash/prefer-lodash-typecheck": 2,
"lodash/prefer-map": 2,
"lodash/prefer-matches": [2, 3],
"lodash/prefer-noop": 2,
"lodash/prefer-over-quantifier": 2,
"lodash/prefer-reject": [2, 3],
"lodash/prefer-startswith": 2,
"lodash/prefer-thru": 2,
"lodash/prefer-times": 2,
"lodash/prefer-wrapper-method": 2,
"lodash/preferred-alias": 2,
"lodash/prop-shorthand": [2, "always"],
"lodash/unwrap": 2
}
}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:
{
"settings": {
"lodash": {
"version": 3
}
},
"rules": {
"lodash/callback-binding": 2,
"lodash/chain-style": [2, "as-needed"],
"lodash/collection-return": 2,
"lodash/identity-shorthand": [2, "always"],
"lodash/matches-prop-shorthand": [2, "always"],
"lodash/matches-shorthand": [2, "always", 3],
"lodash/no-commit": 2,
"lodash/no-double-unwrap": 2,
"lodash/no-extra-args": 2,
"lodash/no-single-chain": 2,
"lodash/path-style": [2, "as-needed"],
"lodash/prefer-chain": [2, 3],
"lodash/prefer-compact": 2,
"lodash/prefer-constant": 2,
"lodash/prefer-filter": [2, 3],
"lodash/prefer-get": [2, 3],
"lodash/prefer-lodash-chain": 2,
"lodash/prefer-lodash-method": 2,
"lodash/prefer-lodash-typecheck": 2,
"lodash/prefer-map": 2,
"lodash/prefer-matches": [2, 3],
"lodash/prefer-noop": 2,
"lodash/prefer-reject": [2, 3],
"lodash/prefer-startswith": 2,
"lodash/prefer-thru": 2,
"lodash/prefer-times": 2,
"lodash/prefer-wrapper-method": 2,
"lodash/preferred-alias": 2,
"lodash/prop-shorthand": [2, "always"],
"lodash/unwrap": 2
}
}List of provided rules
Rules are divided into categories for your convenience. All rules are off by default.
Possible Errors
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. - unwrap: Prevent chaining without evaluation via
value()or non-chainable methods likemax()., - no-double-unwrap: Do not use
.value()on chains that have already ended (e.g. withmax()orreduce()) (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
_.rejectover filter with!(expression)orx.prop1 !== value - prefer-filter: Prefer
_.filterover_.forEachwith anifstatement inside. - prefer-compact: Prefer
_.compactover_.filterfor only truthy values. - prefer-map: Prefer
_.mapover_.forEachwith apushinside. - 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
_.invokeover_.mapwith a method call inside. - prefer-thru: Prefer using
_.prototype.thruin 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
_.flatMapover consecutivemapandflatten. - path-style: Enforce a specific path style for methods like
getandproperty: 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 overtypeofandinstanceofchecks when applicable. - prefer-get: Prefer using
_.getor_.hasover expression chains likea && a.b && a.b.c. - prefer-matches: Prefer
_.matchesover conditions likea.foo === 1 && a.bar === 2 && a.baz === 3. - prefer-times: Prefer
_.timesover_.mapwithout using the iteratee's arguments. - prefer-startswth: Prefer
_.startsWithovera.indexOf(b) === 0. - prefer-noop: Prefer
_.noopover empty functions. - prefer-constant: Prefer
_.constantover functions returning literals. - prefer-is-nil: Prefer
_.isNilover checks for both null and undefined. - prefer-over-quantifier: Prefer
_.overSomeand_.overEveryinstead of checks with&&and||for methods that have a boolean check iteratee.
Contributing
Contributions are always welcome! For more info, read our contribution guide.
License
ESLint-plugin-lodash is licensed under the MIT License.