Forked from eslint-plugin-lodash
Linting rules for Underscore
Install ESLint either locally or globally.
npm install eslint
If you installed ESLint globally, you have to install the Underscore plugin
globally too. Otherwise, install it locally.
$ npm install eslint-plugin-underscore
Add plugins section and specify ESLint-plugin-underscore as a plugin.
{
"plugins": ["underscore"]
}Finally, enable all of the rules that you would like to use.
{
"rules": {
"underscore/collection-return": 2,
"underscore/identity-shorthand": [2, "always"],
"underscore/jquery-each": [2, "never"],
"underscore/jquery-extend": [2, "never"],
"underscore/jquery-proxy": [2, "never"],
"underscore/matches-shorthand": [2, "always"],
"underscore/no-return-value-from-each-iteratee": 2,
"underscore/no-unnecessary-bind": 2,
"underscore/prefer-chain": [2, 3],
"underscore/prefer-compact": 2,
"underscore/prefer-constant": 2,
"underscore/prefer-filter": 2,
"underscore/prefer-findwhere": 2,
"underscore/prefer-invoke": 2,
"underscore/prefer-map": 2,
"underscore/prefer-matches": 2,
"underscore/prefer-noop": 2,
"underscore/prefer-pick": [2, {objectsLargerThan: 3}],
"underscore/prefer-pluck": 2,
"underscore/prefer-reject": 2,
"underscore/prefer-times": 2,
"underscore/prefer-underscore-method": 2,
"underscore/prefer-underscore-typecheck": 2,
"underscore/prefer-where": 2,
"underscore/preferred-alias": 2,
"underscore/prop-shorthand": [2, "always"],
// The below rules are not (yet) supported
"underscore/no-single-chain": 2,
"underscore/unwrap": 2,
"underscore/no-double-unwrap": 2,
"underscore/prefer-wrapper-method": 2,
"underscore/prefer-lodash-chain": 2,
"underscore/chain-style": [2, "as-needed"]
}
}- collection-return: Always return a value in iteratees of Underscore collection methods that aren't
each. - identity-shorthand: Prefer identity shorthand syntax.
- jquery-each: Standardize on either Underscore's or jQuery's each function.
- jquery-extend: Standardize on either Underscore's or jQuery's extend function.
- jquery-proxy: Standardize on either Underscore's bind or jQuery's proxy function.
- matches-shorthand: Prefer matches shorthand syntax.
- no-return-value-from-each-iteratee: Do not return a value from the iteratee of
_.each. - no-unnecessary-bind: Prefer passing
thisArgover binding. - prefer-chain: Prefer chain over nested Underscore calls.
- prefer-compact: Prefer
_.compactover_.filterfor only truthy values. - prefer-constant: Prefer
_.constantover functions returning literals. - prefer-filter: Prefer
_.filterover_.eachwith anifstatement inside. - prefer-findwhere: Prefer
_.findWhereover_.findwhen using matcher shorthand. (fixable) - prefer-invoke: Prefer using
_.invokeover_.mapwith a method call inside. - prefer-map: Prefer
_.mapover_.eachwith apushinside. - prefer-matches: Prefer
_.matchesover conditions likea.foo === 1 && a.bar === 2 && a.baz === 3. - prefer-noop: Prefer
_.noopover empty functions. - prefer-pick: Prefer
_.pickover defining an object that is a subset of another object. - prefer-pluck: Prefer
_.pluckover_.mapwhen using property shorthand. (fixable) - prefer-reject: Prefer
_.rejectover filter with!(expression)orx.prop1 !== value. - prefer-times: Prefer
_.timesover_.mapwithout using the iteratee's arguments. - prefer-underscore-method: Prefer using Underscore collection methods (e.g.
_.map) over native array methods. - prefer-underscore-typecheck: Prefer using
_.is*methods overtypeofandinstanceofchecks when applicable. - prefer-where: Prefer
_.whereover_.filterwhen using matcher shorthand. (fixable) - preferred-alias: Preferred aliases.
- prop-shorthand: Prefer property shorthand syntax. (fixable)
- no-single-chain: Prevent chaining syntax for single method, e.g.
_(x).map().value(). - 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()). - prefer-wrapper-method: Prefer using array and string methods in the chain and not the initial value, e.g.
_(str).split(' ')... - prefer-lodash-chain: Prefer using Lodash chains (e.g.
_.map) over native and mixed chains. - chain-style: Enforce a specific chain style: explicit, implicit, or explicit only when necessary.
ESLint-plugin-underscore is licensed under the MIT License.