Skip to content

Commit

Permalink
Fix: no-extra-boolean-cast incorrect Boolean() autofixing (fixes #7977)…
Browse files Browse the repository at this point in the history
… (#8037)
  • Loading branch information
jwilsson authored and not-an-aardvark committed Feb 9, 2017
1 parent 834f45d commit 16248e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rules/no-extra-boolean-cast.js
Expand Up @@ -98,6 +98,14 @@ module.exports = {
node,
message: "Redundant Boolean call.",
fix: fixer => {
if (!node.arguments.length) {
return fixer.replaceText(parent, "true");
}

if (node.arguments.length > 1 || node.arguments[0].type === "SpreadElement") {
return null;
}

const argument = node.arguments[0];

if (astUtils.getPrecedence(argument) < astUtils.getPrecedence(node.parent)) {
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/no-extra-boolean-cast.js
Expand Up @@ -186,6 +186,47 @@ ruleTester.run("no-extra-boolean-cast", rule, {
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "!Boolean(...foo);",
output: "!Boolean(...foo);",
parserOptions: { ecmaVersion: 2015 },
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "!Boolean(foo, bar());",
output: "!Boolean(foo, bar());",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "!Boolean((foo, bar()));",
output: "!(foo, bar());",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "!Boolean();",
output: "true;",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
},
{
code: "!(Boolean());",
output: "true;",
errors: [{
message: "Redundant Boolean call.",
type: "CallExpression"
}]
}
]
});

0 comments on commit 16248e2

Please sign in to comment.