@putout/engine-runner
Run 🐊Putout plugins
Last updated 2 years ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/engine-runner 
SYNC missed versions from official npm registry.

@putout/engine-runner NPM version

Run 🐊Putout plugins.

Install

npm i @putout/engine-runner

Supported Plugin Types

There is a couple plugin types available in 🐊Putout:

All of them supports subset of JavaScript 🦎PutoutScript described in @putout/compare.

They goes from simplest to hardest. Let's start from Declarator.

Declarator

The simplest possible type of plugin, even simpler then Replacer:

module.exports.declare = () => ({
    isString: `const isString = (a) => typeof a === 'string'`,
});

Based on @putout/operator-declare, helps to add declaration to any referenced Identifier.

Replacer

Replacer converts code in declarative way. Simplest possible form of 🐊Putout Plugin, no need to use fix. Just from and to parts according to template variables syntax.

Simplest replace example:

module.exports.report = () => 'any message here';

module.exports.replace = () => ({
    'const a = 1': 'const b = 1',
});

// optional
module.exports.filter = (path) => {
    return true;
};

// optional
module.exports.match = () => ({
    'const __a = 1': ({__a}) => {
        return true;
    },
});

// optional
module.exports.exclude = () => [
    `const hello = 'world'`,
    'ArrowFunctionExpression',
];

Simplest remove example:

module.exports.report = () => 'debugger should not be used';

module.exports.replace = () => ({
    debugger: '',
});

// debugger; alert(); -> alert();

Templates:

module.exports.report = () => 'any message here';

module.exports.replace = () => ({
    'var __a = 1': 'const __a = 1',
});

// var x = 1; -> const x = 1;

A couple variables example:

module.exports.report = () => 'any message here';

module.exports.replace = () => ({
    'const __a = __b': 'const __b = __a',
});

// const hello = world; -> const world = hello;

Processing of node using functions

You can pass a function as object value for more soficticated processing.

Remove node:

module.exports.report = () => 'any message here';

module.exports.replace = () => ({
    'for (const __a of __b) __c': ({__a, __b, __c}, path) => {
        // remove node
        return '';
    },
});

// for (a of b) {}; alert(); -> alert();

Update node:

module.exports.report = () => 'any message here';

module.exports.replace = () => ({
    'for (const __a of __array) __c': ({__a, __array, __c}, path) => {
        // update __array elements
        path.node.right.elements = [];
        return path;
    },
});

// for (const a of [1, 2, 3]) {}; -> for (const a of []) {};

Update node using template variables:

module.exports.report = () => 'any message here';

module.exports.replace = () => ({
    'for (const __a of __array) __c': ({__a, __array, __c}, path) => {
        // update the whole node using template string
        return 'for (const x of y) z';
    },
});

// for (const item of array) {}; -> for (const x of y) z;

Includer

Includer is the most preferable format of a plugin, simplest to use (after Replacer):

module.exports.report = () => 'debugger statement should not be used';

module.exports.fix = (path) => {
    path.remove();
};

module.exports.include = () => ['debugger'];
// optional
module.exports.exclude = () => {};
// optional
module.exports.filter = (path) => {
    return true;
};

include and exclude returns an array of @babel/types, or code blocks:

const __ = 'hello';

Where __ can be any node. All this possible with help of @putout/compare. Templates format supported in traverse and find plugins as well.

Traverser

Traverser gives you more power to filter and fix nodes you need.

module.exports.report = () => 'debugger statement should not be used';

module.exports.fix = (path, {options}) => {
    path.remove();
};

module.exports.traverse = ({push}) => ({
    'debugger'(path) {
        push(path);
    },
});

Stores

Stores is preferred way of keeping 🐊Putout data, traverse init function called only once, and any other way of handling variables will most likely will lead to bugs. There is a couple store types:

Let's talk about each of them.

listStore

To save things as a list without duplicates use listStore. Let's suppose you have such code:

debugger;
const hello = '';
debugger;
const world = '';

