1 | ESLint-Plugin-Lodash
|
2 | ===================
|
3 |
|
4 | [![Maintenance Status][status-image]][status-url] [![NPM version][npm-image]][npm-url] [![Dependencies][deps-image]][deps-url] [![Build Status][build-image]][build-url]
|
5 |
|
6 | Lodash-specific linting rules for ESLint.
|
7 | # News
|
8 | - Version 2.0.0 was released, and now supports importing single methods! For a list of changes, you can check the [changelog](CHANGELOG.md).
|
9 | For a detailed rundown of breaking changes and additions, you can check the [release notes](https://github.com/wix/eslint-plugin-lodash/wiki/Release-Notes).
|
10 |
|
11 | # Installation
|
12 |
|
13 | Install [ESLint](https://www.github.com/eslint/eslint) either locally or globally.
|
14 |
|
15 | $ npm install eslint
|
16 |
|
17 | If you installed `ESLint` globally, you have to install the Lodash plugin globally too. Otherwise, install it locally.
|
18 |
|
19 | $ npm install eslint-plugin-lodash
|
20 |
|
21 | # Configuration
|
22 |
|
23 | Add a `plugins` section and specify ESLint-Plugin-Lodash as a plugin.
|
24 | You can additionally add settings for the plugin.
|
25 |
|
26 | ### Shared Rule Settings
|
27 | 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](http://eslint.org/docs/user-guide/configuring#adding-shared-settings).
|
28 |
|
29 | * __pragma__: specifies the name you use for the Lodash variable in your code. If none is specified, the plugin checks what was `import`ed in ES6 modules or `require`d in commonjs.
|
30 | * __version__: specifies the major Lodash Version you are using (default is `4`).
|
31 | If you wish to use this plugin with Lodash v3, this value should be `3`. (on by default in the config `v3`)
|
32 |
|
33 |
|
34 | Finally, enable all of the rules that you would like to use.
|
35 | ## Recommended configuration
|
36 | This plugin exports a `recommended` configuration that enforces all the rules.
|
37 | You can configure the plugin as follows:
|
38 |
|
39 | ```json
|
40 | {
|
41 | "plugins": ["lodash"],
|
42 | "extends": ["plugin:lodash/recommended"]
|
43 | }
|
44 | ```
|
45 | ## Configuration for use with the full Lodash object
|
46 | 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:
|
47 |
|
48 | ```json
|
49 | {
|
50 | "plugins": ["lodash"],
|
51 | "extends": ["plugin:lodash/canonical"]
|
52 | }
|
53 | ```
|
54 |
|
55 | ## Configuration for Using with Lodash v3
|
56 | 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.
|
57 | The plugin also exports a `v3` config for ease of use.
|
58 |
|
59 | ```json
|
60 | {
|
61 | "plugins": ["lodash"],
|
62 | "extends": ["plugin:lodash/v3"]
|
63 | }
|
64 | ```
|
65 |
|
66 | # List of provided rules
|
67 | 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.
|
68 | ### Possible Errors
|
69 | The following rules point out areas where you might have made mistakes.
|
70 |
|
71 | * [callback-binding](docs/rules/callback-binding.md): Use or avoid `thisArg` for Lodash method callbacks, depending on major version.
|
72 | * [collection-method-value](docs/rules/collection-method-value.md): Use value returned from collection methods properly.
|
73 | * [collection-return](docs/rules/collection-return.md): Always return a value in iteratees of Lodash collection methods that aren't `forEach`.
|
74 | * [no-double-unwrap](docs/rules/no-double-unwrap.md): Do not use `.value()` on chains that have already ended (e.g. with `max()` or `reduce()`) (fixable)
|
75 | * [no-extra-args](docs/rules/no-extra-args.md): Do not use superfluous arguments on Lodash methods with a specified arity.
|
76 | * [no-unbound-this](docs/rules/no-unbound-this.md): Do not use `this` inside callbacks without binding them.
|
77 | * [unwrap](docs/rules/unwrap.md): Prevent chaining without evaluation via `value()` or non-chainable methods like `max()`.,
|
78 |
|
79 | ### Stylistic Issues
|
80 | These rules are purely matters of style and are quite subjective.
|
81 | * [chain-style](docs/rules/chain-style.md): Enforce a specific chain style: explicit, implicit, or explicit only when necessary.
|
82 | * [chaining](docs/rules/chaining.md): Prefer a either a Lodash chain or nested Lodash calls
|
83 | * [consistent-compose](docs/rules/consistent-compose.md): Enforce a specific function composition direction: `flow` or `flowRight`.
|
84 | * [identity-shorthand](docs/rules/identity-shorthand.md): Prefer identity shorthand syntax
|
85 | * [import-scope](docs/rules/import-scope.md): Prefer a specific import scope (e.g. `lodash/map` vs `lodash`)
|
86 | * [matches-prop-shorthand](docs/rules/matches-prop-shorthand.md): Prefer matches property shorthand syntax
|
87 | * [matches-shorthand](docs/rules/matches-shorthand.md): Prefer matches shorthand syntax
|
88 | * [no-commit](docs/rules/no-commit.md): Do not use `.commit()` on chains that should end with `.value()`
|
89 | * [path-style](docs/rules/path-style.md): Enforce a specific path style for methods like `get` and `property`: array, string, or arrays only for paths with variables.
|
90 | * [prefer-compact](docs/rules/prefer-compact.md): Prefer `_.compact` over `_.filter` for only truthy values.
|
91 | * [prefer-filter](docs/rules/prefer-filter.md): Prefer `_.filter` over `_.forEach` with an `if` statement inside.
|
92 | * [prefer-flat-map](docs/rules/prefer-flat-map.md): Prefer `_.flatMap` over consecutive `map` and `flatten`.
|
93 | * [prefer-invoke-map](docs/rules/prefer-invoke-map.md): Prefer using `_.invoke` over `_.map` with a method call inside.
|
94 | * [prefer-map](docs/rules/prefer-map.md): Prefer `_.map` over `_.forEach` with a `push` inside.
|
95 | * [prefer-reject](docs/rules/prefer-reject.md): Prefer `_.reject` over filter with `!(expression)` or `x.prop1 !== value`
|
96 | * [prefer-thru](docs/rules/prefer-thru.md): Prefer using `_.prototype.thru` in the chain and not call functions in the initial value, e.g. `_(x).thru(f).map(g)...`
|
97 | * [prefer-wrapper-method](docs/rules/prefer-wrapper-method.md): Prefer using array and string methods in the chain and not the initial value, e.g. `_(str).split(' ')...`
|
98 | * [preferred-alias](docs/rules/preferred-alias.md): Prefer using main method names instead of aliases. (fixable)
|
99 | * [prop-shorthand](docs/rules/prop-shorthand.md): Use/forbid property shorthand syntax.
|
100 |
|
101 | #### Preference over native
|
102 | These rules are also stylistic choices, but they also recommend using Lodash instead of native functions and constructs.
|
103 | For example, Lodash collection methods (e.g. `map`, `forEach`) are generally faster than native collection methods.
|
104 | * [prefer-constant](docs/rules/prefer-constant.md): Prefer `_.constant` over functions returning literals.
|
105 | * [prefer-get](docs/rules/prefer-get.md): Prefer using `_.get` or `_.has` over expression chains like `a && a.b && a.b.c`.
|
106 | * [prefer-includes](docs/rules/prefer-includes.md): Prefer `_.includes` over comparing `indexOf` to -1.
|
107 | * [prefer-is-nil](docs/rules/prefer-is-nil.md): Prefer `_.isNil` over checks for both null and undefined.
|
108 | * [prefer-lodash-chain](docs/rules/prefer-lodash-chain.md): Prefer using Lodash chains (e.g. `_.map`) over native and mixed chains.
|
109 | * [prefer-lodash-method](docs/rules/prefer-lodash-method.md): Prefer using Lodash collection methods (e.g. `_.map`) over native array methods.
|
110 | * [prefer-lodash-typecheck](docs/rules/prefer-lodash-typecheck.md): Prefer using `_.is*` methods over `typeof` and `instanceof` checks when applicable.
|
111 | * [prefer-matches](docs/rules/prefer-matches.md): Prefer `_.matches` over conditions like `a.foo === 1 && a.bar === 2 && a.baz === 3`.
|
112 | * [prefer-noop](docs/rules/prefer-noop.md): Prefer `_.noop` over empty functions.
|
113 | * [prefer-over-quantifier](docs/rules/prefer-over-quantifier.md): Prefer `_.overSome` and `_.overEvery` instead of checks with `&&` and `||` for methods that have a boolean check iteratee.
|
114 | * [prefer-some](docs/rules/prefer-some.md): Prefer using `_.some` over comparing `findIndex` to -1.
|
115 | * [prefer-startswith](docs/rules/prefer-startswith.md): Prefer `_.startsWith` over `a.indexOf(b) === 0`.
|
116 | * [prefer-times](docs/rules/prefer-times.md): Prefer `_.times` over `_.map` without using the iteratee's arguments.
|
117 |
|
118 | # Contributing
|
119 | Contributions are always welcome! For more info, read our [contribution guide](.github/CONTRIBUTING.md).
|
120 |
|
121 | # License
|
122 |
|
123 | ESLint-plugin-lodash is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
124 |
|
125 | [npm-url]: https://npmjs.org/package/eslint-plugin-lodash
|
126 | [npm-image]: http://img.shields.io/npm/v/eslint-plugin-lodash.svg?style=flat-square
|
127 |
|
128 | [deps-url]: https://david-dm.org/wix/eslint-plugin-lodash
|
129 | [deps-image]: https://img.shields.io/david/dev/wix/eslint-plugin-lodash.svg?style=flat-square
|
130 |
|
131 |
|
132 | [status-url]: https://github.com/wix/eslint-plugin-lodash/pulse
|
133 | [status-image]: http://img.shields.io/badge/status-maintained-brightgreen.svg?style=flat-square
|
134 |
|
135 | [build-image]: https://travis-ci.org/wix/eslint-plugin-lodash.svg?branch=master
|
136 | [build-url]: https://travis-ci.org/wix/eslint-plugin-lodash
|