npm.io
8.5.0 • Published 5d ago

@putout/plugin-declare

Licence
MIT
Version
8.5.0
Deps
0
Size
9 kB
Vulns
0
Weekly
0
Stars
804

@putout/plugin-declare NPM version

Putout plugin adds ability to declare variable that was not defined before, including:

Install

npm i putout @putout/plugin-declare -D

Add .putout.json with:

{
    "plugins": {
        "declare": "on"
    }
}

Rules

Use options:

  • dismiss for variables you don't want to declare;
  • declarations to declare variables;
{
    "rules": {
        "declare": ["on", {
            "declarations": {
                "hello": "import {hello} from 'world'"
            },
            "dismiss": [
                "assert",
                "entries",
                "parse",
                "stringify",
                "defineProperty",
                "noop",
                "join",
                "keys",
                "values",
                "stopAll",
                "once",
                "putout"
            ]
        }]
    }
}

assign

Example of incorrect code
const hello = 'world';
const object = {};

assign(object, {
    hello,
});
Example of correct code
const hello = 'world';
const object = {};
const {assign} = Object;

assign(object, {
    hello,
});

keys

Example of incorrect code
const hello = 'world';
const object = {};

const allKeys = keys(object);
Example of correct code
const hello = 'world';
const object = {};
const {keys} = Object;

const allKeys = keys(object);

values

Example of incorrect code
const object = {};
const allValues = values(object);

Example of correct code

const {values} = Object;

const object = {};
const allValues = values(object);

noop

Example of incorrect code
noop();
Example of correct code
const noop = () => {};
noop();

Types

Example of incorrect code
const isFn = (a) => typeof a === 'function';

if (isFn(fn))
    fn();
Example of correct code
const isFn = (a) => typeof a === 'function';

if (isFn(fn))
    fn();

Maybe

Example of incorrect code
const a = maybeArray(b);
Example of correct code
const {isArray} = Array;
const maybeArray = (a) => isArray(a) ? a : [a];

const a = maybeArray(b);

entries

Example of incorrect code
entries([1, 2, 3]);
Example of correct code
const {entries} = Object;

entries([1, 2, 3]);

fromEntries

Example of incorrect code
fromEntries(['hello', 'world']);
Example of correct code
const {fromEntries} = Object;

fromEntries(['hello', 'world']);

fromCharCode

Example of incorrect code
const encoded = new TextEncoder().encode(str);
fromCharCode(...encoded);
Example of correct code
const {fromCharCode} = String;
const encoded = new TextEncoder().encode(str);

fromCharCode(...encoded);

License

MIT

Keywords