@putout/plugin-destructuring
🐊Putout plugin adds ability to transform destructuring
Last updated 2 months ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @putout/plugin-destructuring 
SYNC missed versions from official npm registry.

@putout/plugin-destructuring NPM version

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.

Install

npm i @putout/plugin-destructuring

Rules

Config

{
    "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"
    }
}

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, 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

❌ 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

Current Tags

  • 2.0.0                                ...           latest (2 months ago)

10 Versions

  • 2.0.0                                ...           2 months ago
  • 1.4.2                                ...           3 months ago
  • 1.4.1                                ...           3 months ago
  • 1.4.0                                ...           3 months ago
  • 1.3.0                                ...           4 months ago
  • 1.2.0                                ...           4 months ago
  • 1.1.0                                ...           4 months ago
  • 1.0.2                                ...           4 months ago
  • 1.0.1                                ...           4 months ago
  • 1.0.0                                ...           4 months ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 2
Last Day 0
Last Week 1
Last Month 0
Dependencies (0)
None
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |