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

@putout/plugin-variables NPM version

A variable is a named reference to a value.

(c) MDN

????Putout plugin adds ability to transform variables.

Install

npm i @putout/plugin-variables -D

Rules

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,
} = require('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 let statement declares a block-scoped local variable, optionally initializing it to a value.
  • const statements 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 TypeError object 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 number we might use in a sum, or a string that 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

Current Tags

  • 2.2.0                                ...           latest (a month ago)

12 Versions

  • 2.2.0                                ...           a month ago
  • 2.1.0                                ...           a month ago
  • 2.0.0                                ...           2 months ago
  • 1.6.1                                ...           3 months ago
  • 1.6.0                                ...           3 months ago
  • 1.5.0                                ...           4 months ago
  • 1.4.0                                ...           4 months ago
  • 1.3.1                                ...           4 months ago
  • 1.3.0                                ...           4 months ago
  • 1.2.0                                ...           4 months ago
  • 1.1.0                                ...           4 months ago
  • 1.0.0                                ...           4 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 2
Last Month 0
Dependencies (0)
None
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |