2 * @fileoverview Enforces that a return statement is present in property getters.
3 * @author Aladdin-ADD(hh_2013@foxmail.com)
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
17 const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u;
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.
24 function isReachable(segment) {
25 return segment.reachable;
29 * Gets a readable location.
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.
35 function getId(node) {
36 return node.id || node;
39 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
48 description: "enforce `return` statements in getters",
49 category: "Possible Errors",
51 url: "https://eslint.org/docs/rules/getter-return"
65 additionalProperties: false
70 expected: "Expected to return a value in {{name}}.",
71 expectedAlways: "Expected {{name}} to always return a value."
77 const options = context.options[0] || { allowImplicit: false };
88 * Checks whether or not the last code path segment is reachable.
89 * Then reports this function if the segment is reachable.
91 * If the last code path segment is reachable, there are paths which are not
93 * @param {ASTNode} node A node to check.
96 function checkLastSegment(node) {
97 if (funcInfo.shouldCheck &&
98 funcInfo.codePath.currentSegments.some(isReachable)
102 loc: getId(node).loc.start,
103 messageId: funcInfo.hasReturn ? "expectedAlways" : "expected",
105 name: astUtils.getFunctionNameWithKind(funcInfo.node)
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.
116 function isGetter(node) {
117 const parent = node.parent;
119 if (TARGET_NODE_TYPE.test(node.type) && node.body.type === "BlockStatement") {
120 if (parent.kind === "get") {
123 if (parent.type === "Property" && astUtils.getStaticPropertyName(parent) === "get" && parent.parent.type === "ObjectExpression") {
125 // Object.defineProperty()
126 if (parent.parent.parent.type === "CallExpression" &&
127 astUtils.getStaticPropertyName(parent.parent.parent.callee) === "defineProperty") {
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") {
144 // Stacks this function's information.
145 onCodePathStart(codePath, node) {
150 shouldCheck: isGetter(node),
155 // Pops this function's information.
157 funcInfo = funcInfo.upper;
160 // Checks the return statement is valid.
161 ReturnStatement(node) {
162 if (funcInfo.shouldCheck) {
163 funcInfo.hasReturn = true;
165 // if allowImplicit: false, should also check node.argument
166 if (!options.allowImplicit && !node.argument) {
169 messageId: "expected",
171 name: astUtils.getFunctionNameWithKind(funcInfo.node)
178 // Reports a given function if the last path is reachable.
179 "FunctionExpression:exit": checkLastSegment,
180 "ArrowFunctionExpression:exit": checkLastSegment