no-unsafe-negation.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @fileoverview Rule to disallow negating the left operand of relational operators
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Checks whether the given operator is `in` or `instanceof`
  15. * @param {string} op The operator type to check.
  16. * @returns {boolean} `true` if the operator is `in` or `instanceof`
  17. */
  18. function isInOrInstanceOfOperator(op) {
  19. return op === "in" || op === "instanceof";
  20. }
  21. /**
  22. * Checks whether the given operator is an ordering relational operator or not.
  23. * @param {string} op The operator type to check.
  24. * @returns {boolean} `true` if the operator is an ordering relational operator.
  25. */
  26. function isOrderingRelationalOperator(op) {
  27. return op === "<" || op === ">" || op === ">=" || op === "<=";
  28. }
  29. /**
  30. * Checks whether the given node is a logical negation expression or not.
  31. * @param {ASTNode} node The node to check.
  32. * @returns {boolean} `true` if the node is a logical negation expression.
  33. */
  34. function isNegation(node) {
  35. return node.type === "UnaryExpression" && node.operator === "!";
  36. }
  37. //------------------------------------------------------------------------------
  38. // Rule Definition
  39. //------------------------------------------------------------------------------
  40. /** @type {import('../shared/types').Rule} */
  41. module.exports = {
  42. meta: {
  43. type: "problem",
  44. defaultOptions: [{
  45. enforceForOrderingRelations: false
  46. }],
  47. docs: {
  48. description: "Disallow negating the left operand of relational operators",
  49. recommended: true,
  50. url: "https://eslint.org/docs/latest/rules/no-unsafe-negation"
  51. },
  52. hasSuggestions: true,
  53. schema: [
  54. {
  55. type: "object",
  56. properties: {
  57. enforceForOrderingRelations: {
  58. type: "boolean"
  59. }
  60. },
  61. additionalProperties: false
  62. }
  63. ],
  64. fixable: null,
  65. messages: {
  66. unexpected: "Unexpected negating the left operand of '{{operator}}' operator.",
  67. suggestNegatedExpression: "Negate '{{operator}}' expression instead of its left operand. This changes the current behavior.",
  68. suggestParenthesisedNegation: "Wrap negation in '()' to make the intention explicit. This preserves the current behavior."
  69. }
  70. },
  71. create(context) {
  72. const sourceCode = context.sourceCode;
  73. const [{ enforceForOrderingRelations }] = context.options;
  74. return {
  75. BinaryExpression(node) {
  76. const operator = node.operator;
  77. const orderingRelationRuleApplies = enforceForOrderingRelations && isOrderingRelationalOperator(operator);
  78. if (
  79. (isInOrInstanceOfOperator(operator) || orderingRelationRuleApplies) &&
  80. isNegation(node.left) &&
  81. !astUtils.isParenthesised(sourceCode, node.left)
  82. ) {
  83. context.report({
  84. node,
  85. loc: node.left.loc,
  86. messageId: "unexpected",
  87. data: { operator },
  88. suggest: [
  89. {
  90. messageId: "suggestNegatedExpression",
  91. data: { operator },
  92. fix(fixer) {
  93. const negationToken = sourceCode.getFirstToken(node.left);
  94. const fixRange = [negationToken.range[1], node.range[1]];
  95. const text = sourceCode.text.slice(fixRange[0], fixRange[1]);
  96. return fixer.replaceTextRange(fixRange, `(${text})`);
  97. }
  98. },
  99. {
  100. messageId: "suggestParenthesisedNegation",
  101. fix(fixer) {
  102. return fixer.replaceText(node.left, `(${sourceCode.getText(node.left)})`);
  103. }
  104. }
  105. ]
  106. });
  107. }
  108. }
  109. };
  110. }
  111. };