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
Configuration
Options
warning
ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.
| Option | Type | Default | Description |
|---|---|---|---|
ignoreFiles | string[] | [] | File names to skip. Supports a leading-* suffix (*.test.ts), an exact path, or a trailing path segment; ** globs are not supported. |
Autofix
This rule provides automatic fixes. Run ESLint with the --fix flag:
npx eslint --fix . --rule '@cbnventures/nova/no-assign-then-return: error'
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. - Destructuring is not flagged — Declarations that destructure (e.g.
const { active } = obj; return active;orconst [first] = arr; return first;) are skipped; only a plainconst name = expr;followed byreturn name;is reported. - Only single-declarator
conststatements are checked — Aconstdeclaring multiple variables (e.g.const a = x(), result = y(); return result;) is skipped, since removing it would drop the other declarations.