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-unsafe-negation.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. },
  46. create(context) {
  47. const sourceCode = context.getSourceCode();
  48. return {
  49. BinaryExpression(node) {
  50. if (isRelationalOperator(node.operator) &&
  51. isNegation(node.left) &&
  52. !astUtils.isParenthesised(sourceCode, node.left)
  53. ) {
  54. context.report({
  55. node,
  56. loc: node.left.loc,
  57. message: "Unexpected negating the left operand of '{{operator}}' operator.",
  58. data: node,
  59. fix(fixer) {
  60. const negationToken = sourceCode.getFirstToken(node.left);
  61. const fixRange = [negationToken.range[1], node.range[1]];
  62. const text = sourceCode.text.slice(fixRange[0], fixRange[1]);
  63. return fixer.replaceTextRange(fixRange, `(${text})`);
  64. }
  65. });
  66. }
  67. }
  68. };
  69. }
  70. };