Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-lodash

idok1.4mMIT8.0.0

Lodash specific linting rules for ESLint

eslint, eslint-plugin, eslintplugin, lodash

readme

ESLint-Plugin-Lodash

Maintenance Status NPM version Dependencies Build Status

Lodash-specific linting rules for ESLint.

News

  • 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.

Installation

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

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. If none is specified, the plugin checks what was imported in ES6 modules or required 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 be 3. (on by default in the config v3)

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"]
}

Configuration for use with the full Lodash object

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"]
}

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.
  • 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. with max() or reduce()) (fixable)
  • no-extra-args: Do not use superfluous arguments on Lodash methods with a specified arity.
  • no-unbound-this: Do not use this inside callbacks without binding them.
  • unwrap: Prevent chaining without evaluation via value() or non-chainable methods like max().,

Stylistic Issues

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: sortBy or orderBy
  • consistent-compose: Enforce a specific function composition direction: flow or flowRight.
  • identity-shorthand: Prefer identity shorthand syntax
  • import-scope: Prefer a specific import scope (e.g. lodash/map vs lodash)
  • 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 get and property: array, string, or arrays only for paths with variables. (fixable)
  • prefer-compact: Prefer _.compact over _.filter for only truthy values.
  • prefer-filter: Prefer _.filter over _.forEach with an if statement inside.
  • prefer-find: Prefer _.find over _.filter followed by selecting the first result.
  • prefer-flat-map: Prefer _.flatMap over consecutive map and flatten.
  • prefer-immutable-method: Prefer using methods that do not mutate the source parameters, e.g. _.without instead of _.pull.
  • prefer-invoke-map: Prefer using _.invoke over _.map with a method call inside.
  • prefer-map: Prefer _.map over _.forEach with a push inside.
  • prefer-reject: Prefer _.reject over filter with !(expression) or x.prop1 !== value
  • prefer-thru: Prefer using _.prototype.thru in 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.

Preference over native

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 _.constant over functions returning literals.
  • prefer-get: Prefer using _.get or _.has over expression chains like a && a.b && a.b.c.
  • prefer-includes: Prefer _.includes over comparing indexOf to -1.
  • prefer-is-nil: Prefer _.isNil over 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 over typeof and instanceof checks when applicable.
  • prefer-matches: Prefer _.matches over conditions like a.foo === 1 && a.bar === 2 && a.baz === 3.
  • prefer-noop: Prefer _.noop over empty functions.
  • prefer-over-quantifier: Prefer _.overSome and _.overEvery instead of checks with && and || for methods that have a boolean check iteratee.
  • prefer-some: Prefer using _.some over comparing findIndex to -1.
  • prefer-startswith: Prefer _.startsWith over a.indexOf(b) === 0.
  • prefer-times: Prefer _.times over _.map without using the iteratee's arguments.

Contributing

Contributions are always welcome! For more info, read our contribution guide.

License

ESLint-plugin-lodash is licensed under the MIT License.

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

v8.0.0

v8.0.0

