0.7.0 • Published 1 year ago

ts-factory-code-generator-generator v0.7.0

Weekly downloads
61
License
MIT
Repository
github
Last release
1 year ago

TypeScript Compiler API Factory Code Generator Generator

Generates code that generates TypeScript Compiler API factory code from an AST.

This serves the purpose of generating code that generates compiler API factory code from an AST based on specific TypeScript compiler API version. The generated code is used on ts-ast-viewer.com.

Benefits

  1. Automatic (minimal) maintenance of future factory code functionality (typescript@next support).
  2. Support all compiler API versions.
  3. Reuses the AST used by the rest of the ts-ast-viewer.com site. So improves performance and allows showing the factory code for the selected node. Couldn't do that before because I was pegged to a specific compiler API version and had to reparse the source file specifically for the factory code generation.

Use

Setup

First, save the TypeScript versions as aliases using yarn:

yarn add typescript-4.0.2@npm:typescript@4.0.2

Get and save the generated code for each compiler API version

Then generate the code for each compiler api version and save it to a file:

import * as fs from "fs";
import { generateCode } from "ts-factory-code-generator-generator";

const code = generateCode("typescript");
fs.writeFile("./factoryCodeGenerator.ts", { encoding: "utf-8" }, code, ...etc...);

const codeFor4_0_2 = generateCode("typescript-4.0.2");
fs.writeFile("./factoryCodeGenerator-4.0.2.ts", { encoding: "utf-8" }, codeFor4_0_2, ...etc...);

// ...etc...

Use the generated code to generate factory code

Finally, generate the factory code from an AST for the appropriate compiler API version:

import * as ts from "typescript-4.0.2";
import { generateFactoryCode } from "./factoryCodeGenerator-4.0.2";

// get ast
const sourceFile = ts.createSourceFile("/file.ts", "4n + 5n;", ts.ScriptTarget.Latest);

// get the generated factory code
const factoryCode = generateFactoryCode(ts, sourceFile); // or provide a different node here
console.log(factoryCode);

Outputs:

[
  factory.createExpressionStatement(ts.createBinary(
    factory.createBigIntLiteral("4n"),
    factory.createToken(ts.SyntaxKind.PlusToken),
    factory.createBigIntLiteral("5n")
  ))
];
0.7.0

1 year ago

0.6.4

2 years ago

0.6.3

2 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

4 years ago

0.5.5

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago