Skip to content

Commit

Permalink
change rule no-commit to not accept forEach cases since forEach break…
Browse files Browse the repository at this point in the history
…s chains in v4
  • Loading branch information
ganimomer committed Jan 25, 2016
1 parent 90d057c commit c28ba30
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
5 changes: 1 addition & 4 deletions docs/rules/no-commit.md
Expand Up @@ -2,7 +2,6 @@

Using `_.prototype.commit()` at the end of the chain executes the chain but doesn't unwrap the value.
In most cases, this means that `_.prototype.value()` would be preferable.
However, in cases of running `forEach`, the chain could be executed only for the side effect.

## Rule Details

Expand All @@ -20,13 +19,11 @@ The following patterns are not considered warnings:

```js

_(a).map(f).forEach(g).commit();

_(a).map(f).filter(g).value();

```


## When Not To Use It

If you do not want to disallow using `commit`, you should not use this rule.
If you do not want to disallow using `commit`, you should not use this rule.
6 changes: 3 additions & 3 deletions lib/rules/no-commit.js
@@ -1,5 +1,5 @@
/**
* @fileoverview Rule to disallow using _.prototype.commit unless used after a function that only runs for its side effect.
* @fileoverview Rule to disallow using _.prototype.commit.
*/
'use strict';

Expand All @@ -17,8 +17,8 @@ module.exports = function (context) {
do {
node = node.parent.parent;
} while (astUtil.isMethodCall(node) && !lodashUtil.isCallToMethod(node, settings.version, 'commit'));
if (lodashUtil.isCallToMethod(node, settings.version, 'commit') && !lodashUtil.isCallToMethod(astUtil.getCaller(node), settings.version, 'forEach')) {
context.report(node, 'Do not end chain with commit, except for side effects.');
if (lodashUtil.isCallToMethod(node, settings.version, 'commit')) {
context.report(node, 'Do not end chain with commit.');
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/lib/rules/no-commit.js
Expand Up @@ -12,12 +12,11 @@ var RuleTester = require('eslint').RuleTester;
// ------------------------------------------------------------------------------

var ruleTester = new RuleTester();
var errors = [{message: 'Do not end chain with commit, except for side effects.'}];
var errors = [{message: 'Do not end chain with commit.'}];
ruleTester.run('prefer-map', rule, {
valid: [
'var x = _(a).map(f).value();',
'_(a).filter(f).forEach(g).value()',
'_(a).filter(f).forEach(g).commit()'
'_(a).filter(f).forEach(g);'
],
invalid: [{
code: '_(arr).map(f).commit();',
Expand Down

0 comments on commit c28ba30

Please sign in to comment.