Skip to content

Commit

Permalink
Update: add fixer for no-extra-boolean-cast (#7387)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Nov 1, 2016
1 parent 183def6 commit 36338f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/rules/no-extra-boolean-cast.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# disallow unnecessary boolean casts (no-extra-boolean-cast)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

In contexts such as an `if` statement's test where the result of the expression will already be coerced to a Boolean, casting to a Boolean via double negation (`!!`) or a `Boolean` call is unnecessary. For example, these `if` statements are equivalent:

```js
Expand Down
17 changes: 14 additions & 3 deletions lib/rules/no-extra-boolean-cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ module.exports = {
recommended: true
},

schema: []
schema: [],

fixable: "code"
},

create(context) {
const sourceCode = context.getSourceCode();

// Node types which have a test which will coerce values to booleans.
const BOOLEAN_NODE_TYPES = [
Expand Down Expand Up @@ -70,7 +73,11 @@ module.exports = {
grandparent.callee.type === "Identifier" &&
grandparent.callee.name === "Boolean")
) {
context.report(node, "Redundant double negation.");
context.report({
node,
message: "Redundant double negation.",
fix: fixer => fixer.replaceText(parent, sourceCode.getText(node.argument))
});
}
},
CallExpression(node) {
Expand All @@ -81,7 +88,11 @@ module.exports = {
}

if (isInBooleanContext(node, parent)) {
context.report(node, "Redundant Boolean call.");
context.report({
node,
message: "Redundant Boolean call.",
fix: fixer => fixer.replaceText(node, sourceCode.getText(node.arguments[0]))
});
}
}
};
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-extra-boolean-cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,97 +37,111 @@ ruleTester.run("no-extra-boolean-cast", rule, {
invalid: [
{
code: "if (!!foo) {}",
output: "if (foo) {}",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "do {} while (!!foo)",
output: "do {} while (foo)",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "while (!!foo) {}",
output: "while (foo) {}",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "!!foo ? bar : baz",
output: "foo ? bar : baz",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "for (; !!foo;) {}",
output: "for (; foo;) {}",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "!!!foo",
output: "!foo",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "Boolean(!!foo)",
output: "Boolean(foo)",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "new Boolean(!!foo)",
output: "new Boolean(foo)",
errors: [{
message: "Redundant double negation.",
type: "UnaryExpression"
}]
},
{
code: "if (Boolean(foo)) {}",
output: "if (foo) {}",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "do {} while (Boolean(foo))",
output: "do {} while (foo)",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "while (Boolean(foo)) {}",
output: "while (foo) {}",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "Boolean(foo) ? bar : baz",
output: "foo ? bar : baz",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "for (; Boolean(foo);) {}",
output: "for (; foo;) {}",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "!Boolean(foo)",
output: "!foo",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
Expand Down

0 comments on commit 36338f0

Please sign in to comment.