项目原始demo,不改动
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.
 
 
 
 

82 řádky
2.6 KiB

  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("../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. docs: {
  37. description: "disallow negating the left operand of relational operators",
  38. category: "Possible Errors",
  39. recommended: true,
  40. url: "https://eslint.org/docs/rules/no-unsafe-negation"
  41. },
  42. schema: [],
  43. fixable: "code"
  44. },
  45. create(context) {
  46. const sourceCode = context.getSourceCode();
  47. return {
  48. BinaryExpression(node) {
  49. if (isRelationalOperator(node.operator) &&
  50. isNegation(node.left) &&
  51. !astUtils.isParenthesised(sourceCode, node.left)
  52. ) {
  53. context.report({
  54. node,
  55. loc: node.left.loc,
  56. message: "Unexpected negating the left operand of '{{operator}}' operator.",
  57. data: node,
  58. fix(fixer) {
  59. const negationToken = sourceCode.getFirstToken(node.left);
  60. const fixRange = [negationToken.range[1], node.range[1]];
  61. const text = sourceCode.text.slice(fixRange[0], fixRange[1]);
  62. return fixer.replaceTextRange(fixRange, `(${text})`);
  63. }
  64. });
  65. }
  66. }
  67. };
  68. }
  69. };