Skip to content

JavaScript And TypeScript Code Generator

License

Notifications You must be signed in to change notification settings

narumincho/js-ts-code-generator

Repository files navigation

@narumincho/js-ts-code-generator

https://jsr.io/@narumincho/js-ts-code-generator

コンセプト

などで TypeScript, JavaScript のコードを生成したかったので作った

  • 入力値は, 構造化されているので TypeScript の AST(抽象構文木)とは違う
  • 出力した形式は人間にも読みやすい
  • Deno でもブラウザでも動く
  • 予約語はあまり気にしなくて良い
  • 対応している構文は一部だけ

Deno, ブラウザ, 両方対応

sample code サンプルコード

import {
  callMethod,
  type data,
  exportDefinitionFunction,
  generateCodeAsString,
  get,
  identifierFromString,
  logicalAnd,
  notEqual,
  statementEvaluateExpr,
  statementIf,
  statementVariableDefinition,
  stringLiteral,
  typeUnion,
  variable,
} from "./mod.ts";

const serverCode: data.JsTsCode = {
  exportDefinitionList: [
    exportDefinitionFunction({
      isAsync: false,
      name: identifierFromString("middleware"),
      document: "ミドルウェア",
      typeParameterList: [],
      parameterList: [
        {
          name: identifierFromString("request"),
          document: "リクエスト",
          type: ({
            _: "ImportedType",
            importedType: {
              moduleName: "express",
              nameAndArguments: {
                name: identifierFromString("Request"),
                arguments: [],
              },
            },
          }),
        },
        {
          name: identifierFromString("response"),
          document: "レスポンス",
          type: ({
            _: "ImportedType",
            importedType: {
              moduleName: "express",
              nameAndArguments: {
                name: identifierFromString("Response"),
                arguments: [],
              },
            },
          }),
        },
      ],
      returnType: { _: "Void" },
      statementList: [
        statementVariableDefinition({
          isConst: true,
          name: identifierFromString("accept"),
          type: typeUnion([{ _: "String" }, { _: "Undefined" }]),
          expr: get(
            get(
              variable(identifierFromString("request")),
              "headers",
            ),
            "accept",
          ),
        }),
        statementIf({
          condition: logicalAnd(
            notEqual(
              variable(identifierFromString("accept")),
              { _: "UndefinedLiteral" },
            ),
            callMethod(
              variable(identifierFromString("accept")),
              "includes",
              [stringLiteral("text/html")],
            ),
          ),
          thenStatementList: [
            statementEvaluateExpr(
              callMethod(
                variable(identifierFromString("response")),
                "setHeader",
                [
                  stringLiteral("content-type"),
                  stringLiteral("text/html"),
                ],
              ),
            ),
          ],
        }),
      ],
    }),
  ],
  statementList: [],
};
const codeAsString = generateCodeAsString(serverCode, "TypeScript");
console.log(codeAsString);

出力 output

/* generated by https://jsr.io/@narumincho/js-ts-code-generator. Do not edit! */

import * as a from "express";

/**
 * ミドルウェア
 * @param request リクエスト
 * @param response レスポンス
 */
export const middleware = (request: a.Request, response: a.Response): void => {
  const accept: string | undefined = request.headers.accept;
  if (accept !== undefined && accept.includes("text/html")) {
    response.setHeader("content-type", "text/html");
  }
};

About

JavaScript And TypeScript Code Generator

Resources

License

Stars

Watchers

Forks

Packages

No packages published