Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-implicit-coercion false positive with String() (fixes #14623) #14641

Merged
merged 1 commit into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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