no-unsafe-negation.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Checks whether the given operator is a relational operator or not.
  15. *
  16. * @param {string} op - The operator type to check.
  17. * @returns {boolean} `true` if the operator is a relational operator.
  18. */
  19. function isRelationalOperator(op) {
  20. return op === "in" || op === "instanceof";
  21. }
  22. /**
  23. * Checks whether the given node is a logical negation expression or not.
  24. *
  25. * @param {ASTNode} node - The node to check.
  26. * @returns {boolean} `true` if the node is a logical negation expression.
  27. */
  28. function isNegation(node) {
  29. return node.type === "UnaryExpression" && node.operator === "!";
  30. }
  31. //------------------------------------------------------------------------------
  32. // Rule Definition
  33. //------------------------------------------------------------------------------
  34. module.exports = {
  35. meta: {
  36. type: "problem",
  37. docs: {
  38. description: "disallow negating the left operand of relational operators",
  39. category: "Possible Errors",
  40. recommended: true,
  41. url: "https://eslint.org/docs/rules/no-unsafe-negation"
  42. },
  43. schema: [],
  44. fixable: "code",
  45. messages: {
  46. unexpected: "Unexpected negating the left operand of '{{operator}}' operator."
  47. }
  48. },
  49. create(context) {
  50. const sourceCode = context.getSourceCode();
  51. return {
  52. BinaryExpression(node) {
  53. if (isRelationalOperator(node.operator) &&
  54. isNegation(node.left) &&
  55. !astUtils.isParenthesised(sourceCode, node.left)
  56. ) {
  57. context.report({
  58. node,
  59. loc: node.left.loc,
  60. messageId: "unexpected",
  61. data: { operator: node.operator },
  62. fix(fixer) {
  63. const negationToken = sourceCode.getFirstToken(node.left);
  64. const fixRange = [negationToken.range[1], node.range[1]];
  65. const text = sourceCode.text.slice(fixRange[0], fixRange[1]);
  66. return fixer.replaceTextRange(fixRange, `(${text})`);
  67. }
  68. });
  69. }
  70. }
  71. };
  72. }
  73. };