9 May 2024

  • Bump eslint-config-wix-editor from 8.1.0 to 8.4.0 #293
  • fix unwrap rule #239
  • Make Lodash a full dependency #255
  • Bump eslint from 6.8.0 to 7.7.0 #262
  • Improve test coverage for matches-shorthand & matches-prop-shorthand. Fix incorrect iteratee & shorthand method data uncovered thereby. #220
  • fix full lodash import trailing slash #195
  • s/ignorePatterns/ignoreObjects #223
  • docs: add collection-ordering to readme #224
  • fix: Autofix for paths with double member access #230
  • infra: upgrade all deps; drop Node 8 support #231
  • Bump babel-plugin-istanbul from 5.2.0 to 6.0.0 #228
  • Bump handlebars from 4.1.2 to 4.5.3 #226
  • Bump js-yaml from 3.12.0 to 3.13.1 #217
  • Bump handlebars from 4.0.10 to 4.1.2 #210
  • Bump debug from 2.6.8 to 2.6.9 #216
  • Fix typo #196
  • fix: Lodash security vulnerability #208
  • Remove redundant comma #197
  • add eslint peer dependency #85
  • fix and improve prefer-flat-map docs #72
  • Sync chainable in v4 with lodash master. Fixes #49 #52
  • Fix the property shorthand for lodash v4 #47
  • close #42 removes quote in lodash/recommended #43
  • Output correct column number for matches-shorthand #19
  • Being consistent with other rule declarations that include default va… #7
  • missings commas in rules example in readme #6
  • Fix typo in readme. #2
  • Add rule collection-ordering (fixes #190) #190
  • Add mechanism for methods to support only some shorthands (fixes #189) #189
  • Prevent prefer-lodash-method error on chain() when imported as member import (fixes #186) #186
  • add implicit option to chaining rule (fixes #184) #184
  • add cases and fixer to path-style (fixes #185) #185
  • add rule prefer-find (fixes #175) #175
  • add rule prefer-immutable-method (fixes #154) #154
  • Add correct example of using _.compact (fixes #165) #165
  • make prefer-compact report on filter with identity (fixes #177) #177
  • add ignoreMethods option for rule preferred-alias (fixes #171) #171
  • Docs: Fix identity-shorthand example (closes #164) #164
  • add option to import from method packages in import-scope (fixes #163) #163
  • fix rule prefer-invoke-map to work with eslint >= 4.4.0 (fixes #156) #156
  • add clean script before every babel (fixes #153) #153
  • fix prefer-over-quantifier to only report on single-argument functions (fixes #152) #152
  • Add rule prefer-some (fixes #145) #145
  • change structure of method data (fixes #144) #144
  • add sortedUniq and sortedUniqBy as chainable methods (fixes #143) #143
  • adding more string methods to prefer-lodash-method rule (fixes #136) #136
  • Fix import-scope with lodash/fp (fixes #134) #134
  • fix prefer-noop to stop reporting on async and generator functions (fixes #132) #132
  • allow async functions in collection-return (fixes #131) #131
  • Fix import-scope to allow import with asterisk in full config (fixes #130) #130
  • make prefer-invoke-map only check object usages (fixes #128) #128
  • fix matches-prop-shorthand warning when both sides are a member expression of the parameter (fixes #127) #127
  • add support for Lodash-es (fixes #126) #126
  • add rule no-unbound-this (fixes #120) #120
  • fix crash in prefer-over-quantifier (fixes #118) #118
  • fix preferred-alias case where method is on Object.prototype (fixes #117) #117
  • fix callback-binding not to warn on find methods (fixes #86) #86
  • ignore Object.prototype methods when checking for Lodash imports (fixes #115) #115
  • upgrade eslint-traverser to correct 1.5.2 version (fixes #114) #114
  • report on callExp instead of method for cases of single method import (fixes #113) #113
  • refactor prefer-over-quantifier to warn on identity shorthand in chains (fixes #111) #111
  • change native includes to lodash/includes for earlier node versions (fixes #109) #109
  • make hasOnlyOneStatement return falsy for nodes that aren't functions (fixes #108) #108
  • fix crash in getImportedLodashMethod (fixes #107) #107
  • make prefer-lodash-method warn on string methods (fixes #99) #99
  • clarify docs for prefer-reject (fixes #105) #105
  • reimplement path-style rule (fixes #106) #106
  • fix isMemberExpOf to return false on computed values when allowComputed is false (fixes #91) #91
  • make prefer-map stop reporting if the push is from a declared parameter (fixes #92) #92
  • redefine prefer-chain and no-single-chain into a single chaining rule (fixes #82) #82
  • make rules work for single method imports and requires (fixes #80) #80
  • fix prefer-lodash-typecheck to warn on undefined checks of declared variables (fixes #21) #21
  • make prefer-lodash-method not warn on Object.create for null (fixes #94) #94
  • update prefer-lodash-method docs (fixes #98) #98
  • make prefer-invoke-map only warn on functions with a named first argument (fixes #93) #93
  • add comment about lodash performance in preference over native (fixes #90) #90
  • add docs and option schema for the ignorePatterns option of #88
  • fix arg count for lodash 4 find, findIndex, findLast, findLastIndex (fixes #86) #86
  • create rule consistent-compose (fixes #79) #79
  • change prefer-matches error message to suggest _.isMatch (fixes #77) #77
  • fix prefer-lodash-method to accept complex objects to ignoreObjects parameter (fixes #78) #78
  • fix collection-return to stop handling blockless arrow functions (fixes #75) #75
  • make prefer-times check all levels in the call stack (fixes #73) #73
  • add option to prefer-constant to handle function declarations (fixes #70) #70
  • add option ignoreObjects to rule prefer-lodash-method (fixes #68) #68
  • fix getLodashMethodVisitor false positive when called by function with lodash method name (fixes #69) #69
  • fix prefer-noop to not warn if the empty function is a method definition (fixes #66) #66
  • remove number of arguments from _.attempt (fixes #65) #65
  • remove zipWith from list of methods with fixed-index iteratee (fixes #64) #64
  • remove slice from list of native collection method calls as it behaves differently with strings (fixes #63) #63
  • add _.negate as a negative condition in prefer-reject (fixes #62) #62
  • change recommended config of path-style to string (fixes #58) #58
  • change default config of path-style to string and fix docs (fixes #60) #60
  • add onlyLiterals option to prefer-matches and prefer-matches-prop #48
  • add rule collection-method-value (fixes #54) #54
  • add missed cases to prefer-includes (fixes #53) #53
  • change isLodashWrapper to handle explicit chaining (fixes #50) #50
  • Merge pull request #52 from lukeapage/bugfix-49 #49
  • Add methods from 4.6.0. Fixes #51 #51
  • Sync chainable in v4 with lodash master. Fixes #49 #49
  • add rule prefer-includes (fixes #46) #46
  • SortedLastIndexBy has an iterator on the 2nd arg #44
  • Merge pull request #43 from godu/master #42
  • add recommended and v3 rule configs (fixes #40) #40
  • add option for exceptions in prefer-lodash-method (fixes #38) #38
  • fix false positive for prefer-lodash-chain where callback uses lodash (fixes #37) #37
  • fix prefer-times when using destructuring (fixes #32) #32
  • add rule no-extra-args (fixes #26) #26
  • fix bug in prefer-filter where _ isn't lodash (fixes #23) #23
  • refactor ecmaFeature flag to settingsUtil to support both ESLint v1 and v2 (fixes #22) #22
  • Fix examples in prop-shorthand.md (fixes #16) #16
  • add support for settings object with pragma (fixes #1) #1
  • implement rule prefer-startswith (fixes #11) #11
  • Suggest to replace indexOf(..., '') === 0 with _.startsWith. Fixes #11 #11
  • exclude typeof === undefined from prefer-lodash-typecheck (fix #5) #5
  • update dependencies and fix lint 2c3edb0
  • bump deps and fix errors e6cd66f
  • bump yarn f31dc1d