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

feat: add suggestions to use-isnan in binary expressions #17996

Merged
merged 13 commits into from
Jan 29, 2024
45 changes: 23 additions & 22 deletions lib/rules/use-isnan.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,31 @@ module.exports = {
/^(?:[<>]|[!=]=)=?$/u.test(node.operator) &&
(isNaNIdentifier(node.left) || isNaNIdentifier(node.right))
) {
context.report({
node,
messageId: "comparisonWithNaN",
suggest: [
{
messageId: "replaceWithIsNaN",
fix(fixer) {
const comparedValue = isNaNIdentifier(node.left) ? node.right : node.left;
let shouldNegate = ["!=", "!=="].includes(node.operator);

if (comparedValue === node.right) {
shouldNegate ||= ["<", "<="].includes(node.operator);
} else {
shouldNegate ||= [">", ">="].includes(node.operator);
const fixableOperators = ["==", "===", "!=", "!=="];
const isFixable = fixableOperators.includes(node.operator);
StyleShit marked this conversation as resolved.
Show resolved Hide resolved

if (isFixable) {
context.report({
node,
messageId: "comparisonWithNaN",
suggest: [
{
messageId: "replaceWithIsNaN",
fix(fixer) {
const comparedValue = isNaNIdentifier(node.left) ? node.right : node.left;
const shouldNegate = node.operator[0] === "!";

const negation = shouldNegate ? "!" : "";
const comparedValueText = sourceCode.getText(comparedValue);

return fixer.replaceText(node, `${negation}Number.isNaN(${comparedValueText})`);
StyleShit marked this conversation as resolved.
Show resolved Hide resolved
}

const negation = shouldNegate ? "!" : "";
const comparedValueText = sourceCode.getText(comparedValue);

return fixer.replaceText(node, `${negation}Number.isNaN(${comparedValueText})`);
}
}
]
});
]
});
} else {
context.report({ node, messageId: "comparisonWithNaN" });
}
}
}

Expand Down
80 changes: 16 additions & 64 deletions tests/lib/rules/use-isnan.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,80 +431,56 @@ ruleTester.run("use-isnan", rule, {
code: "NaN < \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" < NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "NaN > \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" > NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "NaN <= \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" <= NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "NaN >= \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" >= NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
Expand Down Expand Up @@ -591,80 +567,56 @@ ruleTester.run("use-isnan", rule, {
code: "Number.NaN < \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" < Number.NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "Number.NaN > \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" > Number.NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "Number.NaN <= \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" <= Number.NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "Number.NaN >= \"abc\";",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: 'Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
code: "\"abc\" >= Number.NaN;",
errors: [{
...comparisonError,
suggestions: [{
messageId: "replaceWithIsNaN",
output: '!Number.isNaN("abc");'
}]
suggestions: []
}]
},
{
Expand Down