How to use the @babel/template.program 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 ark120202 / babel-lua / packages / babel-lua-helpers / src / helpers.js View on Github external
/* eslint max-len: "off" */

import template from '@babel/template';

const helpers = {};
export default helpers;

// Helpers never include placeholders, so we disable placeholder pattern
// matching to allow us to use pattern-like variable names.
const defineHelper = template.program({ placeholderPattern: false });

helpers.iif = defineHelper(`
  export default function iif(condition, t, f) {
    if (condition) {
      return type(t) == 'function' && t() || t
    } else {
      return type(f) == 'function' && f() || f
    }
  }
`);

helpers.typeof = defineHelper(`
  export default function _typeof(obj) {
    const ot = type(obj)
    return ot === "table" ? "object" : ot;
  }