2 * @fileoverview Rule to flag when return statement contains assignment
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils = require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
17 const SENTINEL_TYPE = /^(?:[a-zA-Z]+?Statement|ArrowFunctionExpression|FunctionExpression|ClassExpression)$/u;
19 //------------------------------------------------------------------------------
21 //------------------------------------------------------------------------------
28 description: "disallow assignment operators in `return` statements",
29 category: "Best Practices",
31 url: "https://eslint.org/docs/rules/no-return-assign"
36 enum: ["except-parens", "always"]
41 returnAssignment: "Return statement should not contain assignment.",
42 arrowAssignment: "Arrow function should not return assignment."
47 const always = (context.options[0] || "except-parens") !== "except-parens";
48 const sourceCode = context.getSourceCode();
51 AssignmentExpression(node) {
52 if (!always && astUtils.isParenthesised(sourceCode, node)) {
56 let currentChild = node;
57 let parent = currentChild.parent;
59 // Find ReturnStatement or ArrowFunctionExpression in ancestors.
60 while (parent && !SENTINEL_TYPE.test(parent.type)) {
61 currentChild = parent;
62 parent = parent.parent;
66 if (parent && parent.type === "ReturnStatement") {
69 messageId: "returnAssignment"
71 } else if (parent && parent.type === "ArrowFunctionExpression" && parent.body === currentChild) {
74 messageId: "arrowAssignment"