πPutout plugin adds ability to transform code related to assignment
npm install @putout/plugin-assignment[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-assignment.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-assignment "npm"
> The assignment operator (=) is used to assign a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables.
πPutout plugin adds ability to transform to new Node.js API and apply best practices.
```
npm i putout @putout/plugin-assignment -D
- β
convert-to-arrow-function;
- β
convert-to-comparison;
- β
convert-to-declaration;
- β
simplify;
- β
split;
`json`
{
"rules": {
"assignment/convert-to-arrow-function": "on",
"assignment/convert-to-comparison": "on",
"assignment/convert-to-declaration": "on",
"assignment/simplify": "on",
"assignment/split": "on"
}
}
> An arrow function expression is a compact alternative to a function expression.
>
> (c) MDN
Rule adds ability to convert assignment to arrow function.
`js`
const createRegExp = a = RegExp(a, 'g');
`js`
const createRegExp = (a) => RegExp(a, 'g');
> You should almost never have an if...else with an assignment like a = b as a condition.
>
> (c) MDN
`js`
if (a = b) {}
`js`
if (a === b) {}
> The const declaration declares block-scoped local variables. The value of a constant can't be changed through reassignment using the assignment operator, but if a constant is an object, its properties can be added, updated, or removed.
>
> (c) MDN
Checkout in πPutout Editor.
`js`
a = 5;
`js`
const a = 5;
`js
const {a} = {
a: 5,
};
const [b] = [5];
const c = (() => 7)();
`
`js`
const a = 5;
const b = 5;
const c = 7;
Rule adds ability to find and split variable declarations because (re)moving a line is simpler and less error prone then changing coma (=) to colon (;).
For the same reason, diff of changed declarations are more comfortable to read. Checkout in πPutout Editor.
`js`
a = b = c = 1;
`js``
a = 1;
b = a;
c = a;
MIT