Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / getter-return.js
1 /**
2  * @fileoverview Enforces that a return statement is present in property getters.
3  * @author Aladdin-ADD(hh_2013@foxmail.com)
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Helpers
16 //------------------------------------------------------------------------------
17 const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u;
18
19 /**
20  * Checks a given code path segment is reachable.
21  * @param {CodePathSegment} segment A segment to check.
22  * @returns {boolean} `true` if the segment is reachable.
23  */
24 function isReachable(segment) {
25     return segment.reachable;
26 }
27
28 /**
29  * Gets a readable location.
30  *
31  * - FunctionExpression -> the function name or `function` keyword.
32  * @param {ASTNode} node A function node to get.
33  * @returns {ASTNode|Token} The node or the token of a location.
34  */
35 function getId(node) {
36     return node.id || node;
37 }
38
39 //------------------------------------------------------------------------------
40 // Rule Definition
41 //------------------------------------------------------------------------------
42
43 module.exports = {
44     meta: {
45         type: "problem",
46
47         docs: {
48             description: "enforce `return` statements in getters",
49             category: "Possible Errors",
50             recommended: true,
51             url: "https://eslint.org/docs/rules/getter-return"
52         },
53
54         fixable: null,
55
56         schema: [
57             {
58                 type: "object",
59                 properties: {
60                     allowImplicit: {
61                         type: "boolean",
62                         default: false
63                     }
64                 },
65                 additionalProperties: false
66             }
67         ],
68
69         messages: {
70             expected: "Expected to return a value in {{name}}.",
71             expectedAlways: "Expected {{name}} to always return a value."
72         }
73     },
74
75     create(context) {
76
77         const options = context.options[0] || { allowImplicit: false };
78
79         let funcInfo = {
80             upper: null,
81             codePath: null,
82             hasReturn: false,
83             shouldCheck: false,
84             node: null
85         };
86
87         /**
88          * Checks whether or not the last code path segment is reachable.
89          * Then reports this function if the segment is reachable.
90          *
91          * If the last code path segment is reachable, there are paths which are not
92          * returned or thrown.
93          * @param {ASTNode} node A node to check.
94          * @returns {void}
95          */
96         function checkLastSegment(node) {
97             if (funcInfo.shouldCheck &&
98                 funcInfo.codePath.currentSegments.some(isReachable)
99             ) {
100                 context.report({
101                     node,
102                     loc: getId(node).loc.start,
103                     messageId: funcInfo.hasReturn ? "expectedAlways" : "expected",
104                     data: {
105                         name: astUtils.getFunctionNameWithKind(funcInfo.node)
106                     }
107                 });
108             }
109         }
110
111         /**
112          * Checks whether a node means a getter function.
113          * @param {ASTNode} node a node to check.
114          * @returns {boolean} if node means a getter, return true; else return false.
115          */
116         function isGetter(node) {
117             const parent = node.parent;
118
119             if (TARGET_NODE_TYPE.test(node.type) && node.body.type === "BlockStatement") {
120                 if (parent.kind === "get") {
121                     return true;
122                 }
123                 if (parent.type === "Property" && astUtils.getStaticPropertyName(parent) === "get" && parent.parent.type === "ObjectExpression") {
124
125                     // Object.defineProperty()
126                     if (parent.parent.parent.type === "CallExpression" &&
127                         astUtils.getStaticPropertyName(parent.parent.parent.callee) === "defineProperty") {
128                         return true;
129                     }
130
131                     // Object.defineProperties()
132                     if (parent.parent.parent.type === "Property" &&
133                         parent.parent.parent.parent.type === "ObjectExpression" &&
134                         parent.parent.parent.parent.parent.type === "CallExpression" &&
135                         astUtils.getStaticPropertyName(parent.parent.parent.parent.parent.callee) === "defineProperties") {
136                         return true;
137                     }
138                 }
139             }
140             return false;
141         }
142         return {
143
144             // Stacks this function's information.
145             onCodePathStart(codePath, node) {
146                 funcInfo = {
147                     upper: funcInfo,
148                     codePath,
149                     hasReturn: false,
150                     shouldCheck: isGetter(node),
151                     node
152                 };
153             },
154
155             // Pops this function's information.
156             onCodePathEnd() {
157                 funcInfo = funcInfo.upper;
158             },
159
160             // Checks the return statement is valid.
161             ReturnStatement(node) {
162                 if (funcInfo.shouldCheck) {
163                     funcInfo.hasReturn = true;
164
165                     // if allowImplicit: false, should also check node.argument
166                     if (!options.allowImplicit && !node.argument) {
167                         context.report({
168                             node,
169                             messageId: "expected",
170                             data: {
171                                 name: astUtils.getFunctionNameWithKind(funcInfo.node)
172                             }
173                         });
174                     }
175                 }
176             },
177
178             // Reports a given function if the last path is reachable.
179             "FunctionExpression:exit": checkLastSegment,
180             "ArrowFunctionExpression:exit": checkLastSegment
181         };
182     }
183 };