How to use the template function from @babel/core

Find comprehensive JavaScript @babel/core.template code examples handpicked from public code repositorys.

64
65
66
67
68
69
70
71
72
73
}

// After minification, these requires are going to be a bit smaller and denser than RN require

if (state.get('uses_rn_view')) {
  const makeZacsElement = template(`const ZACS_RN_View = require('@nozbe/zacs/src/ZACS_RN_View')`)
  path.get('body')[0].insertBefore(makeZacsElement())
}

if (state.get('uses_rn_text')) {
fork icon7
star icon425
watch icon16

+ 23 other calls in file

5
6
7
8
9
10
11
12
13
14
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _helperHoistVariables = require("@babel/helper-hoist-variables");
var _core = require("@babel/core");
var _helperModuleTransforms = require("@babel/helper-module-transforms");
var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
const buildTemplate = _core.template.statement(`
  SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
    "use strict";
    BEFORE_BODY;
    return {
fork icon3
star icon26
watch icon2

68
69
70
71
72
73
74
75
76
77
  imports.local = imports.local.concat(ret.local)
  path.remove()
}else if (p.extname(localPath) == '.json'){
  const text = fs.readFileSync(localPath, 'utf-8')
  const name = value.split('/').slice(-1)[0].split('.')[0] + '__json'
  const constNameTemplate = babel.template('const %%define%% = %%name%%'.replace('%%name%%', name).replace('%%define%%', names.join(',')))
  cache.add(localPath, `var ${name} = ` + text, 'utf-8')
  path.replaceWith(constNameTemplate())
}else {
  cache.add(localPath, fs.readFileSync(localPath, 'utf-8'))
fork icon3
star icon0
watch icon1

+ 23 other calls in file

79
80
81
82
83
84
85
86
87
    node,
    id,
    scope
  }) || node));
} else if (_core.types.isClassProperty(node) && node.value) {
  properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
} else {
  properties.push(prop("value", scope.buildUndefinedNode()));
}
fork icon0
star icon2
watch icon1

+ 2 other calls in file

27
28
29
30
31
32
33
34
35
36
  file.path.unshiftContainer("body", [fn]);
  file.scope.registerDeclaration(file.path.get("body.0"));
  return _core.types.cloneNode(id);
}


const helper = _core.template.statement`
  function CREATE_SUPER(Derived) {
    function isNativeReflectConstruct() {
      if (typeof Reflect === "undefined" || !Reflect.construct) return false;
fork icon0
star icon1
watch icon1

+ 2 other calls in file

566
567
568
569
570
571
572
573
574
575
    element.remove();
  }
});

if (statics.length > 0 || staticBlocks.length > 0) {
  const staticsClass = _core.template.expression.ast`
    class extends ${state.addHelper("identity")} {}
  `;
  staticsClass.body.body = [_core.types.staticBlock([_core.types.toStatement(path.node, false)]), ...statics];
  const constructorBody = [];
fork icon0
star icon1
watch icon1

70
71
72
73
74
75
76
77
78
79
});

console.log(returnState)
if (returnState.length != 0) {
    returnState = 'return [' + returnState + ']'
    const CONSOLE_AST = babel.template.ast(returnState);
    babel.traverse(ast, {
        enter(path) {
            if (path.node.type === "FunctionDeclaration" && path.node.id.name === "fuzzopt") {
                const blockStatementBody = path.node.body.body;
fork icon0
star icon1
watch icon1

+ 5 other calls in file

670
671
672
673
674
675
676
677
678
679
};
const globalTemplateAlteredFunction = template(`
        var Function = (function(){}).constructor;
        var global = (new Function(GLOBAL_COVERAGE_SCOPE))();
`);
const globalTemplateFunction = template(`
        var global = (new Function(GLOBAL_COVERAGE_SCOPE))();
`);
const globalTemplateVariable = template(`
        var global = GLOBAL_COVERAGE_SCOPE;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

368
369
370
371
372
373
374
375
376
377
  object
} = member.node;
const {
  name
} = member.node.property.id;
return _core.template.expression`BASE(REF, PROP)[PROP]`({
  BASE: file.addHelper("classPrivateFieldLooseBase"),
  REF: _core.types.cloneNode(object),
  PROP: _core.types.cloneNode(privateNamesMap.get(name).id)
});
fork icon0
star icon0
watch icon1

+ 55 other calls in file

44
45
46
47
48
49
50
51
52
53

if (typeof mjsStrictNamespace !== "boolean") {
  throw new Error(`.mjsStrictNamespace must be a boolean, or undefined`);
}

const getAssertion = localName => _core.template.expression.ast`
  (function(){
    throw new Error(
      "The CommonJS '" + "${localName}" + "' variable is not available in ES6 modules." +
      "Consider setting setting sourceType:script or sourceType:unambiguous in your " +
fork icon0
star icon0
watch icon1

+ 23 other calls in file

50
51
52
53
54
55
56
57
58
59
60
      ID = INDEX.value;
    }
  }
`);


const buildForOf = _core.template.statements(`
  var ITERATOR_COMPLETION = true;
  var ITERATOR_HAD_ERROR_KEY = false;
  var ITERATOR_ERROR_KEY = undefined;
  try {
fork icon0
star icon0
watch icon1

70
71
72
73
74
75
76
77
78
79

if (!isStringsRawEqual) {
  callExpressionInput.push(_core.types.arrayExpression(raws));
}

const lazyLoad = _core.template.ast`
  function ${templateObject}() {
    const data = ${_core.types.callExpression(helperId, callExpressionInput)};
    ${templateObject} = function() { return data };
    return data;
fork icon0
star icon0
watch icon1

87
88
89
90
91
92
93
94
95
96

if (hasConstructor) return;
let params, body;

if (classState.isDerived) {
  const constructor = _core.template.expression.ast`
    (function () {
      super(...arguments);
    })
  `;
fork icon0
star icon0
watch icon1

+ 3 other calls in file