How to use the @babel/template.default.ast 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 Wscats / omil / tests / ast.js View on Github external
// import generate from "@babel/generator";
// console.log(template,t)
// const buildRequire = template(`
//   var %%importName%% = require(%%source%%);
// `);
// const ast = buildRequire({
//   importName: t.identifier("myModule"),
//   source: t.stringLiteral("my-module"),
// });

// https://astexplorer.net/


const source = "my-module";

const ast = template.ast(`
// JS
var a = 'abcd'
`);
ast.kind = 'let'

console.log(ast);
console.log(generate(ast).code);
github kristerkari / babel-plugin-react-native-platform-specific-extensions / index.js View on Github external
function astTernary(platform, valueTrue, valueFalse) {
          // Omit the var assignment when specifier is empty (global import case, executing for the side-effects only).
          const assignee = specifier ? `var ${specifier.local.name} = ` : "";
          const platformStr = `import { Platform } from "react-native";`;
          const platformImport = !isPlatformImportInserted ? platformStr : "";

          if (platformImport) {
            isPlatformImportInserted = true;
          }

          return babelTemplate.ast(
            platformImport +
              assignee +
              `Platform.OS === "${platform}" ? require("${valueTrue}") : require("${valueFalse}");`
          );
        }
github mdx-js / mdx / packages / babel-plugin-apply-mdx-type-props / index.js View on Github external
const buildShortcodeFunction = () =>
  template.ast(
    `
  const mdxMakeShortcode = name => props => {
    console.warn("Component " + name + " was not imported, exported, or provided by MDXProvider as global scope")
    return <div>
  }
`,
    {
      plugins: ['jsx']
    }
  )
</div>
github coderaiser / putout / packages / engine-runner / lib / template / compare.js View on Github external
module.exports.generate = memo((value) => {
    const result = template.ast(value, {
        allowAwaitOutsideFunction: true,
    });
    
    return result.expression || result;
});
github instructure / instructure-ui / packages / babel-plugin-themeable-styles / lib / index.js View on Github external
function generateVariableDeclaration (name, tokens, css, componentId) {
    return template.ast(`const ${name} = ${transformCSSRequire(tokens, css, componentId)}`)
  }
github coderaiser / putout / packages / engine-parser / lib / template.js View on Github external
module.exports.ast = memo((value, options) => {
    const result = template.ast(value, {
        ...defaults,
        ...options,
    });
    
    return result.expression || result;
});