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

Added a rule to R.cond warning if the last condition is not a R.T. #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Configure it in `package.json`.
"ramda/complement-simplification": "error",
"ramda/compose-pipe-style": "error",
"ramda/compose-simplification": "error",
"ramda/cond-default": "error",
"ramda/cond-simplification": "error",
"ramda/either-simplification": "error",
"ramda/eq-by-simplification": "error",
Expand Down Expand Up @@ -67,6 +68,7 @@ Configure it in `package.json`.
- `complement-simplification` - Forbids confusing `complement`, suggesting a better one
- `compose-pipe-style` - Enforces `compose` for single line expression and `pipe` for multiline
- `compose-simplification` - Detects when a function that has the same behavior already exists
- `cond-default` - Enforces `cond` having `R.T` as the last condition
- `cond-simplification` - Forbids using `cond` when `ifElse`, `either` or `both` fits
- `either-simplification` - Suggests transforming negated `either` conditions on negated `both`
- `eq-by-simplification` - Forbids `eqBy(prop(_))` and suggests `eqProps`
Expand Down
43 changes: 43 additions & 0 deletions rules/cond-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
const R = require('ramda');
const ast = require('../ast-helper');

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

const create = context => ({
CallExpression(node) {
const match = isCalling({
name: 'cond',
arguments: R.tryCatch(
R.pipe(
R.head,
R.prop('elements'),
R.last,
R.prop('elements'),
R.head,
isRamdaMethod('T'),
R.not
),
R.F
)
});

if (match(node)) {
context.report({
node,
message: '`cond` should finish with a `T` condition so it returns a default value'
});
}
}
});

module.exports = {
create,
meta: {
docs: {
description: '`cond` default, like suggesting returning a default value',
recommended: 'error'
}
}
};
37 changes: 37 additions & 0 deletions test/cond-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/cond-default';

const ruleTester = avaRuleTester(test, {
env: {
es6: true
},
parserOptions: {
sourceType: 'module'
}
});

const error = {
ruleId: 'cond-default',
message: '`cond` should finish with a `T` condition so it returns a default value'
};

ruleTester.run('cond-default', rule, {
valid: [
'cond([[a, b], [T, d]])',
'R.cond([[a, b], [R.T, d]])',
'cond([[a, b], [c, d], [T, f]])',
'R.cond([[a, b], [c, d], [R.T, f]])',
'cond(anything)'
],
invalid: [
{
code: 'cond([[a, b], [c, d]])',
errors: [error]
},
{
code: 'cond([[a, b], [c, d], [e, f]])',
errors: [error]
}
]
});