项目原始demo,不改动
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
Este repositorio está archivado. Puede ver los archivos y clonarlo, pero no puede subir cambios o reportar incidencias ni pedir Pull Requests.
 
 
 
 

82 líneas
2.4 KiB

  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. };