@putout/plugin-variables
v1.6.1
Published
πPutout plugin adds ability to find and remove useless
Maintainers
Readme
@putout/plugin-variables 
A variable is a named reference to a value.
(c) MDN
πPutout plugin adds ability to transform variables.
Install
npm i @putout/plugin-variables -DRules
- β apply-declarations-order;
- β convert-const-to-let;
- β extract-keywords;
- β reuse-duplicate-init;
- β remove-useless-assignment;
- β remove-useless-declaration;
- β remove-useless-duplicate;
- β remove-useless-variables;
- β remove-useless-rename;
- β remove-unused;
- β split-declarations;
Config
{
"rules": {
"variables/apply-declarations-order": "on",
"variables/convert-const-to-let": "on",
"variables/extract-keywords": "on",
"variables/reuse-duplicate-init": "on",
"variables/remove-useless-assignment": "on",
"variables/remove-useless-declaration": ["on", {
"maxLength": 20
}],
"variables/remove-useless-duplicate": "on",
"variables/remove-useless-rename": "on",
"variables/remove-useless-remove": "on",
"variables/remove-unused": "on",
"variables/split-declarations": "on"
}
}apply-declarations-order
Helps to reuse duplicate init. Checkout in πPutout Editor.
β Example of incorrect code
const {env} = require('node:process');
const process = require('node:process');β Example of correct code
const process = require('node:process');
const {env} = process;assignment
Checkout in πPutout Editor.
β Example of incorrect code
while (!(files = readDirectory(parentDir)).length) {}β Example of correct code
while (!readDirectory(parentDir).length) {}reuse-duplicate-init
Functions are one of the fundamental building blocks it contains set of statements that performs a calculations, takes some input and returns an output. To use a function, you must define it somewhere in the scope from which you wish to call it.
(c) MDN
πPutout plugin adds ability to reuse duplicate init.
β Example of incorrect code
const putout = require('putout');
const {
a,
b,
operator,
} = putout;
const {replaceWith} = operator;β Example of correct code
const putout = require('putout');
const {
a,
b,
operator,
} = putout;
const {replaceWith} = operator;remove-useless-rename
β Example of incorrect code
function hi(a) {
const b = a;
}β Example of correct code
function hi(b) {}remove
β Example of incorrect code
const child_process = require('node:child_process');
const {exec, spawn} = child_process;β Example of correct code
const {exec, spawn} = require('node:child_process');remove
Check it out in πPutout Editor.
β Example of incorrect code
const a = 5;
const b = a;
const c = 5;
d = c;β Example of correct code
const b = 5;
d = 5;remove-useless-declaration
Check it out in πPutout Editor.
β Example of incorrect code
function x() {
const a = 5;
return a;
}
const z = b.c.replace('x', 'y');
b.c = z;β Example of correct code
function x() {
return 5;
}
b.c = b.c.replace('x', 'y');remove-useless-duplicate
Check it out in πPutout Editor.
β Example of incorrect code
const DestructuringErrors = function DestructuringErrors(a, b) {
return [a, b];
};β Example of correct code
function DestructuringErrors(a, b) {
return [a, b];
}
bc = b.c.replace('x', 'y');remove-unreferenced
β Example of incorrect code
let a;
let b;
a = 5;
b = 6;
console.log(a);β Example of correct code
let a;
a = 5;
console.log(a);split-declarations
- The
letstatement declares a block-scoped local variable, optionally initializing it to a value.conststatements are also block-scoped. The value of a constant can't be changed through reassignment, and it can't be redeclared. However, if a constant is an object or array its properties or items can be updated or removed.(c) MDN
Add 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.
β Example of incorrect code
let a, b;β Example of correct code
let a;
let b;Comparison
Linter | Rule | Fix
--------|-------|------------|
π Putout | remove-debugger | β
β£ ESLint | no-var | β
convert-const-to-let
The
TypeErrorobject represents an error when attempting to modify a value that cannot be changed.(c) MDN
Convert const to let to avoid TypeError.
Check out in πPutout Editor.
β Example of incorrect code
let a = 5;
a = 3;β Example of correct code
let a = 5;
a = 3;remove-unused
A variable is a container for a value, like a
numberwe might use in a sum, or astringthat we might use as part of a sentence.(c) MDN
πPutout plugin adds ability to find and remove the variables that are declared, but:
- not passed as argument to a function;
- not used as operand in expression;
That is unused variables. Most likely it is a leftovers due to incomplete transforming of the code. Such variables take up space and gives no value so they must be removed.
βοΈRemember, when you writing a transform you can skip all parts related to removing unused variables and just reuse current plugin it will make your code simpler and less error prone.
βοΈNo, you cannot just look at referenced and constant fields to determine if you can remove variable and here is why one of the biggest plugins exists.
β Example of incorrect code
const a = 'hello';
const b = 'world';
console.log(a);β Example of correct code
const a = 'hello';
console.log(a);Comparison
Linter | Rule | Fix
--------|-------|------------|
π Putout| remove-unused-variables| β
β£ ESLint | no-unused-vars | β
extract-keywords
The JavaScript exceptions "unexpected token" occur when the parser does not see a token it recognizes at the given position, so it cannot make sense of the structure of the program. This might be a simple typo.
(c) MDN
Extract keywords from variables. Check out in πPutout Editor.
-export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle',
+export const isTemplateMiddle = (a) => a?.type === 'TemplateMiddle';
export const isTemplateTail = (a) => a?.type === 'TemplateTail';
-const a 5;
+const a = 5;
-export const packContent = (content) {
+export const packContent = (content) => {
console.log(a);
}License
MIT
