No Destructuring
Disallow destructuring patterns to encourage descriptive parameter names and explicit property access.
Summary
The no-destructuring rule reports destructuring patterns in five contexts: callback parameters, for...of loops, standalone function parameters, variable declarations, and assignment expressions. Each check can be independently toggled. Destructuring in for...of is allowed when the iterable is an Object.entries() call.
Why Use This Rule?
- Encourages descriptive parameter names that convey the whole object's role.
- Makes property access explicit so readers can trace where each value comes from.
- Keeps callback bodies readable with named intermediate constants.
Examples
- Correct
- Incorrect
// Callback — descriptive param, explicit access.
items.map((item) => item.name);
// for...of — Object.entries() destructuring is allowed.
for (const [key, entry] of Object.entries(obj)) {
process(key, entry);
}
// Variable — assign and access separately.
const config = getConfig();
const name = config.name;
// Callback — destructured parameter.
items.map(({ name }) => name);
// for...of — destructuring without Object.entries().
for (const [key, value] of grouped) {}
// Variable — destructured declaration.
const { name } = config;
// Function — destructured parameter.
function fn({ id }) { return id; }
// Assignment — destructured assignment.
({ name } = config);
Configuration
Enable the rule in your ESLint flat config:
import { noDestructuring } from '@cbnventures/nova/rules/eslint';
export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-destructuring': noDestructuring,
},
},
},
rules: {
'@cbnventures/nova/no-destructuring': ['warn'],
},
},
];
Options
| Option | Type | Default | Description |
|---|---|---|---|
functionParams | boolean | true | Ban destructuring in standalone function/method parameters. |
callbackParams | boolean | true | Ban destructuring in callback parameters of array methods. |
forOfLoops | boolean | true | Ban destructuring in for...of (except Object.entries()). |
variableDeclarations | boolean | true | Ban destructuring in const/let/var declarations. |
assignmentExpressions | boolean | true | Ban destructuring in assignment expressions. |
Set any option to false to allow destructuring in that specific context.
Troubleshooting
- Warning fires on
Object.entries()destructuring. — TheforOfLoopscheck specifically allowsObject.entries(). If the warning still fires, make sure the iterable is a directObject.entries(x)call. - Want to allow destructuring in variable declarations only. — Set
variableDeclarations: falsewhile keeping the other checks enabled.