Skip to content

Commit

Permalink
don't fix numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Dec 16, 2023
1 parent 6814836 commit 9c74329
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/no-implicit-coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ module.exports = {

// -(-foo)
operatorAllowed = options.allow.includes("- -");
if (!operatorAllowed && options.number && node.operator === "-" && node.argument.type === "UnaryExpression" && node.argument.operator === "-") {
if (!operatorAllowed && options.number && node.operator === "-" && node.argument.type === "UnaryExpression" && node.argument.operator === "-" && !isNumeric(node.argument.argument)) {
const recommendation = `Number(${sourceCode.getText(node.argument.argument)})`;

report(node, recommendation, true);
Expand All @@ -327,7 +327,7 @@ module.exports = {

// foo - 0
operatorAllowed = options.allow.includes("-");
if (!operatorAllowed && options.number && node.operator === "-" && isNumeric(node.right) && node.right.value === 0) {
if (!operatorAllowed && options.number && node.operator === "-" && isNumeric(node.right) && node.right.value === 0 && !isNumeric(node.left)) {
const recommendation = `Number(${sourceCode.getText(node.left)})`;

report(node, recommendation, true);
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-implicit-coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ ruleTester.run("no-implicit-coercion", rule, {
"-foo",
"+1234",
"-1234",
"- -1234",
"+Number(lol)",
"-parseFloat(lol)",
"2 * foo",
"1 * 1234",
"123 - 0",
"1 * Number(foo)",
"1 * parseInt(foo)",
"1 * parseFloat(foo)",
"Number(foo) * 1",
"Number(foo) - 0",
"parseInt(foo) * 1",
"parseFloat(foo) * 1",
"- -Number(foo)",
"1 * 1234 * 678 * Number(foo)",
"1 * 1234 * 678 * parseInt(foo)",
"(1 - 0) * parseInt(foo)",
"1234 * 1 * 678 * Number(foo)",
"1234 * 1 * Number(foo) * Number(bar)",
"1234 * 1 * Number(foo) * parseInt(bar)",
Expand All @@ -54,6 +59,7 @@ ruleTester.run("no-implicit-coercion", rule, {
"1234 * parseInt(foo) * 1 * Number(bar)",
"1234 * parseFloat(foo) * 1 * parseInt(bar)",
"1234 * parseFloat(foo) * 1 * Number(bar)",
"(- -1234) * (parseFloat(foo) - 0) * (Number(bar) - 0)",
"1234*foo*1",
"1234*1*foo",
"1234*bar*1*foo",
Expand Down

0 comments on commit 9c74329

Please sign in to comment.