@babel/types#Program TypeScript Examples

The following examples show how to use @babel/types#Program. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From tailchat with GNU General Public License v3.0 6 votes vote down vote up
export async function generateFunctionDeclare(options: Options) {
  const sourcecode = await fs.readFile(options.entryPath, 'utf8');

  const exported = getSourceCodeExportedFunction(sourcecode);

  const astList = exported.map((e) => {
    return buildNamedExport({
      name: e.name,
    });
  });

  const code = generate(program(_.flatten(astList))).code;

  return code;
}
Example #2
Source File: index.d.ts    From amazon-kinesis-video-streams-webrtc-sdk-js-with-amazon-cognito with MIT No Attribution 5 votes vote down vote up
program: TemplateBuilder<Program>
Example #3
Source File: babel-polyfill.ts    From nota with MIT License 5 votes vote down vote up
program = (body: Array<Statement>): Program => ({
  type: "Program",
  body,
  sourceType: "module",
  directives: [],
  sourceFile: "",
  ...baseNode,
})
Example #4
Source File: translate.ts    From nota with MIT License 5 votes vote down vote up
translateAst = (input: string, tree: Tree): Program => {
  let node = tree.topNode;
  assert(matches(node, terms.Document));
  let translator = new Translator(input);

  let docBody = translator.translateTextbody(node.getChild(terms.TextBody)!);
  let docProps = t.identifier("docProps");
  let doc = toReact(
    t.identifier("Document"),
    [t.spreadElement(docProps)],
    [t.spreadElement(docBody)]
  );

  let prelude: { [k: string]: string } = COMPONENTS;

  let usedPrelude: Set<string> = new Set();
  t.traverse(doc, node => {
    if (node.type == "Identifier" && node.name in prelude) {
      usedPrelude.add(node.name);
    }
  });

  let preludeImports: { [k: string]: string[] } = {};
  for (let k of usedPrelude) {
    let path = prelude[k];
    if (!(path in preludeImports)) {
      preludeImports[path] = [];
    }
    preludeImports[path].push(k);
  }

  let createElLong = t.identifier("createElement");
  let observer = t.identifier("observer");

  let program: Statement[] = [
    t.importDeclaration(
      [t.importSpecifier(createEl, createElLong), t.importSpecifier(fragment, fragment)],
      t.stringLiteral("react")
    ),
    t.importDeclaration([t.importSpecifier(observer, observer)], t.stringLiteral("mobx-react")),
    t.importDeclaration(
      Object.keys(preludeImports).map(mod =>
        t.importSpecifier(t.identifier(mod), t.identifier(mod))
      ),
      t.stringLiteral("@nota-lang/nota-components")
    ),
    ..._.toPairs(preludeImports).map(([mod, ks]) =>
      t.variableDeclaration("const", [
        t.variableDeclarator(
          t.objectPattern(ks.map(k => t.objectProperty(t.identifier(k), t.identifier(k), true))),
          t.identifier(mod)
        ),
      ])
    ),
    // ..._.toPairs(preludeImports).map(([path, ks]) =>
    //   t.importDeclaration(
    //     ks.map(k => t.importSpecifier(t.identifier(k), t.identifier(k))),
    //     t.stringLiteral(path)
    //   ),
    // ),
    ...Array.from(translator.imports),
    ...Array.from(translator.exports),
    t.exportDefaultDeclaration(
      t.callExpression(observer, [t.arrowFunctionExpression([docProps], doc)])
    ),
  ];

  return t.program(program);
}