]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/getter-return.js
8bb42536e8c06752ae8202fdaf44c893546a0374
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
;
28 //------------------------------------------------------------------------------
30 //------------------------------------------------------------------------------
37 description
: "enforce `return` statements in getters",
39 url
: "https://eslint.org/docs/rules/getter-return"
53 additionalProperties
: false
58 expected
: "Expected to return a value in {{name}}.",
59 expectedAlways
: "Expected {{name}} to always return a value."
65 const options
= context
.options
[0] || { allowImplicit
: false };
66 const sourceCode
= context
.getSourceCode();
77 * Checks whether or not the last code path segment is reachable.
78 * Then reports this function if the segment is reachable.
80 * If the last code path segment is reachable, there are paths which are not
82 * @param {ASTNode} node A node to check.
85 function checkLastSegment(node
) {
86 if (funcInfo
.shouldCheck
&&
87 funcInfo
.codePath
.currentSegments
.some(isReachable
)
91 loc
: astUtils
.getFunctionHeadLoc(node
, sourceCode
),
92 messageId
: funcInfo
.hasReturn
? "expectedAlways" : "expected",
94 name
: astUtils
.getFunctionNameWithKind(funcInfo
.node
)
101 * Checks whether a node means a getter function.
102 * @param {ASTNode} node a node to check.
103 * @returns {boolean} if node means a getter, return true; else return false.
105 function isGetter(node
) {
106 const parent
= node
.parent
;
108 if (TARGET_NODE_TYPE
.test(node
.type
) && node
.body
.type
=== "BlockStatement") {
109 if (parent
.kind
=== "get") {
112 if (parent
.type
=== "Property" && astUtils
.getStaticPropertyName(parent
) === "get" && parent
.parent
.type
=== "ObjectExpression") {
114 // Object.defineProperty()
115 if (parent
.parent
.parent
.type
=== "CallExpression" &&
116 astUtils
.getStaticPropertyName(parent
.parent
.parent
.callee
) === "defineProperty") {
120 // Object.defineProperties()
121 if (parent
.parent
.parent
.type
=== "Property" &&
122 parent
.parent
.parent
.parent
.type
=== "ObjectExpression" &&
123 parent
.parent
.parent
.parent
.parent
.type
=== "CallExpression" &&
124 astUtils
.getStaticPropertyName(parent
.parent
.parent
.parent
.parent
.callee
) === "defineProperties") {
133 // Stacks this function's information.
134 onCodePathStart(codePath
, node
) {
139 shouldCheck
: isGetter(node
),
144 // Pops this function's information.
146 funcInfo
= funcInfo
.upper
;
149 // Checks the return statement is valid.
150 ReturnStatement(node
) {
151 if (funcInfo
.shouldCheck
) {
152 funcInfo
.hasReturn
= true;
154 // if allowImplicit: false, should also check node.argument
155 if (!options
.allowImplicit
&& !node
.argument
) {
158 messageId
: "expected",
160 name
: astUtils
.getFunctionNameWithKind(funcInfo
.node
)
167 // Reports a given function if the last path is reachable.
168 "FunctionExpression:exit": checkLastSegment
,
169 "ArrowFunctionExpression:exit": checkLastSegment