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 parent = node.parent;
44 return parent.type === "CallExpression" &&
45 parent.arguments[index] === node &&
46 parent.callee.type === "MemberExpression" &&
47 astUtils.getStaticPropertyName(parent.callee) === methodName &&
48 parent.callee.object.type === "Identifier" &&
49 parent.callee.object.name === objectName &&
50 isGlobalReference(parent.callee.object, scope);
54 * Determines whether the given node is used as a property descriptor.
55 * @param {ASTNode} node The node to check.
56 * @param {Scope} scope Scope to which the node belongs.
57 * @returns {boolean} `true` if the node is a property descriptor.
59 function isPropertyDescriptor(node, scope) {
61 isArgumentOfGlobalMethodCall(node, scope, "Object", "defineProperty", 2) ||
62 isArgumentOfGlobalMethodCall(node, scope, "Reflect", "defineProperty", 2)
67 const parent = node.parent;
70 parent.type === "Property" &&
73 const grandparent = parent.parent;
76 grandparent.type === "ObjectExpression" &&
78 isArgumentOfGlobalMethodCall(grandparent, scope, "Object", "create", 1) ||
79 isArgumentOfGlobalMethodCall(grandparent, scope, "Object", "defineProperties", 1)
90 * Determines whether the given function node is used as a setter function.
91 * @param {ASTNode} node The node to check.
92 * @param {Scope} scope Scope to which the node belongs.
93 * @returns {boolean} `true` if the node is a setter.
95 function isSetter(node, scope) {
96 const parent = node.parent;
99 parent.kind === "set" &&
100 parent.value === node
103 // Setter in an object literal or in a class
108 parent.type === "Property" &&
109 parent.value === node &&
110 astUtils.getStaticPropertyName(parent) === "set" &&
111 parent.parent.type === "ObjectExpression" &&
112 isPropertyDescriptor(parent.parent, scope)
115 // Setter in a property descriptor
123 * Finds function's outer scope.
124 * @param {Scope} scope Function's own scope.
125 * @returns {Scope} Function's outer scope.
127 function getOuterScope(scope) {
128 const upper = scope.upper;
130 if (upper.type === "function-expression-name") {
137 //------------------------------------------------------------------------------
139 //------------------------------------------------------------------------------
146 description: "disallow returning values from setters",
147 category: "Possible Errors",
149 url: "https://eslint.org/docs/rules/no-setter-return"
155 returnsValue: "Setter cannot return a value."
163 * Creates and pushes to the stack a function info object for the given function node.
164 * @param {ASTNode} node The function node.
167 function enterFunction(node) {
168 const outerScope = getOuterScope(context.getScope());
172 isSetter: isSetter(node, outerScope)
177 * Pops the current function info object from the stack.
180 function exitFunction() {
181 funcInfo = funcInfo.upper;
185 * Reports the given node.
186 * @param {ASTNode} node Node to report.
189 function report(node) {
190 context.report({ node, messageId: "returnsValue" });
196 * Function declarations cannot be setters, but we still have to track them in the `funcInfo` stack to avoid
197 * false positives, because a ReturnStatement node can belong to a function declaration inside a setter.
199 * Note: A previously declared function can be referenced and actually used as a setter in a property descriptor,
200 * but that's out of scope for this rule.
202 FunctionDeclaration: enterFunction,
203 FunctionExpression: enterFunction,
204 ArrowFunctionExpression(node) {
207 if (funcInfo.isSetter && node.expression) {
209 // { set: foo => bar } property descriptor. Report implicit return 'bar' as the equivalent for a return statement.
214 "FunctionDeclaration:exit": exitFunction,
215 "FunctionExpression:exit": exitFunction,
216 "ArrowFunctionExpression:exit": exitFunction,
218 ReturnStatement(node) {
220 // Global returns (e.g., at the top level of a Node module) don't have `funcInfo`.
221 if (funcInfo && funcInfo.isSetter && node.argument) {