no-confusing-arrow.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @fileoverview A rule to warn against using arrow functions when they could be
  3. * confused with comparisons
  4. * @author Jxck <https://github.com/Jxck>
  5. */
  6. "use strict";
  7. const astUtils = require("./utils/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. /** @type {import('../shared/types').Rule} */
  23. module.exports = {
  24. meta: {
  25. type: "suggestion",
  26. docs: {
  27. description: "Disallow arrow functions where they could be confused with comparisons",
  28. recommended: false,
  29. url: "https://eslint.org/docs/rules/no-confusing-arrow"
  30. },
  31. fixable: "code",
  32. schema: [{
  33. type: "object",
  34. properties: {
  35. allowParens: { type: "boolean", default: true },
  36. onlyOneSimpleParam: { type: "boolean", default: false }
  37. },
  38. additionalProperties: false
  39. }],
  40. messages: {
  41. confusing: "Arrow function used ambiguously with a conditional expression."
  42. }
  43. },
  44. create(context) {
  45. const config = context.options[0] || {};
  46. const allowParens = config.allowParens || (config.allowParens === void 0);
  47. const onlyOneSimpleParam = config.onlyOneSimpleParam;
  48. const sourceCode = context.getSourceCode();
  49. /**
  50. * Reports if an arrow function contains an ambiguous conditional.
  51. * @param {ASTNode} node A node to check and report.
  52. * @returns {void}
  53. */
  54. function checkArrowFunc(node) {
  55. const body = node.body;
  56. if (isConditional(body) &&
  57. !(allowParens && astUtils.isParenthesised(sourceCode, body)) &&
  58. !(onlyOneSimpleParam && !(node.params.length === 1 && node.params[0].type === "Identifier"))) {
  59. context.report({
  60. node,
  61. messageId: "confusing",
  62. fix(fixer) {
  63. // if `allowParens` is not set to true don't bother wrapping in parens
  64. return allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`);
  65. }
  66. });
  67. }
  68. }
  69. return {
  70. ArrowFunctionExpression: checkArrowFunc
  71. };
  72. }
  73. };