Skip to content

Commit

Permalink
Merge pull request #96 from ShridharGoel/boolean-update
Browse files Browse the repository at this point in the history
Add custom config to use double negation instead of Boolean
  • Loading branch information
aldo-expensify authored May 29, 2024
2 parents bc908fd + 30a3dbf commit 89be844
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions eslint-plugin-expensify/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
MUST_USE_VARIABLE_FOR_ASSIGNMENT: '{{key}} must be assigned as a variable instead of direct assignment.',
NO_DEFAULT_PROPS: 'defaultProps should not be used in function components. Use default Arguments instead.',
USE_PERIODS_ERROR_MESSAGES: 'Use periods at the end of error messages.',
USE_DOUBLE_NEGATION_INSTEAD_OF_BOOLEAN: 'Use !! instead of Boolean().',
NO_ACC_SPREAD_IN_REDUCE: 'Avoid a use of spread (`...`) operator on accumulators in reduce callback. Mutate them directly instead.',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../use-double-negation-instead-of-boolean');
const message = require('../CONST').MESSAGE.USE_DOUBLE_NEGATION_INSTEAD_OF_BOOLEAN;

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});

ruleTester.run('use-double-negation-instead-of-Boolean()', rule, {
valid: [
{
code: '!!test',
},
{
code: '!!(test1 || test2)',
},
{
code: '!!(test1 && test2)',
},
{
code: '!!(test1 && (test2 || test3))',
},
{
code: '!!(test1 || test2 && test3)',
},
{
code: '!!test ? "" : "example"',
},
],
invalid: [
{
code: 'Boolean(test)',
output: '!!test',
errors: [{
message,
}],
},
{
code: 'Boolean(test1 || test2)',
output: '!!(test1 || test2)',
errors: [{
message,
}],
},
{
code: 'Boolean(test1 && test2)',
output: '!!(test1 && test2)',
errors: [{
message,
}],
},
{
code: 'Boolean(test1 && (test2 || test3))',
output: '!!(test1 && (test2 || test3))',
errors: [{
message,
}],
},
{
code: 'Boolean(test1 || test2 && test3)',
output: '!!(test1 || test2 && test3)',
errors: [{
message,
}],
},
{
code: 'Boolean(test) ? "" : "example"',
output: '!!test ? "" : "example"',
errors: [{
message,
}],
},
],
});
31 changes: 31 additions & 0 deletions eslint-plugin-expensify/use-double-negation-instead-of-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const message = require('./CONST').MESSAGE.USE_DOUBLE_NEGATION_INSTEAD_OF_BOOLEAN;

module.exports = {
meta: {
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
if (node.callee.type === 'Identifier' && node.callee.name === 'Boolean' && node.arguments.length === 1) {
const argument = node.arguments[0];
const sourceCode = context.getSourceCode();
const argumentText = sourceCode.getText(argument);
let fixedText = `!!${argumentText}`;

if (argument.type === 'LogicalExpression' || argument.type === 'BinaryExpression') {
fixedText = `!!(${argumentText})`;
}

context.report({
node,
message,
fix(fixer) {
return fixer.replaceText(node, fixedText);
},
});
}
},
};
},
};
1 change: 1 addition & 0 deletions rules/expensify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'rulesdir/no-call-actions-from-actions': 'error',
'rulesdir/no-api-side-effects-method': 'error',
'rulesdir/prefer-localization': 'error',
'rulesdir/use-double-negation-instead-of-boolean': 'error',
'rulesdir/no-acc-spread-in-reduce': 'error',
'no-restricted-imports': ['error', {
paths: [{
Expand Down

0 comments on commit 89be844

Please sign in to comment.