This rule enforces the usage of single-level destructuring over deep destructuring.
// bad
// undefined `b` or `object` would cause a runtime error
const {a, b: {d: {e: f}}, c} = object;
// good
const {a, c} = object;
const f = object.?b.?d.?e;
```
Use Yarn or NPM to install the package.
yarn add eslint-plugin-destructure-depth --dev
Add destructure-depth
to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:
{
"plugins": ["destructure-depth"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"destructure-depth/max-depth": ["warn"]
}
}
To customize the allowed destructuring depth:
{
"rules": {
"destructure-depth/max-depth": [
"warn",
{
"object": {
"max": 1
}
}
]
}
}
This will allow up to 1 level deep of destructuring:
const {
a: { b },
} = object;