-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from jogold/prefer-both-either
Add prefer-both-either
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
const R = require('ramda'); | ||
const ast = require('../ast-helper'); | ||
|
||
const isCalling = ast.isCalling; | ||
const getName = ast.getName; | ||
|
||
const prefer = { | ||
allPass: 'both', | ||
anyPass: 'either' | ||
} | ||
|
||
const match = name => isCalling({ | ||
name, | ||
arguments: R.where({ | ||
0: R.both( | ||
R.propEq('type', 'ArrayExpression'), | ||
R.pathEq(['elements', 'length'], 2) | ||
), | ||
}) | ||
}) | ||
|
||
const elementsToString = R.pipe( | ||
R.prop('elements'), | ||
R.map(getName), | ||
R.join(', ') | ||
); | ||
|
||
const report = instead => `Instead of \`${instead}\`, prefer \`${prefer[instead]}\` when there are only two predicates` | ||
|
||
const create = context => ({ | ||
CallExpression(node) { | ||
if (match('allPass')(node) || match('anyPass')(node)) { | ||
const callee = getName(node.callee); | ||
context.report({ | ||
node, | ||
message: report(callee) | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
description: 'Enforces using `both`/`either` instead of `allPass`/`anyPass` with a list of only two predicates', | ||
recommended: 'off' | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prefer-both-either'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
}, | ||
parserOptions: { | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
const prefer = { | ||
allPass: 'both', | ||
anyPass: 'either' | ||
} | ||
|
||
const error = instead => ({ | ||
ruleId: 'prefer-both-either', | ||
message: `Instead of \`${instead}\`, prefer \`${prefer[instead]}\` when there are only two predicates` | ||
}); | ||
|
||
ruleTester.run('prefer-both-either', rule, { | ||
valid: [ | ||
'both(foo, bar)', | ||
'either(foo, bar)', | ||
'allPass([foo, bar, baz])', | ||
'anyPass([foo, bar, baz])', | ||
'allPass(predicates)', | ||
'allPass(predicates, foo)', | ||
'anyPass(predicates)', | ||
'anyPass(predicates, foo)' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'allPass([foo, bar])', | ||
errors: [error('allPass')] | ||
}, | ||
{ | ||
code: 'allPass([foo, bar], baz)', | ||
errors: [error('allPass')] | ||
}, | ||
{ | ||
code: 'allPass([(foo) => !foo, function () { return false; }])', | ||
errors: [error('allPass')] | ||
}, | ||
{ | ||
code: 'allPass([complement(foo), complement(bar)])', | ||
errors: [error('allPass')] | ||
}, | ||
{ | ||
code: 'anyPass([foo, bar])', | ||
errors: [error('anyPass')] | ||
}, | ||
{ | ||
code: 'anyPass([foo, bar], baz)', | ||
errors: [error('anyPass')] | ||
}, | ||
{ | ||
code: 'anyPass([R.T, R.F])', | ||
errors: [error('anyPass')] | ||
} | ||
] | ||
}); |