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

Add custom config to use double negation instead of Boolean #96

Merged
merged 3 commits into from
May 29, 2024
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
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