use-isnan.js 943 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * @fileoverview Rule to flag comparisons to the value NaN
  3. * @author James Allardice
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "require calls to `isNaN()` when checking for `NaN`",
  13. category: "Possible Errors",
  14. recommended: true,
  15. url: "https://eslint.org/docs/rules/use-isnan"
  16. },
  17. schema: []
  18. },
  19. create(context) {
  20. return {
  21. BinaryExpression(node) {
  22. if (/^(?:[<>]|[!=]=)=?$/.test(node.operator) && (node.left.name === "NaN" || node.right.name === "NaN")) {
  23. context.report({ node, message: "Use the isNaN function to compare with NaN." });
  24. }
  25. }
  26. };
  27. }
  28. };