Let's process it!

module.exports.traverse = ({listStore}) => ({
    'debugger'(path) {
        listStore(path);
    },
    
    Program: {
        exit() {
            console.log(listStore());
            // returns
            [{
                type: 'DebuggerStatement',
            }, {
                type: 'DebuggerStatement',
            }];
            
            // for code
            'debugger; debugger';
        },
    },
});
pathStore

When you want additional check that path not removed.

debugger;
const hello = '';

Let's process it!

module.exports.traverse = ({pathStore}) => ({
    'debugger'(path) {
        pathStore(path);
        path.remove();
    },
    
    Program: {
        exit() {
            console.log(pathStore());
            // returns
            [];
        },
    },
});
store

When you need key-value use store:

module.exports.traverse = ({push, store}) => ({
    'debugger'(path) {
        store('hello', 'world');
        push(path);
    },
    
    Program: {
        exit() {
            store();
            // returns
            ['world'];
            
            store.entries();
            // returns
            [['hello', 'world']];
            
            store('hello');
            // returns
            'world';
        },
    },
});
upstore

When you need to update already saved values, use upstore:

module.exports.traverse = ({push, upstore}) => ({
    TSTypeAliasDeclaration(path) {
        if (path.parentPath.isExportNamedDeclaration())
            return;
        
        upstore(path.node.id.name, {
            path,
        });
    },
    
    ObjectProperty(path) {
        const {value} = path.node;
        const {name} = value;
        
        store(name, {
            used: true,
        });
    },
    
    Program: {
        exit() {
            for (const {path, used} of upstore()) {
                if (used)
                    continue;
                
                push(path);
            }
        },
    },
});
uplist

When you need to update named arrays:

module.exports.traverse = ({uplist, push}) => ({
    'const __object = __a.__b': (fullPath) => {
        const {__a, __b} = getTemplateValues(fullPath, 'const __object = __a.__b');
        const initPath = fullPath.get('declarations.0.init');
        const {uid} = initPath.scope;
        
        if (isIdentifier(__a) || isCallExpression(__a)) {
            const {code} = generate(__a);
            const id = `${uid}-${code}`;
            
            return uplist(id, initPath);
        }
    },
    'Program': {
        exit: () => {
            for (const items of uplist()) {
                if (items.length < 2)
                    continue;
                
                const index = items.length - 1;
                const path = items[index];
                
                push({
                    path,
                    items,
                });
            }
        },
    },
});

Scanner

Scanner gives you all ability to work with files.

module.exports.report = () => 'Create file hello.txt';

module.exports.fix = (rootPath) => {
    createFile(rootPath, 'hello.txt', 'hello world');
};

module.exports.scan = (rootPath, {push}) => {
    const [filePath] = findFile(rootPath, 'hello.txt');
    
    if (filePath)
        return null;
    
    push(rootPath);
};

You can also subscribe to progress events:

  • start;
  • end;
  • push;

And one more: file if your plugin uses progress function:

export const report = () => 'Create file hello.txt';

export const fix = (rootPath) => {
    createFile(rootPath, 'hello.txt', 'hello world');
};

export const scan = (rootPath, {progress}) => {
    const [filePath] = findFile(rootPath, 'hello.txt');
    
    if (filePath)
        return null;
    
    progress({
        i: 0,
        n: 1,
    });
    
    push(filePath);
};

Or better trackFile, which does the same, but also count progress:

export const report = () => 'Add file';
export const fix = (file) => {
    writeFileContent(file, 'hello');
};

export const scan = (rootPath, {push, trackFile}) => {
    for (const file of trackFile(rootPath, 'hello.txt')) {
        push(file);
    }
};

Finder

Finder gives you all the control over traversing, but it's the slowest format. Because traversers not merged in contrast with other plugin formats.

module.exports.report = () => 'debugger statement should not be used';

module.exports.fix = (path) => {
    path.remove();
};

