Skip to content

Commit 89be844

Browse files
Merge pull request #96 from ShridharGoel/boolean-update
Add custom config to use double negation instead of Boolean
2 parents bc908fd + 30a3dbf commit 89be844

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

eslint-plugin-expensify/CONST.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
MUST_USE_VARIABLE_FOR_ASSIGNMENT: '{{key}} must be assigned as a variable instead of direct assignment.',
2929
NO_DEFAULT_PROPS: 'defaultProps should not be used in function components. Use default Arguments instead.',
3030
USE_PERIODS_ERROR_MESSAGES: 'Use periods at the end of error messages.',
31+
USE_DOUBLE_NEGATION_INSTEAD_OF_BOOLEAN: 'Use !! instead of Boolean().',
3132
NO_ACC_SPREAD_IN_REDUCE: 'Avoid a use of spread (`...`) operator on accumulators in reduce callback. Mutate them directly instead.',
3233
},
3334
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const RuleTester = require('eslint').RuleTester;
2+
const rule = require('../use-double-negation-instead-of-boolean');
3+
const message = require('../CONST').MESSAGE.USE_DOUBLE_NEGATION_INSTEAD_OF_BOOLEAN;
4+
5+
const ruleTester = new RuleTester({
6+
parserOptions: {
7+
ecmaVersion: 6,
8+
sourceType: 'module',
9+
},
10+
});
11+
12+
ruleTester.run('use-double-negation-instead-of-Boolean()', rule, {
13+
valid: [
14+
{
15+
code: '!!test',
16+
},
17+
{
18+
code: '!!(test1 || test2)',
19+
},
20+
{
21+
code: '!!(test1 && test2)',
22+
},
23+
{
24+
code: '!!(test1 && (test2 || test3))',
25+
},
26+
{
27+
code: '!!(test1 || test2 && test3)',
28+
},
29+
{
30+
code: '!!test ? "" : "example"',
31+
},
32+
],
33+
invalid: [
34+
{
35+
code: 'Boolean(test)',
36+
output: '!!test',
37+
errors: [{
38+
message,
39+
}],
40+
},
41+
{
42+
code: 'Boolean(test1 || test2)',
43+
output: '!!(test1 || test2)',
44+
errors: [{
45+
message,
46+
}],
47+
},
48+
{
49+
code: 'Boolean(test1 && test2)',
50+
output: '!!(test1 && test2)',
51+
errors: [{
52+
message,
53+
}],
54+
},
55+
{
56+
code: 'Boolean(test1 && (test2 || test3))',
57+
output: '!!(test1 && (test2 || test3))',
58+
errors: [{
59+
message,
60+
}],
61+
},
62+
{
63+
code: 'Boolean(test1 || test2 && test3)',
64+
output: '!!(test1 || test2 && test3)',
65+
errors: [{
66+
message,
67+
}],
68+
},
69+
{
70+
code: 'Boolean(test) ? "" : "example"',
71+
output: '!!test ? "" : "example"',
72+
errors: [{
73+
message,
74+
}],
75+
},
76+
],
77+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const message = require('./CONST').MESSAGE.USE_DOUBLE_NEGATION_INSTEAD_OF_BOOLEAN;
2+
3+
module.exports = {
4+
meta: {
5+
fixable: 'code',
6+
},
7+
create(context) {
8+
return {
9+
CallExpression(node) {
10+
if (node.callee.type === 'Identifier' && node.callee.name === 'Boolean' && node.arguments.length === 1) {
11+
const argument = node.arguments[0];
12+
const sourceCode = context.getSourceCode();
13+
const argumentText = sourceCode.getText(argument);
14+
let fixedText = `!!${argumentText}`;
15+
16+
if (argument.type === 'LogicalExpression' || argument.type === 'BinaryExpression') {
17+
fixedText = `!!(${argumentText})`;
18+
}
19+
20+
context.report({
21+
node,
22+
message,
23+
fix(fixer) {
24+
return fixer.replaceText(node, fixedText);
25+
},
26+
});
27+
}
28+
},
29+
};
30+
},
31+
};

rules/expensify.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
'rulesdir/no-call-actions-from-actions': 'error',
1616
'rulesdir/no-api-side-effects-method': 'error',
1717
'rulesdir/prefer-localization': 'error',
18+
'rulesdir/use-double-negation-instead-of-boolean': 'error',
1819
'rulesdir/no-acc-spread-in-reduce': 'error',
1920
'no-restricted-imports': ['error', {
2021
paths: [{

0 commit comments

Comments
 (0)