A variable is a named reference to a value.
(c) MDN
🐊Putout plugin adds ability to transform variables.
npm i @putout/plugin-variables -D
- ✅ 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;
{
"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"
}
}Helps to reuse duplicate init. Checkout in 🐊Putout Editor.
const {env} = require('node:process');
const process = require('node:process');const process = require('node:process');
const {env} = process;Checkout in 🐊Putout Editor.
while (!(files = readDirectory(parentDir)).length) {}while (!readDirectory(parentDir).length) {}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.
const putout = require('putout');
const {
a,
b,
operator,
} = require('putout');
const {replaceWith} = operator;const putout = require('putout');
const {
a,
b,
operator,
} = putout;
const {replaceWith} = operator;function hi(a) {
const b = a;
}function hi(b) {}const child_process = require('node:child_process');
const {exec, spawn} = child_process;const {exec, spawn} = require('node:child_process');Check it out in 🐊Putout Editor.
const a = 5;
const b = a;
const c = 5;
d = c;const b = 5;
d = 5;Check it out in 🐊Putout Editor.
function x() {
const a = 5;
return a;
}
const z = b.c.replace('x', 'y');
b.c = z;function x() {
return 5;
}
b.c = b.c.replace('x', 'y');Check it out in 🐊Putout Editor.
const DestructuringErrors = function DestructuringErrors(a, b) {
return [a, b];
};function DestructuringErrors(a, b) {
return [a, b];
}
bc = b.c.replace('x', 'y');let a;
let b;
a = 5;
b = 6;
console.log(a);let a;
a = 5;
console.log(a);
- 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.
let a, b;let a;
let b;| Linter | Rule | Fix |
|---|---|---|
| 🐊 Putout | remove-debugger |
✅ |
| ⏣ ESLint | no-var |
✅ |
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.
let a = 5;
a = 3;let a = 5;
a = 3;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.
const a = 'hello';
const b = 'world';
console.log(a);const a = 'hello';
console.log(a);| Linter | Rule | Fix |
|---|---|---|
| 🐊 Putout | remove-unused-variables |
✅ |
| ⏣ ESLint | no-unused-vars |
❌ |
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);
}MIT