TypeScript是否有代码生成API?

31

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

是否有类似于typescript的东西?

[编辑]: 我最终使用了ts-morph


我看到你已经标记为T4了--T4可以与任何类型的语言一起使用,因为它基本上只是纯文本。Roslyn仅支持VB.NET和C#。 - Jeroen Vannevel
您可以查看此链接:https://www.codeproject.com/Tips/1166380/Generating-TypeScript-Code-with-CatFactory - H. Herzl
5个回答

40

尝试使用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({});

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

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

10
在使用这个库更长的时间后,我可以确认它是真正有用的。尽管还有一些错误,但它肯定具有很多潜力。作者做得真的非常好。 - NSjonas
1
仍在积极开发中,具有非常好的API。感谢推荐。 - Ryall
3
作为原始提问者,我可以确认我最终使用了ts-morph。我尝试过原生的TypeScript编译器API,但它太底层了,我最终编写了一些帮助函数,这些函数正好是ts-morph所做的。 - Liero

26
截至2018年10月,您可以使用标准的TypeScript API进行操作。

import ts = require("typescript");

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

  const condition = ts.factory.createBinaryExpression(
    paramName,
    ts.SyntaxKind.LessThanEqualsToken,
    ts.factory.createNumericLiteral(1)
  );

  const ifBody = ts.factory.createBlock(
    [ts.factory.createReturnStatement(ts.factory.createNumericLiteral(1))],
    /*multiline*/ true
  );
  const decrementedArg = ts.factory.createBinaryExpression(
    paramName,
    ts.SyntaxKind.MinusToken,
    ts.factory.createNumericLiteral(1)
  );
  const recurse = ts.factory.createBinaryExpression(
    paramName,
    ts.SyntaxKind.AsteriskToken,
    ts.factory.createCallExpression(functionName, /*typeArgs*/ undefined, [decrementedArg])
  );
  const statements = [ts.factory.createIfStatement(condition, ifBody), ts.factory.createReturnStatement(recurse)];

  return ts.factory.createFunctionDeclaration(
    /*decorators*/ undefined,
    /*modifiers*/ [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
    /*asteriskToken*/ undefined,
    functionName,
    /*typeParameters*/ undefined,
    [parameter],
    /*returnType*/ ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
    ts.factory.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


4

-7

-9

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接