-
Notifications
You must be signed in to change notification settings - Fork 27
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 #96 from ShridharGoel/boolean-update
Add custom config to use double negation instead of Boolean
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 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
77 changes: 77 additions & 0 deletions
77
eslint-plugin-expensify/tests/use-double-negation-instead-of-boolean.test.js
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,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
31
eslint-plugin-expensify/use-double-negation-instead-of-boolean.js
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,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); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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