How to use the @babel/template.statement function in @babel/template

To help you get started, we’ve selected a few @babel/template 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 ecomfe / zrender / build / babel-plugin-transform-modules-commonjs-ec.js View on Github external
function buildExport({exportName, namespace, propName, localName}) {
    const exportDefault = exportName === 'default';

    const head = exportDefault ? 'module.exports' : `exports.${exportName}`;

    let opt = {};
    // FIXME
    // Does `PRIORITY`, `LOCATION_PARAMS` recognised as babel-template placeholder?
    // We have to do this for workaround temporarily.
    if (/^[A-Z0-9_]+$/.test(localName)) {
        opt[localName] = localName;
    }

    return babelTemplate.statement(
        localName
            ? `${head} = ${localName};`
            : `${head} = ${namespace}.${propName};`
    )(opt);
}
github ecomfe / zrender / build / babel-plugin-transform-modules-commonjs-ec.js View on Github external
types.identifier(localImportName),
                loadExpr
            )
        ]);

        reqHeader.loc = metadata.loc;
        headers.push(reqHeader);

        if (!localImportDefaultName) {
            // src:
            //      import {someInZrUtil1 as someInZrUtil1Alias, zz} from 'zrender/core/util';
            // metadata.imports:
            //      Map { 'someInZrUtil1Alias' => 'someInZrUtil1', 'zz' => 'zz' }
            for (const importEntry of metadata.imports) {
                headers.push(
                    babelTemplate.statement(`var IMPORTNAME = NAMESPACE.PROPNAME;`)({
                        NAMESPACE: localImportName,
                        IMPORTNAME: importEntry[0],
                        PROPNAME: importEntry[1]
                    })
                );
            }
        }
    }

    return headers;
}
github ecomfe / zrender / build / babel-plugin-transform-modules-commonjs-ec.js View on Github external
function buildNamespaceReexport(meta, namespace, checkExport) {
    checkExport();
    return babelTemplate.statement(`
        (function() {
          for (var key in NAMESPACE) {
            if (NAMESPACE == null || !NAMESPACE.hasOwnProperty(key) || key === 'default' || key === '__esModule') return;
            VERIFY_NAME_LIST;
            exports[key] = NAMESPACE[key];
          }
        })();
    `)({
        NAMESPACE: namespace,
        VERIFY_NAME_LIST: meta.exportNameListName
            ? babelTemplate.statement(`
                if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
            `)({EXPORTS_LIST: meta.exportNameListName})
            : null
    });
}
github ecomfe / zrender / build / babel-plugin-transform-modules-commonjs-ec.js View on Github external
function buildNamespaceReexport(meta, namespace, checkExport) {
    checkExport();
    return babelTemplate.statement(`
        (function() {
          for (var key in NAMESPACE) {
            if (NAMESPACE == null || !NAMESPACE.hasOwnProperty(key) || key === 'default' || key === '__esModule') return;
            VERIFY_NAME_LIST;
            exports[key] = NAMESPACE[key];
          }
        })();
    `)({
        NAMESPACE: namespace,
        VERIFY_NAME_LIST: meta.exportNameListName
            ? babelTemplate.statement(`
                if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
            `)({EXPORTS_LIST: meta.exportNameListName})
            : null
    });
}
github FujitsuLaboratories / escapin / src / util.ts View on Github external
export function statement(tpl: string, vars: { [x: string]: OneOrMore }): t.Statement {
  return template.statement(tpl, {
    placeholderPattern: PLACEHOLDER_PATTERN,
  })(vars);
}