Skip to content

Commit

Permalink
add two more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Dec 9, 2023
1 parent 3a22236 commit 0541585
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rules/no-implicit-coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ module.exports = {

report(node, recommendation, true);
}

// -(-foo)
if (node.operator === "-" && node.argument.type === "UnaryExpression" && node.argument.operator === "-") {
const recommendation = `Number(${sourceCode.getText(node.argument.argument)})`;

report(node, recommendation, true);
}
},

// Use `:exit` to prevent double reporting
Expand All @@ -317,6 +324,13 @@ module.exports = {
report(node, recommendation, true);
}

// foo - 1
if (node.operator === "-" && isNumeric(node.right) && node.right.value === 0) {
const recommendation = `Number(${sourceCode.getText(node.left)})`;

report(node, recommendation, true);
}

// "" + foo
operatorAllowed = options.allow.includes("+");
if (!operatorAllowed && options.string && isConcatWithEmptyString(node)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/no-implicit-coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ ruleTester.run("no-implicit-coercion", rule, {
type: "UnaryExpression"
}]
},
{
code: "-(-foo)",
output: "Number(foo)",
errors: [{
messageId: "useRecommendation",
data: { recommendation: "Number(foo)" },
type: "UnaryExpression"
}]
},
{
code: "+foo.bar",
output: "Number(foo.bar)",
Expand Down Expand Up @@ -193,6 +202,15 @@ ruleTester.run("no-implicit-coercion", rule, {
type: "BinaryExpression"
}]
},
{
code: "foo.bar-0",
output: "Number(foo.bar)",
errors: [{
messageId: "useRecommendation",
data: { recommendation: "Number(foo.bar)" },
type: "BinaryExpression"
}]
},
{
code: "\"\"+foo",
output: "String(foo)",
Expand Down

0 comments on commit 0541585

Please sign in to comment.