How to use the @babel/template 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 parcel-bundler / parcel / packages / transformers / react-refresh-wrap / src / ReactRefreshWrapTransformer.js View on Github external
// @flow

import semver from 'semver';
import path from 'path';
import {Transformer} from '@parcel/plugin';
import {relativeUrl} from '@parcel/utils';
import SourceMap from '@parcel/source-map';
import generate from '@babel/generator';
import {parse} from '@babel/parser';
import template from '@babel/template';
import * as t from '@babel/types';

const WRAPPER = path.join(__dirname, 'helpers', 'helpers.js');

const wrapper = template(`
var helpers = require(%%helper%%);
var prevRefreshReg = window.$RefreshReg$;
var prevRefreshSig = window.$RefreshSig$;
helpers.prelude(module);

try {
  %%module%%
  helpers.postlude(module);
} finally {
  window.$RefreshReg$ = prevRefreshReg;
  window.$RefreshSig$ = prevRefreshSig;
}
`);

function shouldExclude(asset, options) {
  return (
github ttag-org / babel-plugin-ttag / tests / unit / test_gettext_extractor.js View on Github external
it('should throw if has invalid argument', () => {
        const node = template('gettext(fn())')().expression;
        const fn = () => gettext.validate(node, enConfig);
        expect(fn).to.throw('You can not use CallExpression \'fn()\' as an argument to gettext');
    });
github ttag-org / babel-plugin-ttag / tests / unit / test_gettext_extractor.js View on Github external
it('should match gettext with zero arguments', () => {
        const node = template('gettext()')().expression;
        const result = gettext.match(node, enConfig);
        expect(result).to.be.false;
    });
});
github ttag-org / babel-plugin-ttag / tests / unit / test_utils.js View on Github external
it('should extract msgid with expressions', () => {
        const node = template('nt(n)`${n} banana ${ b }`')().expression;
        const expected = '${ n } banana ${ b }';
        expect(template2Msgid(node, testContext)).to.eql(expected);
    });
github ttag-org / babel-plugin-ttag / tests / unit / test_tag_gettext_extractor.js View on Github external
it('should throw if has invalid expressions', () => {
        const node = template('t`banana ${ n + 1}`')().expression;
        const fn = () => gettext.validate(node, enConfig);
        expect(fn).to.throw('You can not use BinaryExpression \'${n + 1}\' in localized strings');
    });
github ttag-org / babel-plugin-ttag / tests / unit / test_utils.js View on Github external
it('should extract msgid with expressions', () => {
        const node = template('`test ${ a } ${ b }`')().expression;
        const [strs, exprs] = getStrsExprs(node);
        expect(getMsgidNumbered(strs, exprs)).to.be.eql('test ${ 0 } ${ 1 }');
    });
    it('should extract msgid without expressions', () => {
github spockjs / spockjs / packages / assertion-post-processor-regular-errors / src / index.ts View on Github external
import { addNamed } from '@babel/helper-module-imports';
import template from '@babel/template';
import { NodePath } from '@babel/traverse';
import { ExpressionStatement, TryStatement } from '@babel/types';

import { AssertionPostProcessor } from '@spockjs/config';

import autoImportDisabled from './auto-import-disabled-warning';

const createTryStatement = template(`
  try {
    ASSERTION
  } catch(e) {
    if (e instanceof ASSERTION_ERROR) {
      throw new Error(e.message);
    }
    throw e;
  }
`);

const processor: AssertionPostProcessor = (t, { autoImport }) => (
  originalPath,
  patterns,
) => {
  const assertionErrorImportedName = 'AssertionError';
  const assertionErrorLocal = autoImport
github asapach / babel-plugin-rewire-exports / src / index.js View on Github external
export default function ({types: t}) {
  const defaultIdentifier = t.identifier('default');
  const rewireIdentifier = t.identifier('rewire');
  const restoreIdentifier = t.identifier('restore');
  const stubIdentifier = t.identifier('$stub');
  const VISITED = Symbol('visited');

  const buildStub = template(`
    export function REWIRE(STUB) {
      LOCAL = STUB;
    }
  `, {sourceType: 'module'});

  const buildRestore = template(`
    export function RESTORE() {
      BODY
    }
  `, {sourceType: 'module'});

  function buildNamedExport(local, exported) {
    return markVisited(t.exportNamedDeclaration(null, [
      t.exportSpecifier(t.identifier(local.name), t.identifier(exported.name))
    ]));
  }
github lnlfps / symph-joy / build / babel / plugins / joy-controller-react-hot-loader-label-plugin.js View on Github external
import { PluginObj } from '@babel/core'
import template from '@babel/template'
import { NodePath } from '@babel/traverse'
import * as BabelTypes from '@babel/types'

const requireRHL = template(
  'require(\'react-hot-loader/root\').hot'
)()

export default function ({ types: t }: { types: typeof BabelTypes }): PluginObj {
  return {
    pre: (file) => {
      let importController
      let controller
      file.path.traverse({
        ImportDeclaration (importPath) {
          if (importPath.node.source.value === '@symph/joy/controller') {
            importController = importPath
            controller = importPath.get('specifiers').find(item => item.get('type').node === 'ImportDefaultSpecifier' || item.get('imported.name').node === 'controller')
          }
        }
      })
github ttag-org / babel-plugin-ttag / src / extractors / ngettext.js View on Github external
function ngettextTemplate(ngettext, pluralForm) {
    return tpl(`function NGETTEXT(n, args) { ${pluralFnBody(pluralForm)} }`)({ NGETTEXT: ngettext });
}