How to use the putout.template.ast function in putout

To help you get started, we’ve selected a few putout examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github coderaiser / putout / packages / plugin-putout / lib / convert-replace-with-multiple / index.js View on Github external
const {
    insertAfter,
    replaceWith,
} = operate;

const {
    Identifier,
    ObjectProperty,
} = types;

module.exports.report = () => {
    return `"operate.replaceWithMultiple" should be called instead of "path.replaceWithMultiple"`;
};

const replaceWithAST = template.ast(`
    const {replaceWithMultiple} = require('putout').operate;
`);

module.exports.fix = ({path, calleePath, property, object, program, isInserted}) => {
    const strictModePath = program.get('body.0');
    const {bindings} = strictModePath.scope;
    
    replaceWith(calleePath, property);
    path.node.arguments.unshift(object);
    
    if (bindings.replaceWithMultiple)
        return;
    
    if (isInserted())
        return;
github coderaiser / putout / packages / plugin-putout / lib / convert-replace-with / index.js View on Github external
replaceWith,
    insertAfter,
} = operate;

const {
    Identifier,
    ObjectProperty,
} = types;

const fullstore = require('fullstore');

module.exports.report = () => {
    return `"operate.replaceWith" should be called instead of "path.replaceWith"`;
};

const replaceWithAST = template.ast(`
    const {replaceWith} = require('putout').operate;
`);

module.exports.fix = ({path, calleePath, property, object, program, isInserted}) => {
    replaceWith(calleePath, property);
    const strictModePath = program.get('body.0');
    const {bindings} = strictModePath.scope;
    
    path.node.arguments.unshift(object);
    
    if (bindings.replaceWith || isInserted())
        return;
    
    if (!bindings.replaceWithMultiple && !bindings.insertAfter && !isInserted()) {
        isInserted(true);
        insertAfter(strictModePath, replaceWithAST);
github coderaiser / putout / packages / plugin-madrun / lib / add-fix-lint / index.js View on Github external
types,
    operate,
    template,
} = require('putout');

const {replaceWithMultiple} = operate;

const {
    isIdentifier,
    isLiteral,
    isStringLiteral,
    ObjectProperty,
    StringLiteral,
} = types;

const fixLintScript = template.ast(`
    () => run('lint', '--fix')
`);

module.exports.report = () => `fix:lint should exist`;

module.exports.fix = (path) => {
    replaceWithMultiple(path, [
        path.node,
        ObjectProperty(StringLiteral('fix:lint'), fixLintScript),
    ]);
};

module.exports.traverse = ({push}) => {
    return {
        'module.exports = __object'(path) {
            const propertiesPaths = path.get('right.properties');
github coderaiser / putout / packages / plugin-putout / lib / convert-babel-types / index.js View on Github external
'use strict';

const {
    operate,
    template,
} = require('putout');

const {replaceWith} = operate;

const astRequire = template.ast(`
    require('putout').types
`);

module.exports.report = () => {
    return `"putout.types" should be used instead of "@babel/types"`;
};

function isRequire(path) {
    return path
        .get('callee')
        .isIdentifier({name: 'require'});
}

function isBabelTypes(path) {
    return path
        .get('arguments.0')
github coderaiser / putout / packages / plugin-madrun / lib / add-run / index.js View on Github external
'use strict';

const {
    operate,
    template,
    types,
} = require('putout');

const {isProgram} = types;
const {findBinding} = operate;

const node = template.ast(`const {run} = require('madrun')`);

module.exports.report = () => 'run should be declared';

module.exports.fix = (path) => {
    path.node.body.unshift(node);
};

module.exports.traverse = ({push}) => {
    let added = false;
    
    return {
        'run(__args)'(path) {
            if (added)
                return;
            
            if (findBinding(path, 'run'))