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