Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-return-assign.js
1 /**
2  * @fileoverview Rule to flag when return statement contains assignment
3  * @author Ilya Volodin
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const astUtils = require("./utils/ast-utils");
12
13 //------------------------------------------------------------------------------
14 // Helpers
15 //------------------------------------------------------------------------------
16
17 const SENTINEL_TYPE = /^(?:[a-zA-Z]+?Statement|ArrowFunctionExpression|FunctionExpression|ClassExpression)$/u;
18
19 //------------------------------------------------------------------------------
20 // Rule Definition
21 //------------------------------------------------------------------------------
22
23 module.exports = {
24     meta: {
25         type: "suggestion",
26
27         docs: {
28             description: "disallow assignment operators in `return` statements",
29             category: "Best Practices",
30             recommended: false,
31             url: "https://eslint.org/docs/rules/no-return-assign"
32         },
33
34         schema: [
35             {
36                 enum: ["except-parens", "always"]
37             }
38         ]
39     },
40
41     create(context) {
42         const always = (context.options[0] || "except-parens") !== "except-parens";
43         const sourceCode = context.getSourceCode();
44
45         return {
46             AssignmentExpression(node) {
47                 if (!always && astUtils.isParenthesised(sourceCode, node)) {
48                     return;
49                 }
50
51                 let currentChild = node;
52                 let parent = currentChild.parent;
53
54                 // Find ReturnStatement or ArrowFunctionExpression in ancestors.
55                 while (parent && !SENTINEL_TYPE.test(parent.type)) {
56                     currentChild = parent;
57                     parent = parent.parent;
58                 }
59
60                 // Reports.
61                 if (parent && parent.type === "ReturnStatement") {
62                     context.report({
63                         node: parent,
64                         message: "Return statement should not contain assignment."
65                     });
66                 } else if (parent && parent.type === "ArrowFunctionExpression" && parent.body === currentChild) {
67                     context.report({
68                         node: parent,
69                         message: "Arrow function should not return assignment."
70                     });
71                 }
72             }
73         };
74     }
75 };