]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-setter-return.js
2 * @fileoverview Rule to disallow returning values from setters
3 * @author Milos Djermanovic
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
13 const { findVariable
} = require("eslint-utils");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
20 * Determines whether the given identifier node is a reference to a global variable.
21 * @param {ASTNode} node `Identifier` node to check.
22 * @param {Scope} scope Scope to which the node belongs.
23 * @returns {boolean} True if the identifier is a reference to a global variable.
25 function isGlobalReference(node
, scope
) {
26 const variable
= findVariable(scope
, node
);
28 return variable
!== null && variable
.scope
.type
=== "global" && variable
.defs
.length
=== 0;
32 * Determines whether the given node is an argument of the specified global method call, at the given `index` position.
33 * E.g., for given `index === 1`, this function checks for `objectName.methodName(foo, node)`, where objectName is a global variable.
34 * @param {ASTNode} node The node to check.
35 * @param {Scope} scope Scope to which the node belongs.
36 * @param {string} objectName Name of the global object.
37 * @param {string} methodName Name of the method.
38 * @param {number} index The given position.
39 * @returns {boolean} `true` if the node is argument at the given position.
41 function isArgumentOfGlobalMethodCall(node
, scope
, objectName
, methodName
, index
) {
42 const callNode
= node
.parent
;
44 return callNode
.type
=== "CallExpression" &&
45 callNode
.arguments
[index
] === node
&&
46 astUtils
.isSpecificMemberAccess(callNode
.callee
, objectName
, methodName
) &&
47 isGlobalReference(astUtils
.skipChainExpression(callNode
.callee
).object
, scope
);
51 * Determines whether the given node is used as a property descriptor.
52 * @param {ASTNode} node The node to check.
53 * @param {Scope} scope Scope to which the node belongs.
54 * @returns {boolean} `true` if the node is a property descriptor.
56 function isPropertyDescriptor(node
, scope
) {
58 isArgumentOfGlobalMethodCall(node
, scope
, "Object", "defineProperty", 2) ||
59 isArgumentOfGlobalMethodCall(node
, scope
, "Reflect", "defineProperty", 2)
64 const parent
= node
.parent
;
67 parent
.type
=== "Property" &&
70 const grandparent
= parent
.parent
;
73 grandparent
.type
=== "ObjectExpression" &&
75 isArgumentOfGlobalMethodCall(grandparent
, scope
, "Object", "create", 1) ||
76 isArgumentOfGlobalMethodCall(grandparent
, scope
, "Object", "defineProperties", 1)
87 * Determines whether the given function node is used as a setter function.
88 * @param {ASTNode} node The node to check.
89 * @param {Scope} scope Scope to which the node belongs.
90 * @returns {boolean} `true` if the node is a setter.
92 function isSetter(node
, scope
) {
93 const parent
= node
.parent
;
96 parent
.kind
=== "set" &&
100 // Setter in an object literal or in a class
105 parent
.type
=== "Property" &&
106 parent
.value
=== node
&&
107 astUtils
.getStaticPropertyName(parent
) === "set" &&
108 parent
.parent
.type
=== "ObjectExpression" &&
109 isPropertyDescriptor(parent
.parent
, scope
)
112 // Setter in a property descriptor
120 * Finds function's outer scope.
121 * @param {Scope} scope Function's own scope.
122 * @returns {Scope} Function's outer scope.
124 function getOuterScope(scope
) {
125 const upper
= scope
.upper
;
127 if (upper
.type
=== "function-expression-name") {
134 //------------------------------------------------------------------------------
136 //------------------------------------------------------------------------------
143 description
: "disallow returning values from setters",
144 category
: "Possible Errors",
146 url
: "https://eslint.org/docs/rules/no-setter-return"
152 returnsValue
: "Setter cannot return a value."
160 * Creates and pushes to the stack a function info object for the given function node.
161 * @param {ASTNode} node The function node.
164 function enterFunction(node
) {
165 const outerScope
= getOuterScope(context
.getScope());
169 isSetter
: isSetter(node
, outerScope
)
174 * Pops the current function info object from the stack.
177 function exitFunction() {
178 funcInfo
= funcInfo
.upper
;
182 * Reports the given node.
183 * @param {ASTNode} node Node to report.
186 function report(node
) {
187 context
.report({ node
, messageId
: "returnsValue" });
193 * Function declarations cannot be setters, but we still have to track them in the `funcInfo` stack to avoid
194 * false positives, because a ReturnStatement node can belong to a function declaration inside a setter.
196 * Note: A previously declared function can be referenced and actually used as a setter in a property descriptor,
197 * but that's out of scope for this rule.
199 FunctionDeclaration
: enterFunction
,
200 FunctionExpression
: enterFunction
,
201 ArrowFunctionExpression(node
) {
204 if (funcInfo
.isSetter
&& node
.expression
) {
206 // { set: foo => bar } property descriptor. Report implicit return 'bar' as the equivalent for a return statement.
211 "FunctionDeclaration:exit": exitFunction
,
212 "FunctionExpression:exit": exitFunction
,
213 "ArrowFunctionExpression:exit": exitFunction
,
215 ReturnStatement(node
) {
217 // Global returns (e.g., at the top level of a Node module) don't have `funcInfo`.
218 if (funcInfo
&& funcInfo
.isSetter
&& node
.argument
) {