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>
9 const astUtils = require("./utils/ast-utils.js");
11 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
16 * Checks whether or not a node is a conditional expression.
17 * @param {ASTNode} node node to test
18 * @returns {boolean} `true` if the node is a conditional expression.
20 function isConditional(node) {
21 return node && node.type === "ConditionalExpression";
24 //------------------------------------------------------------------------------
26 //------------------------------------------------------------------------------
33 description: "disallow arrow functions where they could be confused with comparisons",
34 category: "ECMAScript 6",
36 url: "https://eslint.org/docs/rules/no-confusing-arrow"
44 allowParens: { type: "boolean", default: true }
46 additionalProperties: false
50 confusing: "Arrow function used ambiguously with a conditional expression."
55 const config = context.options[0] || {};
56 const allowParens = config.allowParens || (config.allowParens === void 0);
57 const sourceCode = context.getSourceCode();
61 * Reports if an arrow function contains an ambiguous conditional.
62 * @param {ASTNode} node A node to check and report.
65 function checkArrowFunc(node) {
66 const body = node.body;
68 if (isConditional(body) && !(allowParens && astUtils.isParenthesised(sourceCode, body))) {
71 messageId: "confusing",
74 // if `allowParens` is not set to true don't bother wrapping in parens
75 return allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`);
82 ArrowFunctionExpression: checkArrowFunc