Skip to content

Commit

Permalink
Fix: no-implicit-coercion false positive with String() (fixes #14623)…
Browse files Browse the repository at this point in the history
… (#14641)
  • Loading branch information
mdjermanovic committed Jun 4, 2021
1 parent d709abf commit 1f048cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
23 changes: 21 additions & 2 deletions lib/rules/no-implicit-coercion.js
Expand Up @@ -109,6 +109,20 @@ function getNonNumericOperand(node) {
return null;
}

/**
* Checks whether an expression evaluates to a string.
* @param {ASTNode} node node that represents the expression to check.
* @returns {boolean} Whether or not the expression evaluates to a string.
*/
function isStringType(node) {
return astUtils.isStringLiteral(node) ||
(
node.type === "CallExpression" &&
node.callee.type === "Identifier" &&
node.callee.name === "String"
);
}

/**
* Checks whether a node is an empty string literal or not.
* @param {ASTNode} node The node to check.
Expand All @@ -126,8 +140,8 @@ function isEmptyString(node) {
*/
function isConcatWithEmptyString(node) {
return node.operator === "+" && (
(isEmptyString(node.left) && !astUtils.isStringLiteral(node.right)) ||
(isEmptyString(node.right) && !astUtils.isStringLiteral(node.left))
(isEmptyString(node.left) && !isStringType(node.right)) ||
(isEmptyString(node.right) && !isStringType(node.left))
);
}

Expand Down Expand Up @@ -332,6 +346,11 @@ module.exports = {
return;
}

// if the expression is already a string, then this isn't a coercion
if (isStringType(node.expressions[0])) {
return;
}

const code = sourceCode.getText(node.expressions[0]);
const recommendation = `String(${code})`;

Expand Down
11 changes: 10 additions & 1 deletion tests/lib/rules/no-implicit-coercion.js
Expand Up @@ -95,7 +95,16 @@ ruleTester.run("no-implicit-coercion", rule, {
{ code: "`${foo}`", parserOptions: { ecmaVersion: 6 } },
{ code: "`${foo}`", options: [{ }], parserOptions: { ecmaVersion: 6 } },
{ code: "`${foo}`", options: [{ disallowTemplateShorthand: false }], parserOptions: { ecmaVersion: 6 } },
"+42"
"+42",

// https://github.com/eslint/eslint/issues/14623
"'' + String(foo)",
"String(foo) + ''",
{ code: "`` + String(foo)", parserOptions: { ecmaVersion: 6 } },
{ code: "String(foo) + ``", parserOptions: { ecmaVersion: 6 } },
{ code: "`${'foo'}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "`${`foo`}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "`${String(foo)}`", options: [{ disallowTemplateShorthand: true }], parserOptions: { ecmaVersion: 6 } }
],
invalid: [
{
Expand Down

0 comments on commit 1f048cb

Please sign in to comment.