module.exports.find = (ast, {push, traverse}) => {
    traverse(ast, {
        'debugger'(path) {
            push(path);
        },
    });
};

Example

const {runPlugins} = require('@putout/engine-runner');
const {parse} = require('@putout/engine-parser');

const plugins = [{
    rule: 'remove-debugger',
    msg: '', // optional
    options: {}, // optional
    plugin: {
        include: () => ['debugger'],
        fix: (path) => path.remove(),
        report: () => `debugger should not be used`,
    },
}];

const ast = parse('const m = "hi"; debugger');

const places = runPlugins({
    ast,
    shebang: false, // default
    fix: false, // default
    fixCount: 0, // default
    plugins,
    parser: 'babel', // default
});

Logs

To see logs, use:

  • βœ… DEBUG=putout:runner:*
  • βœ… DEBUG=putout:runner:fix
  • βœ… DEBUG=putout:runner:find
  • βœ… DEBUG=putout:runner:template
  • βœ… DEBUG=putout:runner:replace

License

MIT

Current Tags

  • 21.1.0                                ...           latest (5 months ago)

159 Versions

  • 21.1.0                                ...           5 months ago
  • 21.0.3                                ...           7 months ago
  • 21.0.2                                ...           7 months ago
  • 21.0.1                                ...           9 months ago
  • 21.0.0                                ...           9 months ago
  • 20.4.1                                ...           10 months ago
  • 20.4.0                                ...           a year ago
  • 20.3.0                                ...           a year ago
  • 20.2.0                                ...           a year ago
  • 20.1.0                                ...           a year ago
  • 20.0.5                                ...           a year ago
  • 20.0.4                                ...           a year ago
  • 20.0.3                                ...           a year ago
  • 20.0.2                                ...           a year ago
  • 20.0.1                                ...           a year ago
  • 20.0.0                                ...           a year ago
  • 19.2.0                                ...           a year ago
  • 19.1.0                                ...           a year ago
  • 19.0.2                                ...           a year ago
  • 19.0.1                                ...           a year ago
  • 19.0.0                                ...           a year ago
  • 18.0.5                                ...           a year ago
  • 18.0.4                                ...           a year ago
  • 18.0.3                                ...           a year ago
  • 18.0.2                                ...           a year ago
  • 18.0.1                                ...           a year ago
  • 18.0.0                                ...           a year ago
  • 17.2.1                                ...           a year ago
  • 17.2.0                                ...           a year ago
  • 17.1.0                                ...           a year ago
  • 17.0.1                                ...           a year ago
  • 17.0.0                                ...           a year ago
  • 16.1.0                                ...           a year ago
  • 16.0.1                                ...           a year ago
  • 16.0.0                                ...           2 years ago
  • 15.1.1                                ...           2 years ago
  • 15.1.0                                ...           2 years ago
  • 15.0.1                                ...           2 years ago
  • 15.0.0                                ...           2 years ago
  • 14.1.1                                ...           2 years ago
  • 14.1.0                                ...           2 years ago
  • 14.0.2                                ...           2 years ago
  • 14.0.1                                ...           2 years ago
  • 14.0.0                                ...           2 years ago
  • 13.7.0                                ...           2 years ago
  • 13.6.0                                ...           2 years ago
  • 13.5.0                                ...           3 years ago
  • 13.4.0                                ...           3 years ago
  • 13.3.1                                ...           3 years ago
  • 13.3.0                                ...           3 years ago
  • 13.2.1                                ...           3 years ago
  • 13.2.0                                ...           3 years ago
  • 13.1.0                                ...           3 years ago
  • 13.0.1                                ...           3 years ago
  • 13.0.0                                ...           3 years ago
  • 12.6.0                                ...           3 years ago
  • 12.5.0                                ...           3 years ago
  • 12.4.1                                ...           3 years ago
  • 12.4.0                                ...           3 years ago
  • 12.3.0                                ...           3 years ago
  • 12.2.0                                ...           3 years ago
  • 12.1.1                                ...           3 years ago
  • 12.1.0                                ...           3 years ago
  • 12.0.0                                ...           3 years ago
  • 11.6.0                                ...           3 years ago
  • 11.5.0                                ...           3 years ago
  • 11.4.1                                ...           3 years ago
  • 11.4.0                                ...           3 years ago
  • 11.3.1                                ...           3 years ago
  • 11.3.0                                ...           3 years ago
  • 11.2.0                                ...           3 years ago
  • 11.1.0                                ...           3 years ago
  • 11.0.0                                ...           3 years ago
  • 10.2.1                                ...           3 years ago
  • 10.2.0                                ...           3 years ago
  • 10.1.0                                ...           3 years ago
  • 10.0.2                                ...           3 years ago
  • 10.0.1                                ...           4 years ago
  • 10.0.0                                ...           4 years ago
  • 9.3.2                                ...           4 years ago
  • 9.3.1                                ...           4 years ago
  • 9.3.0                                ...           4 years ago
  • 9.2.1                                ...           4 years ago
  • 9.2.0                                ...           4 years ago
  • 9.1.1                                ...           4 years ago
  • 9.1.0                                ...           4 years ago
  • 9.0.1                                ...           4 years ago
  • 9.0.0                                ...           4 years ago
  • 8.2.1                                ...           4 years ago
  • 8.2.0                                ...           4 years ago
  • 8.1.1                                ...           4 years ago
  • 8.1.0                                ...           4 years ago
  • 8.0.2                                ...           4 years ago
  • 8.0.1                                ...           4 years ago
  • 8.0.0                                ...           4 years ago
  • 7.0.0                                ...           4 years ago
  • 6.10.1                                ...           4 years ago
  • 6.10.0                                ...           4 years ago
  • 6.9.2                                ...           4 years ago
  • 6.9.1                                ...           4 years ago
  • 6.9.0                                ...           4 years ago
  • 6.8.0                                ...           4 years ago
  • 6.7.1                                ...           4 years ago
  • 6.7.0                                ...           4 years ago
  • 6.6.1                                ...           5 years ago
  • 6.6.0                                ...           5 years ago
  • 6.5.0                                ...           5 years ago
  • 6.4.0                                ...           5 years ago
  • 6.3.0                                ...           5 years ago
  • 6.2.0                                ...           5 years ago
  • 6.1.1                                ...           5 years ago
  • 6.1.0                                ...           5 years ago
  • 6.0.1                                ...           5 years ago
  • 6.0.0                                ...           5 years ago
  • 5.1.1                                ...           5 years ago
  • 5.1.0                                ...           5 years ago
  • 5.0.1                                ...           5 years ago
  • 5.0.0                                ...           5 years ago
  • 4.1.0                                ...           5 years ago
  • 4.0.2                                ...           5 years ago
  • 4.0.1                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 3.7.0                                ...           5 years ago
  • 3.6.0                                ...           5 years ago
  • 3.5.3                                ...           5 years ago
  • 3.5.2                                ...           5 years ago
  • 3.5.1                                ...           5 years ago
  • 3.5.0                                ...           5 years ago
  • 3.4.0                                ...           5 years ago
  • 3.3.3                                ...           5 years ago
  • 3.3.2                                ...           5 years ago
  • 3.3.1                                ...           5 years ago
  • 3.3.0                                ...           5 years ago
  • 3.2.0                                ...           5 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.10.1                                ...           5 years ago
  • 1.10.0                                ...           5 years ago
  • 1.9.1                                ...           5 years ago
  • 1.9.0                                ...           5 years ago
  • 1.8.0                                ...           5 years ago
  • 1.7.0                                ...           5 years ago
  • 1.6.2                                ...           5 years ago
  • 1.6.1                                ...           5 years ago
  • 1.6.0                                ...           5 years ago
  • 1.5.1                                ...           5 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.1                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.1                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (12)
Dev Dependencies (13)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE