Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-compare-neg-zero.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @fileoverview The rule should warn against code that tries to compare against -0.
  3. * @author Aladdin-ADD <hh_2013@foxmail.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "problem",
  12. docs: {
  13. description: "disallow comparing against -0",
  14. category: "Possible Errors",
  15. recommended: true,
  16. url: "https://eslint.org/docs/rules/no-compare-neg-zero"
  17. },
  18. fixable: null,
  19. schema: [],
  20. messages: {
  21. unexpected: "Do not use the '{{operator}}' operator to compare against -0."
  22. }
  23. },
  24. create(context) {
  25. //--------------------------------------------------------------------------
  26. // Helpers
  27. //--------------------------------------------------------------------------
  28. /**
  29. * Checks a given node is -0
  30. *
  31. * @param {ASTNode} node - A node to check.
  32. * @returns {boolean} `true` if the node is -0.
  33. */
  34. function isNegZero(node) {
  35. return node.type === "UnaryExpression" && node.operator === "-" && node.argument.type === "Literal" && node.argument.value === 0;
  36. }
  37. const OPERATORS_TO_CHECK = new Set([">", ">=", "<", "<=", "==", "===", "!=", "!=="]);
  38. return {
  39. BinaryExpression(node) {
  40. if (OPERATORS_TO_CHECK.has(node.operator)) {
  41. if (isNegZero(node.left) || isNegZero(node.right)) {
  42. context.report({
  43. node,
  44. messageId: "unexpected",
  45. data: { operator: node.operator }
  46. });
  47. }
  48. }
  49. }
  50. };
  51. }
  52. };