No Assign Then Return
Return expressions directly instead of assigning to an intermediate variable that is immediately returned.
Summary
The no-assign-then-return rule reports a return statement that returns an identifier when the immediately preceding statement is a const declaration of that same identifier. It is recommended to return the expression directly.
Why Use This Rule?
- Removes unnecessary intermediate variables that add visual noise.
- Makes the return value immediately visible without scanning back to the declaration.
- Keeps functions concise by eliminating a redundant line.
Examples
- Correct
- Incorrect
function getActive(items) {
return items.filter((item) => item.active === true);
}
// Variable is used before the return — this is fine.
function process(items) {
const filtered = items.filter((item) => item.active === true);
Logger.info(`Found ${filtered.length} items`);
return filtered;
}
function getActive(items) {
const filtered = items.filter((item) => item.active === true);
return filtered;
}
Configuration
Enable the rule in your ESLint flat config:
import { noAssignThenReturn } from '@cbnventures/nova/rules/eslint';
export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-assign-then-return': noAssignThenReturn,
},
},
},
rules: {
'@cbnventures/nova/no-assign-then-return': ['warn'],
},
},
];
This rule has no additional options.
Troubleshooting
- Warning fires but the variable name is descriptive. — It is recommended to return the expression directly. If a descriptive name is important, consider adding a comment above the return statement instead.
- Only
constdeclarations are checked. — Reassignable variables (let) are skipped because they might be modified between declaration and return.