项目原始demo,不改动
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

no-confusing-arrow.js 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @fileoverview A rule to warn against using arrow functions when they could be
  3. * confused with comparisions
  4. * @author Jxck <https://github.com/Jxck>
  5. */
  6. "use strict";
  7. const astUtils = require("../ast-utils.js");
  8. //------------------------------------------------------------------------------
  9. // Helpers
  10. //------------------------------------------------------------------------------
  11. /**
  12. * Checks whether or not a node is a conditional expression.
  13. * @param {ASTNode} node - node to test
  14. * @returns {boolean} `true` if the node is a conditional expression.
  15. */
  16. function isConditional(node) {
  17. return node && node.type === "ConditionalExpression";
  18. }
  19. //------------------------------------------------------------------------------
  20. // Rule Definition
  21. //------------------------------------------------------------------------------
  22. module.exports = {
  23. meta: {
  24. docs: {
  25. description: "disallow arrow functions where they could be confused with comparisons",
  26. category: "ECMAScript 6",
  27. recommended: false,
  28. url: "https://eslint.org/docs/rules/no-confusing-arrow"
  29. },
  30. fixable: "code",
  31. schema: [{
  32. type: "object",
  33. properties: {
  34. allowParens: { type: "boolean" }
  35. },
  36. additionalProperties: false
  37. }],
  38. messages: {
  39. confusing: "Arrow function used ambiguously with a conditional expression."
  40. }
  41. },
  42. create(context) {
  43. const config = context.options[0] || {};
  44. const sourceCode = context.getSourceCode();
  45. /**
  46. * Reports if an arrow function contains an ambiguous conditional.
  47. * @param {ASTNode} node - A node to check and report.
  48. * @returns {void}
  49. */
  50. function checkArrowFunc(node) {
  51. const body = node.body;
  52. if (isConditional(body) && !(config.allowParens && astUtils.isParenthesised(sourceCode, body))) {
  53. context.report({
  54. node,
  55. messageId: "confusing",
  56. fix(fixer) {
  57. // if `allowParens` is not set to true dont bother wrapping in parens
  58. return config.allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`);
  59. }
  60. });
  61. }
  62. }
  63. return {
  64. ArrowFunctionExpression: checkArrowFunc
  65. };
  66. }
  67. };