The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from
arrays, orpropertiesfromobjects, into distinctvariables.(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;
{
"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"
}
}const first = array[0];const [first] = array;const name = user.name;
hello = world.hello;const {name} = user;
({hello} = world);Check out in 🐊Putout Editor.
const {maxElementsInOneLine} = {
options,
};const {maxElementsInOneLine} = options;Check out in 🐊Putout Editor.
const {0: a, 1: b} = c;const [a, b] = c;
- Don't use nested destructuring on data that comes from any external data sources (such as
REST APIs,GraphQLendpoints or files).- Don't use nested destructuring on function arguments that have long or complicated signatures.
const {
a: {
b,
},
a: {
b: x,
},
} = c;
function f({a}) {
const {b} = a;
console.log(b);
}const {a} = c;
const {b, b: x} = a;
function f({a}) {
const {b} = a;
console.log(b);
}console.log('hello')({uid} = path.scope);
console.log('hello')[uid] = path.scope;console.log('hello');
({uid} = path.scope);
console.log('hello');
[uid] = path.scope;Checkout in 🐊Putout Editor.
const {one} = require('numbers');
const {two} = require('numbers');
({from} = data);
({to} = data);
({names} = data);const {one, two} = require('numbers');
({
from,
to,
names,
} = data);onIfStatement({
push,
generate,
abc,
helloworld,
});
function onIfStatement({push}) {}onIfStatement({
push,
});
function onIfStatement({push}) {}function hi(c) {
const {a, b} = c;
}function hi({a, b}) {}const {replaceWith} = a.operate;
const {isIdentifier} = a.types;const {operator, types} = a;
const {replaceWith} = operator;
const {isIdentifier} = types;const {replaceWith} = a;
const {isIdentifier} = a.types;const {replaceWith, types} = a;
const {isIdentifier} = types;MIT