πPutout plugin adds ability to transform destructuring
npm install @putout/plugin-destructuring[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-destructuring.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-destructuring"npm"
> The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
>
> (c) MDN
πPutout plugin adds ability to use destructuring on variable declarations.
```
npm i @putout/plugin-destructuring
- β
apply-array;
- β
apply-object;
- β
convert-object-to-array;
- β
extract-properties;
- β
remove-useless-object;
- β
remove-useless-arguments;
- β
remove-useless-variables;
- β
split-nested;
- β
split-call;
- β
merge-properties;
`json`
{
"rules": {
"destructuring/apply-array": "on",
"destructuring/apply-object": "on",
"destructuring/convert-object-to-array": "on",
"destructuring/extract-properties": "on",
"destructuring/remove-useless-object": "on",
"destructuring/remove-useless-arguments": "on",
"destructuring/remove-useless-variables": "on",
"destructuring/split-nested": "on",
"destructuring/split-call": "on",
"destructuring/merge-properties": "on"
}
}
`js`
const first = array[0];
`js`
const [first] = array;
`js
const name = user.name;
hello = world.hello;
`
`js
const {name} = user;
({hello} = world);
`
Check out in πPutout Editor.
`js`
const {maxElementsInOneLine} = {
options,
};
`js`
const {maxElementsInOneLine} = options;
Check out in πPutout Editor.
`js`
const {0: a, 1: b} = c;
`js`
const [a, b] = c;
> - Don't use nested destructuring on data that comes from any external data sources (such as REST APIs, GraphQL endpoints or files).
> - Don't use nested destructuring on function arguments that have long or complicated signatures.
>
> (c) Destructuring in JavaScript: the not so good parts
`js
const {
a: {
b,
},
a: {
b: x,
},
} = c;
function f({a}) {
const {b} = a;
console.log(b);
}
`
`js
const {a} = c;
const {b, b: x} = a;
function f({a}) {
const {b} = a;
console.log(b);
}
`
`js`
console.log('hello')({uid} = path.scope);
console.log('hello')[uid] = path.scope;
`js
console.log('hello');
({uid} = path.scope);
console.log('hello');
[uid] = path.scope;
`
Checkout in πPutout Editor.
`js
const {one} = require('numbers');
const {two} = require('numbers');
({from} = data);
({to} = data);
({names} = data);
`
`js
const {one, two} = require('numbers');
({
from,
to,
names,
} = data);
`
`js
onIfStatement({
push,
generate,
abc,
helloworld,
});
function onIfStatement({push}) {}
`
`js
onIfStatement({
push,
});
function onIfStatement({push}) {}
`
`js`
function hi(c) {
const {a, b} = c;
}
`js`
function hi({a, b}) {}
#### β Example of incorrect code
`js`
const {replaceWith} = a.operate;
const {isIdentifier} = a.types;
#### β Example of correct code
`js
const {operator, types} = a;
const {replaceWith} = operator;
const {isIdentifier} = types;
`
#### β Example of incorrect code
`js`
const {replaceWith} = a;
const {isIdentifier} = a.types;
#### β Example of correct code
`js``
const {replaceWith, types} = a;
const {isIdentifier} = types;
MIT