diff --git a/lib/rules/no-extra-boolean-cast.js b/lib/rules/no-extra-boolean-cast.js index 9ae9b5be61f..9bbd5546edd 100644 --- a/lib/rules/no-extra-boolean-cast.js +++ b/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)); } }); } diff --git a/tests/lib/rules/no-extra-boolean-cast.js b/tests/lib/rules/no-extra-boolean-cast.js index 129329ff0a3..1de914ca585 100644 --- a/tests/lib/rules/no-extra-boolean-cast.js +++ b/tests/lib/rules/no-extra-boolean-cast.js @@ -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",