@putout/plugin-destructuring
v1.3.0
Published
๐Putout plugin adds ability to transform destructuring
Maintainers
Readme
@putout/plugin-destructuring 
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.
Install
npm i @putout/plugin-destructuringRules
- โ apply-object;
- โ apply-array;
- โ convert-object-to-array;
- โ extract-properties;
- โ remove-useless-object;
- โ remove-useless-arguments;
- โ remove-useless-variables;
- โ split-nested;
- โ split-call;
- โ merge-properties;
Config
{
"rules": {
"destructuring/apply-object": "on",
"destructuring/apply-array": "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"
}
}apply-array
โ Example of incorrect code
const first = array[0];โ Example of correct code
const [first] = array;apply-object
โ Example of incorrect code
const name = user.name;
hello = world.hello;โ Example of correct code
const {name} = user;
({hello} = world);remove-useless-object
Check out in ๐Putout Editor.
โ Example of incorrect code
const {maxElementsInOneLine} = {
options,
};โ Example of correct code
const {maxElementsInOneLine} = options;convert-object-to-array
Check out in ๐Putout Editor.
โ Example of incorrect code
const {0: a, 1: b} = c;โ Example of correct code
const [a, b] = c;split-nested
- 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.
๐Putout plugin adds ability to split nested destructuring.
โ Example of incorrect code
const {
a: {
b,
},
a: {
b: x,
},
} = c;
function f({a}) {
const {b} = a;
console.log(b);
}โ Example of correct code
const {a} = c;
const {b, b: x} = a;
function f({a}) {
const {b} = a;
console.log(b);
}split-call
โ Example of incorrect code
console.log('hello')({uid} = path.scope);
console.log('hello')[uid] = path.scope;โ Example of correct code
console.log('hello');
({uid} = path.scope);
console.log('hello');
[uid] = path.scope;merge-properties
Checkout in ๐Putout Editor.
โ Example of incorrect code
const {one} = require('numbers');
const {two} = require('numbers');
({from} = data);
({to} = data);
({names} = data);โ Example of correct code
const {one, two} = require('numbers');
({
from,
to,
names,
} = data);remove-useless-arguments
โ Example of incorrect code
onIfStatement({
push,
generate,
abc,
helloworld,
});
function onIfStatement({push}) {}โ Example of correct code
onIfStatement({
push,
});
function onIfStatement({push}) {}remove-useless-variables
โ Example of incorrect code
function hi(c) {
const {a, b} = c;
}โ Example of correct code
function hi({a, b}) {}extract-properties
Equal Deep
โ Example of incorrect code
const {replaceWith} = a.operate;
const {isIdentifier} = a.types;โ Example of correct code
const {operator, types} = a;
const {replaceWith} = operator;
const {isIdentifier} = types;Not Equal Deep
โ Example of incorrect code
const {replaceWith} = a;
const {isIdentifier} = a.types;โ Example of correct code
const {replaceWith, types} = a;
const {isIdentifier} = types;License
MIT
