no-unsafe-negation.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. module.exports = {
  41. meta: {
  42. type: "problem",
  43. docs: {
  44. description: "disallow negating the left operand of relational operators",
  45. category: "Possible Errors",
  46. recommended: true,
  47. url: "https://eslint.org/docs/rules/no-unsafe-negation",
  48. suggestion: true
  49. },
  50. schema: [
  51. {
  52. type: "object",
  53. properties: {
  54. enforceForOrderingRelations: {
  55. type: "boolean",
  56. default: false
  57. }
  58. },
  59. additionalProperties: false
  60. }
  61. ],
  62. fixable: null,
  63. messages: {
  64. unexpected: "Unexpected negating the left operand of '{{operator}}' operator.",
  65. suggestNegatedExpression: "Negate '{{operator}}' expression instead of its left operand. This changes the current behavior.",
  66. suggestParenthesisedNegation: "Wrap negation in '()' to make the intention explicit. This preserves the current behavior."
  67. }
  68. },
  69. create(context) {
  70. const sourceCode = context.getSourceCode();
  71. const options = context.options[0] || {};
  72. const enforceForOrderingRelations = options.enforceForOrderingRelations === true;
  73. return {
  74. BinaryExpression(node) {
  75. const operator = node.operator;
  76. const orderingRelationRuleApplies = enforceForOrderingRelations && isOrderingRelationalOperator(operator);
  77. if (
  78. (isInOrInstanceOfOperator(operator) || orderingRelationRuleApplies) &&
  79. isNegation(node.left) &&
  80. !astUtils.isParenthesised(sourceCode, node.left)
  81. ) {
  82. context.report({
  83. node,
  84. loc: node.left.loc,
  85. messageId: "unexpected",
  86. data: { operator },
  87. suggest: [
  88. {
  89. messageId: "suggestNegatedExpression",
  90. data: { operator },
  91. fix(fixer) {
  92. const negationToken = sourceCode.getFirstToken(node.left);
  93. const fixRange = [negationToken.range[1], node.range[1]];
  94. const text = sourceCode.text.slice(fixRange[0], fixRange[1]);
  95. return fixer.replaceTextRange(fixRange, `(${text})`);
  96. }
  97. },
  98. {
  99. messageId: "suggestParenthesisedNegation",
  100. fix(fixer) {
  101. return fixer.replaceText(node.left, `(${sourceCode.getText(node.left)})`);
  102. }
  103. }
  104. ]
  105. });
  106. }
  107. }
  108. };
  109. }
  110. };