Navigation Menu

Skip to content

Commit

Permalink
Fix: no-extra-boolean-cast invalid autofix with yield before negation (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and btmills committed Aug 30, 2019
1 parent 4c0b70b commit febb660
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/rules/no-extra-boolean-cast.js
Expand Up @@ -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));
}
});
}
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/rules/no-extra-boolean-cast.js
Expand Up @@ -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",
Expand Down

0 comments on commit febb660

Please sign in to comment.