-
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 #9 from ramda/new-rules
Detect new code smells
- Loading branch information
Showing
13 changed files
with
406 additions
and
12 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
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,45 @@ | ||
'use strict'; | ||
const R = require('ramda'); | ||
const ast = require('../ast-helper'); | ||
|
||
const isCalling = ast.isCalling; | ||
const isBooleanLiteral = ast.isBooleanLiteral; | ||
|
||
const report = (instead, prefer) => `\`always(${instead})\` should be simplified to \`${prefer}\``; | ||
const alternatives = { | ||
'true': 'T', | ||
'false': 'F' | ||
}; | ||
|
||
const create = context => ({ | ||
CallExpression(node) { | ||
const match = isCalling({ | ||
name: 'always', | ||
arguments: R.both( | ||
R.propEq('length', 1), | ||
R.where({ | ||
0: isBooleanLiteral | ||
}) | ||
) | ||
}); | ||
|
||
if (match(node)) { | ||
const instead = node.arguments[0].value; | ||
|
||
context.report({ | ||
node, | ||
message: report(instead, alternatives[instead]) | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
description: 'Detects cases where `always` is redundant', | ||
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,38 @@ | ||
'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: 'compose', | ||
arguments: R.both( | ||
R.propSatisfies(R.lte(2), 'length'), | ||
R.where({ | ||
0: isRamdaMethod('flatten'), | ||
1: isRamdaMethod('map') | ||
}) | ||
) | ||
}); | ||
|
||
if (match(node)) { | ||
context.report({ | ||
node, | ||
message: '`compose(flatten, map)` should be simplified to `chain`' | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
description: 'Detects when there are better functions that behave the same as `compose`', | ||
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
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,38 @@ | ||
'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: 'pipe', | ||
arguments: R.both( | ||
R.propSatisfies(R.lte(2), 'length'), | ||
R.where({ | ||
0: isRamdaMethod('map'), | ||
1: isRamdaMethod('flatten') | ||
}) | ||
) | ||
}); | ||
|
||
if (match(node)) { | ||
context.report({ | ||
node, | ||
message: '`pipe(map, flatten)` should be simplified to `chain`' | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
description: 'Detects when there are better functions that behave the same as `pipe`', | ||
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,64 @@ | ||
'use strict'; | ||
const R = require('ramda'); | ||
const isBooleanLiteral = require('../ast-helper').isBooleanLiteral; | ||
|
||
const report = (instead, prefer) => `Instead of \`() => ${instead}\`, prefer \`${prefer}\``; | ||
const getAlternative = R.applyTo(R.__, R.compose(R.toUpper, R.head, R.toString)); | ||
|
||
// :: Node -> Boolean | ||
const onlyReturnsBoolean = R.where({ | ||
type: R.equals('BlockStatement'), | ||
body: R.both( | ||
R.propEq('length', 1), | ||
R.where({ | ||
0: R.where({ | ||
type: R.equals('ReturnStatement'), | ||
argument: R.both( | ||
R.complement(R.isNil), | ||
isBooleanLiteral | ||
) | ||
}) | ||
}) | ||
) | ||
}); | ||
|
||
// Node -> String | ||
const getRawReturn = R.ifElse( | ||
R.propEq('type', 'BlockStatement'), | ||
R.path(['body', 0, 'argument', 'value']), | ||
R.prop('value') | ||
); | ||
|
||
const create = context => ({ | ||
ArrowFunctionExpression(node) { | ||
const match = R.either(isBooleanLiteral, onlyReturnsBoolean); | ||
|
||
if (match(node.body)) { | ||
const instead = getRawReturn(node.body); | ||
context.report({ | ||
node, | ||
message: report(instead, getAlternative(instead)) | ||
}); | ||
} | ||
}, | ||
|
||
FunctionExpression(node) { | ||
if (onlyReturnsBoolean(node.body)) { | ||
const instead = node.body.body[0].argument.value; | ||
context.report({ | ||
node, | ||
message: report(instead, getAlternative(instead)) | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
description: 'Suggests using Ramda T and F functions instead of explicit versions', | ||
recommended: 'error' | ||
} | ||
} | ||
}; |
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,35 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/always-simplification'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
}, | ||
parserOptions: { | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
const error = (from, to) => ({ | ||
ruleId: 'always-simplification', | ||
message: `\`always(${from})\` should be simplified to \`${to}\`` | ||
}); | ||
|
||
ruleTester.run('always-simplification', rule, { | ||
valid: [ | ||
'always', | ||
'always(1)', | ||
'always(always)' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'always(true)', | ||
errors: [error('true', 'T')] | ||
}, | ||
{ | ||
code: 'always(false)', | ||
errors: [error('false', 'F')] | ||
} | ||
] | ||
}); |
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,37 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/compose-simplification'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
}, | ||
parserOptions: { | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
const error = { | ||
chain: { | ||
ruleId: 'compose-simplification', | ||
message: '`compose(flatten, map)` should be simplified to `chain`' | ||
} | ||
}; | ||
|
||
ruleTester.run('compose-simplification', rule, { | ||
valid: [ | ||
'compose(map, flatten)', | ||
'compose()', | ||
'compose(left, right)' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'compose(flatten, map)', | ||
errors: [error.chain] | ||
}, | ||
{ | ||
code: 'R[\'compose\'](R.flatten, map)', | ||
errors: [error.chain] | ||
} | ||
] | ||
}); |
Oops, something went wrong.