Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and fix error reporting for prefer-complement #18

Merged
merged 1 commit into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions rules/prefer-complement.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,40 @@ const ast = require('../ast-helper');

const isCalling = ast.isCalling;
const isRamdaMethod = ast.isRamdaMethod;
const getName = ast.getName;

const create = context => ({
CallExpression(node) {
const matchCompose = isCalling({
name: 'compose',
arguments: R.both(
R.propEq('length', 2),
R.where({
0: isRamdaMethod('not'),
})
R.propEq('length', 2),
R.where({
0: isRamdaMethod('not'),
})
)
});

const matchPipe = isCalling({
name: 'pipe',
arguments: R.both(
R.propEq('length', 2),
R.where({
1: isRamdaMethod('not'),
})
R.propEq('length', 2),
R.where({
1: isRamdaMethod('not'),
})
)
});

if (matchCompose(node)) {
const fn = getName(node.arguments[1]);
context.report({
node,
message: `Instead of \`compose(not, ${fn})\`, prefer \`complement(${fn})\``
message: `Instead of \`compose(not, ...)\`, prefer \`complement(...)\``
});
}

if (matchPipe(node)) {
const fn = getName(node.arguments[0]);
context.report({
node,
message: `Instead of \`pipe(${fn}, not)\`, prefer \`complement(${fn})\``
message: `Instead of \`pipe(..., not)\`, prefer \`complement(...)\``
});
}
}
Expand Down
51 changes: 39 additions & 12 deletions test/prefer-complement.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ const ruleTester = avaRuleTester(test, {
}
});

const instead = (composition, fn) => (composition === 'compose' ? `compose(not, ${fn})` : `pipe(${fn}, not)`);
const instead = {
compose: 'compose(not, ...)',
pipe: 'pipe(..., not)'
};

const error = (composition, fn) => ({
ruleId: 'prefer-complement',
message: `Instead of \`${instead(composition, fn)}\`, prefer \`complement(${fn})\``
const error = composition => ({
ruleId: 'prefer-complement',
message: `Instead of \`${instead[composition]}\`, prefer \`complement(...)\``
});

ruleTester.run('prefer-complement', rule, {
Expand All @@ -33,35 +36,59 @@ ruleTester.run('prefer-complement', rule, {
invalid: [
{
code: 'compose(not, isEmpty)',
errors: [error('compose', 'isEmpty')]
errors: [error('compose')]
},
{
code: 'compose(not, xs => xs.length)',
errors: [error('compose')]
},
{
code: 'pipe(isEmpty, not)',
errors: [error('pipe', 'isEmpty')]
errors: [error('pipe')]
},
{
code: 'pipe(function (xs) { return xs.length === 0; }, not)',
errors: [error('pipe')]
},
{
code: 'compose(not, isNil)',
errors: [error('compose', 'isNil')]
errors: [error('compose')]
},
{
code: 'compose(not, R.isNil)',
errors: [error('compose')]
},
{
code: 'pipe(isNil, not)',
errors: [error('pipe', 'isNil')]
errors: [error('pipe')]
},
{
code: 'pipe(R.isNil, not)',
errors: [error('pipe')]
},
{
code: 'compose(not, foo)',
errors: [error('compose', 'foo')]
errors: [error('compose')]
},
{
code: 'compose(not, foo(bar))',
errors: [error('compose')]
},
{
code: 'pipe(foo, not)',
errors: [error('pipe', 'foo')]
errors: [error('pipe')]
},
{
code: 'pipe(foo(bar), not)',
errors: [error('pipe')]
},
{
code: 'propSatisfies(compose(not, isNil))',
errors: [error('compose', 'isNil')]
errors: [error('compose')]
},
{
code: 'propSatisfies(pipe(isNil, not))',
errors: [error('pipe', 'isNil')]
errors: [error('pipe')]
}
]
});