是否有TypeScript的代码生成API?

问题描述 投票:12回答:4

当我需要生成一些C#代码,例如来自xsd schema的DTO类或excel表时,我使用了一些roslyn API。

打字稿有类似的东西吗?

typescript code-generation t4 roslyn
4个回答
5
投票

截至2018年10月您可以使用标准的TypeScript API

import ts = require("typescript");

function makeFactorialFunction() {
  const functionName = ts.createIdentifier("factorial");
  const paramName = ts.createIdentifier("n");
  const parameter = ts.createParameter(
    /*decorators*/ undefined,
    /*modifiers*/ undefined,
    /*dotDotDotToken*/ undefined,
    paramName
  );

  const condition = ts.createBinary(
    paramName,
    ts.SyntaxKind.LessThanEqualsToken,
    ts.createLiteral(1)
  );

  const ifBody = ts.createBlock(
    [ts.createReturn(ts.createLiteral(1))],
    /*multiline*/ true
  );
  const decrementedArg = ts.createBinary(
    paramName,
    ts.SyntaxKind.MinusToken,
    ts.createLiteral(1)
  );
  const recurse = ts.createBinary(
    paramName,
    ts.SyntaxKind.AsteriskToken,
    ts.createCall(functionName, /*typeArgs*/ undefined, [decrementedArg])
  );
  const statements = [ts.createIf(condition, ifBody), ts.createReturn(recurse)];

  return ts.createFunctionDeclaration(
    /*decorators*/ undefined,
    /*modifiers*/ [ts.createToken(ts.SyntaxKind.ExportKeyword)],
    /*asteriskToken*/ undefined,
    functionName,
    /*typeParameters*/ undefined,
    [parameter],
    /*returnType*/ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
    ts.createBlock(statements, /*multiline*/ true)
  );
}

const resultFile = ts.createSourceFile(
  "someFileName.ts",
  "",
  ts.ScriptTarget.Latest,
  /*setParentNodes*/ false,
  ts.ScriptKind.TS
);
const printer = ts.createPrinter({
  newLine: ts.NewLineKind.LineFeed
});
const result = printer.printNode(
  ts.EmitHint.Unspecified,
  makeFactorialFunction(),
  resultFile
);

console.log(result);

采取在这里:https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-creating-and-printing-a-typescript-ast


12
投票

试试ts-morph。只使用它大约一个小时,但它似乎真的有能力。

import {Project, Scope, SourceFile} from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile(`./target/file.ts`);

const classDeclaration = sourceFile.addClass({
    name: 'SomeClass'
});

const constr = classDeclaration.addConstructor({});
const param = constr.addParameter({
  name: 'myProp',
  type: string
});

constr.setBodyText('this.myProp = myProp');

classDeclaration.addProperty({
    name: 'myProp',
    type: 'string',
    initializer: 'hello world!',
    scope: Scope.Public
});
sourceFile.formatText();
console.log(sourceFile.getText());

5
投票

打字稿有类似的东西吗?

还没有,但TypeScript团队正在为插件打开发射器(what is that),这将使这成为一个受支持的场景:https://github.com/Microsoft/TypeScript/issues/5595


-1
投票

当我们需要使用Angular 4和TypeScript添加对使用RESTful API到MEAN堆栈的支持时,我们使用http://editor.swagger.io并传入Swagger 2.0 API定义的JSON版本,然后为TypeScript选择客户端生成器。

当然我们作弊了一点,因为我们首先使用SZ Architech(http://www.solution.zone)创建RESTful API,它使用SwaggerUi来记录生成的API,并允许我们只复制Swagger 2.0定义以使用Swagger的代码生成对于客户端代码。

© www.soinside.com 2019 - 2024. All rights reserved.