pshrmn / ast

Generate code by creating AST nodes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@posh/ast

Generate JavaScript modules from AST nodes.

Uses Babel's @babel/types and @babel/generator packages.

Note: Not all node types are wrapped by @posh/ast. For the ones that are not, the types from @babel/types should be used.

npm install @posh/ast

Usage

import { stringify, types } from "@posh/ast";

function createModule() {
  const myVar = types.CONST("myVar", types.STRING("hi!"));
  const exportVar = types.EXPORT_DEFAULT(types.ID("myVar"));

  const code = stringify`
${myVar}
${exportVar}
`;
  /*
   * const myVar = "hi!";
   * export default myVar;
   */
}

function createComponent() {
  const importReact = types.IMPORT_DEFAULT("React", "react");
  const MyComponent = types.FUNCTION(
    "MyComponent",
    [types.ID("props")],
    [
      types.RETURN(types.STRING("test"))
    ]
  );
  const exportComponent = types.EXPORT_DEFAULT("MyComponent");
  const code = stringify`
${importReact}

${MyComponent}

${exportComponent}
`;
  /*
   * import React from "react"
   *
   * function MyComponent(props) {
   *   return "test";
   * }
   *
   * export default MyComponent;
   */
}

About

Generate code by creating AST nodes

License:MIT License


Languages

Language:TypeScript 97.6%Language:JavaScript 2.4%