UNPKG

2.55 kBJavaScriptView Raw
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
7"use strict";
8
9const astUtils = require("../util/ast-utils.js");
10
11//------------------------------------------------------------------------------
12// Helpers
13//------------------------------------------------------------------------------
14
15/**
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.
19 */
20function isConditional(node) {
21 return node && node.type === "ConditionalExpression";
22}
23
24//------------------------------------------------------------------------------
25// Rule Definition
26//------------------------------------------------------------------------------
27
28module.exports = {
29 meta: {
30 type: "suggestion",
31
32 docs: {
33 description: "disallow arrow functions where they could be confused with comparisons",
34 category: "ECMAScript 6",
35 recommended: false,
36 url: "https://eslint.org/docs/rules/no-confusing-arrow"
37 },
38
39 fixable: "code",
40
41 schema: [{
42 type: "object",
43 properties: {
44 allowParens: { type: "boolean", default: false }
45 },
46 additionalProperties: false
47 }],
48
49 messages: {
50 confusing: "Arrow function used ambiguously with a conditional expression."
51 }
52 },
53
54 create(context) {
55 const config = context.options[0] || {};
56 const sourceCode = context.getSourceCode();
57
58 /**
59 * Reports if an arrow function contains an ambiguous conditional.
60 * @param {ASTNode} node - A node to check and report.
61 * @returns {void}
62 */
63 function checkArrowFunc(node) {
64 const body = node.body;
65
66 if (isConditional(body) && !(config.allowParens && astUtils.isParenthesised(sourceCode, body))) {
67 context.report({
68 node,
69 messageId: "confusing",
70 fix(fixer) {
71
72 // if `allowParens` is not set to true dont bother wrapping in parens
73 return config.allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`);
74 }
75 });
76 }
77 }
78
79 return {
80 ArrowFunctionExpression: checkArrowFunc
81 };
82 }
83};