Skip to content
Permalink
Browse files

Fix: no-extra-boolean-cast invalid autofix with yield before negation (

  • Loading branch information
mdjermanovic authored and btmills committed Aug 30, 2019
1 parent 4c0b70b commit febb6605d350c936d64cb73e694482cfbb20b29c
Showing with 73 additions and 1 deletion.
  1. +11 −1 lib/rules/no-extra-boolean-cast.js
  2. +62 −0 tests/lib/rules/no-extra-boolean-cast.js
@@ -102,7 +102,17 @@ module.exports = {
if (hasCommentsInside(parent)) {
return null;
}
return fixer.replaceText(parent, sourceCode.getText(node.argument));

let prefix = "";
const tokenBefore = sourceCode.getTokenBefore(parent);
const firstReplacementToken = sourceCode.getFirstToken(node.argument);

if (tokenBefore && tokenBefore.range[1] === parent.range[0] &&
!astUtils.canTokensBeAdjacent(tokenBefore, firstReplacementToken)) {
prefix = " ";
}

return fixer.replaceText(parent, prefix + sourceCode.getText(node.argument));
}
});
}
@@ -270,6 +270,68 @@ ruleTester.run("no-extra-boolean-cast", rule, {
},

// Adjacent tokens tests
{
code: "function *foo() { yield!!a ? b : c }",
output: "function *foo() { yield a ? b : c }",
parserOptions: { ecmaVersion: 2015 },
errors: [{
messageId: "unexpectedNegation",
type: "UnaryExpression"
}]
},
{
code: "function *foo() { yield!! a ? b : c }",
output: "function *foo() { yield a ? b : c }",
parserOptions: { ecmaVersion: 2015 },
errors: [{
messageId: "unexpectedNegation",
type: "UnaryExpression"
}]
},
{
code: "function *foo() { yield! !a ? b : c }",
output: "function *foo() { yield a ? b : c }",
parserOptions: { ecmaVersion: 2015 },
errors: [{
messageId: "unexpectedNegation",
type: "UnaryExpression"
}]
},
{
code: "function *foo() { yield !!a ? b : c }",
output: "function *foo() { yield a ? b : c }",
parserOptions: { ecmaVersion: 2015 },
errors: [{
messageId: "unexpectedNegation",
type: "UnaryExpression"
}]
},
{
code: "function *foo() { yield(!!a) ? b : c }",
output: "function *foo() { yield(a) ? b : c }",
parserOptions: { ecmaVersion: 2015 },
errors: [{
messageId: "unexpectedNegation",
type: "UnaryExpression"
}]
},
{
code: "function *foo() { yield/**/!!a ? b : c }",
output: "function *foo() { yield/**/a ? b : c }",
parserOptions: { ecmaVersion: 2015 },
errors: [{
messageId: "unexpectedNegation",
type: "UnaryExpression"
}]
},
{
code: "x=!!a ? b : c ",
output: "x=a ? b : c ",
errors: [{
messageId: "unexpectedNegation",
type: "UnaryExpression"
}]
},
{
code: "void!Boolean()",
output: "void true",

0 comments on commit febb660

Please sign in to comment.
You can’t perform that action at this time.