项目原始demo,不改动
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.
 
 
 
 

129 rindas
4.3 KiB

  1. /**
  2. * @fileoverview Rule to flag unnecessary double negation in Boolean contexts
  3. * @author Brandon Mills
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "disallow unnecessary boolean casts",
  17. category: "Possible Errors",
  18. recommended: true,
  19. url: "https://eslint.org/docs/rules/no-extra-boolean-cast"
  20. },
  21. schema: [],
  22. fixable: "code",
  23. messages: {
  24. unexpectedCall: "Redundant Boolean call.",
  25. unexpectedNegation: "Redundant double negation."
  26. }
  27. },
  28. create(context) {
  29. const sourceCode = context.getSourceCode();
  30. // Node types which have a test which will coerce values to booleans.
  31. const BOOLEAN_NODE_TYPES = [
  32. "IfStatement",
  33. "DoWhileStatement",
  34. "WhileStatement",
  35. "ConditionalExpression",
  36. "ForStatement"
  37. ];
  38. /**
  39. * Check if a node is in a context where its value would be coerced to a boolean at runtime.
  40. *
  41. * @param {Object} node The node
  42. * @param {Object} parent Its parent
  43. * @returns {boolean} If it is in a boolean context
  44. */
  45. function isInBooleanContext(node, parent) {
  46. return (
  47. (BOOLEAN_NODE_TYPES.indexOf(parent.type) !== -1 &&
  48. node === parent.test) ||
  49. // !<bool>
  50. (parent.type === "UnaryExpression" &&
  51. parent.operator === "!")
  52. );
  53. }
  54. return {
  55. UnaryExpression(node) {
  56. const ancestors = context.getAncestors(),
  57. parent = ancestors.pop(),
  58. grandparent = ancestors.pop();
  59. // Exit early if it's guaranteed not to match
  60. if (node.operator !== "!" ||
  61. parent.type !== "UnaryExpression" ||
  62. parent.operator !== "!") {
  63. return;
  64. }
  65. if (isInBooleanContext(parent, grandparent) ||
  66. // Boolean(<bool>) and new Boolean(<bool>)
  67. ((grandparent.type === "CallExpression" || grandparent.type === "NewExpression") &&
  68. grandparent.callee.type === "Identifier" &&
  69. grandparent.callee.name === "Boolean")
  70. ) {
  71. context.report({
  72. node,
  73. messageId: "unexpectedNegation",
  74. fix: fixer => fixer.replaceText(parent, sourceCode.getText(node.argument))
  75. });
  76. }
  77. },
  78. CallExpression(node) {
  79. const parent = node.parent;
  80. if (node.callee.type !== "Identifier" || node.callee.name !== "Boolean") {
  81. return;
  82. }
  83. if (isInBooleanContext(node, parent)) {
  84. context.report({
  85. node,
  86. messageId: "unexpectedCall",
  87. fix: fixer => {
  88. if (!node.arguments.length) {
  89. return fixer.replaceText(parent, "true");
  90. }
  91. if (node.arguments.length > 1 || node.arguments[0].type === "SpreadElement") {
  92. return null;
  93. }
  94. const argument = node.arguments[0];
  95. if (astUtils.getPrecedence(argument) < astUtils.getPrecedence(node.parent)) {
  96. return fixer.replaceText(node, `(${sourceCode.getText(argument)})`);
  97. }
  98. return fixer.replaceText(node, sourceCode.getText(argument));
  99. }
  100. });
  101. }
  102. }
  103. };
  104. }
  105. };