How to use the putout.template 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-convert-esm-to-commonjs / lib / fix / import.js View on Github external
isImportNamespaceSpecifier,
} = types;

const getName = ({source}) => source.raw;

const convert = template(`
    const DECLARATION = require(NAME);
`);

const convertDestructureRename = template(`
    const {
          IMPORTED: LOCAL,
    } = require(NAME);
`);

const convertDestructure = template(`
    const {
          IMPORTED,
    } = require(NAME);
`);

const convertEmpty = template(`
    require(%%name%%);
`);

module.exports.convertImport = (path) => {
    const {node} = path;
    const {specifiers} = node;
    
    const name = getName(node);
    const vars = [];
github coderaiser / putout / packages / plugin-convert-spread-to-array-from / lib / convert-spread-to-array-from.js View on Github external
'use strict';

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

const buildArrayFrom = template(`
  Array.from(%%argument%%);
`);

module.exports.report = () => `Array.from should be used instead of array spread`;

module.exports.fix = (path) => {
    const [element] = path.node.elements;
    const {argument} = element;
    
    const ast = buildArrayFrom({
        argument,
    });
    
    replaceWith(path, ast);
};
github coderaiser / putout / packages / plugin-react-hooks / lib / convert-state-to-hooks / set-state-to-hooks.js View on Github external
'use strict';

const {replaceWithMultiple} = require('putout').operate;

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

const buildHooks = template(`
    SETTER(VALUE);
`);

module.exports = (path) => {
    const {properties} = path.node.arguments[0];
    const nodes = [];
    
    for (const {key, value} of properties) {
        const {name} = key;
        const SETTER = getSetter(name);
        const VALUE = String(value.value);
        
        nodes.push(buildHooks({
            SETTER,
            VALUE,
        }));
github coderaiser / putout / packages / plugin-convert-esm-to-commonjs / lib / fix / export.js View on Github external
module.exports.convertDefaultExport = (path) => {
    const {declaration} = path.node;
    const convert = template(`
        module.exports = DECLARATION;
    `);
    
    const DECLARATION = getDeclaration(declaration);
    
    replaceWith(path, convert({
        DECLARATION,
    }));
};
github coderaiser / putout / packages / plugin-react-hooks / lib / convert-state-to-hooks / state-to-hooks.js View on Github external
'use strict';

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

const buildHooks = template(`
    const [NAME, SETTER] = useState(VALUE);
`);

module.exports = (path) => {
    const {right} = path.node;
    const nodes = [];
    
    for (const {key, value} of right.properties) {
        const {name} = key;
        const NAME = name;
        const SETTER = getSetter(name);
        const VALUE = String(value.value);
        
        nodes.push(buildHooks({
            NAME,
            SETTER,
github coderaiser / putout / packages / plugin-convert-esm-to-commonjs / lib / fix / import.js View on Github external
replaceWithMultiple,
} = operate;

const {
    isImportSpecifier,
    isImportDefaultSpecifier,
    isImportNamespaceSpecifier,
} = types;

const getName = ({source}) => source.raw;

const convert = template(`
    const DECLARATION = require(NAME);
`);

const convertDestructureRename = template(`
    const {
          IMPORTED: LOCAL,
    } = require(NAME);
`);

const convertDestructure = template(`
    const {
          IMPORTED,
    } = require(NAME);
`);

const convertEmpty = template(`
    require(%%name%%);
`);

module.exports.convertImport = (path) => {
github coderaiser / putout / packages / plugin-convert-esm-to-commonjs / lib / fix / import.js View on Github external
const DECLARATION = require(NAME);
`);

const convertDestructureRename = template(`
    const {
          IMPORTED: LOCAL,
    } = require(NAME);
`);

const convertDestructure = template(`
    const {
          IMPORTED,
    } = require(NAME);
`);

const convertEmpty = template(`
    require(%%name%%);
`);

module.exports.convertImport = (path) => {
    const {node} = path;
    const {specifiers} = node;
    
    const name = getName(node);
    const vars = [];
    
    if (!specifiers.length)
        return replaceWith(path, convertEmpty({name}));
    
    for (const spec of specifiers) {
        if (isImportDefaultSpecifier(spec)) {
            vars.push(getImportDefaultVar(name, spec));
github coderaiser / putout / packages / plugin-convert-for-each-to-for-of / lib / convert-for-each-to-for-of.js View on Github external
'use strict';

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

const {
    replaceWith,
    replaceWithMultiple,
} = operate;

const forOfTemplate = template(`
  for (const %%item%% of %%items%%)
    %%body%%
`);

const {ContinueStatement} = types;

const {keys} = Object;

const isRoot = (path) => path.isFunction() || path.isProgram();

module.exports.report = () => `for-of should be used instead of forEach`;

module.exports.fix = (path) => {
    const {params, body} = path.node.arguments[0];
    const item = getItem(params);
    delete item.typeAnnotation;
github coderaiser / putout / packages / plugin-extract-object-properties / lib / equal-deep / index.js View on Github external
'use strict';

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

const {replaceWith} = operate;
const {isIdentifier} = types;

module.exports.report = () => `Object properties should be extracted into variables`;

const buildAst = template(`
    const {PROPERTY} = OBJECT;
`);

module.exports.fix = ({items}) => {
    const names = [];
    
    for (const item of items) {
        const {object, property} = item.node;
        
        if (isIdentifier(property) && !names.includes(property.name)) {
            names.push(property.name);
            
            const {
                body = item.scope.block.body,
            } = item.scope.block.body;
            body.unshift(buildAst({
github coderaiser / putout / packages / plugin-convert-esm-to-commonjs / lib / fix / import.js View on Github external
} = require('putout');

const {
    replaceWith,
    replaceWithMultiple,
} = operate;

const {
    isImportSpecifier,
    isImportDefaultSpecifier,
    isImportNamespaceSpecifier,
} = types;

const getName = ({source}) => source.raw;

const convert = template(`
    const DECLARATION = require(NAME);
`);

const convertDestructureRename = template(`
    const {
          IMPORTED: LOCAL,
    } = require(NAME);
`);

const convertDestructure = template(`
    const {
          IMPORTED,
    } = require(NAME);
`);

const convertEmpty = template(`