no-return-assign
Disallow assignments in return statements
return total = price + tax; can be a typo for return total === price + tax;, and a reader can't tell which one you meant. Even when you do want the assignment, it hides a side effect in a line that looks like it only returns a value.
The rule also covers an arrow function whose body is an assignment.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-return-assign" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"no-return-assign" = ["error", "except-parens"] # "except-parens" or "always"Examples#
example.js2 problems
function setName(user, name) {
return user.name = name;Return statement should not contain assignment.
}
const select = (item) => selected = itemArrow function should not return assignment.;example.jsNo problems
function setName(user, name) {
user.name = name;
return name;
}
const select = (item) => {
selected = item;
};Options#
| Option | What it does |
|---|---|
"except-parens" (default) | Allow an assignment that's wrapped in parentheses. |
"always" | Report every assignment in a return. |
The parentheses show that the assignment is on purpose:
example.jsNo problems
function next() {
return (current = current.next);
}example.jswith
"always"1 problemfunction next() {
return (current = current.next);Return statement should not contain assignment.
}