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

JSR

コンセプト

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

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

Deno, ブラウザ, 両方対応

sample code サンプルコード

import {
  exportDefinitionFunction,
  generateCodeAsString,
  identifierFromString,
  type Module,
} from "jsr:@narumincho/js-ts-code-generator";
import * as statement from "jsr:@narumincho/js-ts-code-generator/statement";
import * as type from "jsr:@narumincho/js-ts-code-generator/type";
import * as expr from "jsr:@narumincho/js-ts-code-generator/expr";

const serverModule: Module = {
  definitionList: [
    exportDefinitionFunction({
      export: true,
      isAsync: false,
      name: identifierFromString("middleware"),
      document: "ミドルウェア",
      typeParameterList: [],
      parameterList: [
        {
          name: identifierFromString("request"),
          document: "リクエスト",
          type: ({
            type: "ImportedType",
            importedType: {
              moduleName: "express",
              nameAndArguments: {
                name: identifierFromString("Request"),
                arguments: [],
              },
            },
          }),
        },
        {
          name: identifierFromString("response"),
          document: "レスポンス",
          type: ({
            type: "ImportedType",
            importedType: {
              moduleName: "express",
              nameAndArguments: {
                name: identifierFromString("Response"),
                arguments: [],
              },
            },
          }),
        },
      ],
      returnType: { type: "Void" },
      statementList: [
        statement.variableDefinition({
          isConst: true,
          name: identifierFromString("accept"),
          type: type.union([{ type: "String" }, { type: "Undefined" }]),
          expr: expr.get(
            expr.get(
              expr.variable(identifierFromString("request")),
              "headers",
            ),
            "accept",
          ),
        }),
        statement.if({
          condition: expr.logicalAnd(
            expr.notEqual(
              expr.variable(identifierFromString("accept")),
              { type: "UndefinedLiteral" },
            ),
            expr.callMethod(
              expr.variable(identifierFromString("accept")),
              "includes",
              [expr.stringLiteral("text/html")],
            ),
          ),
          thenStatementList: [
            statement.evaluateExpr(
              expr.callMethod(
                expr.variable(identifierFromString("response")),
                "setHeader",
                [
                  expr.stringLiteral("content-type"),
                  expr.stringLiteral("text/html"),
                ],
              ),
            ),
          ],
        }),
      ],
    }),
  ],
  statementList: [],
};
const codeAsString = generateCodeAsString({
  code: serverModule,
  codeType: "TypeScript",
});
console.log(codeAsString);

出力 output

/** generated by
 * - https://jsr.io/@narumincho/[email protected]
 * Do not edit!
 *
 * @module
 */

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