nushackers / openhack-submissions

Submissions for OpenHack 2020 (https://openhack.nushackers.org/).
Apache License 2.0
4 stars 0 forks source link

Submission - taneliang #3

Open taneliang opened 4 years ago

taneliang commented 4 years ago

Submission Details

Type of Submission:

Links to open-source project:

  1. Repo: https://github.com/taneliang/tscodegen
  2. Published: https://npm.im/@elg/tscodegen

Additional information (optional):

(Copied from README)

tscodegen is a minimal TypeScript port of Facebook's Hack Codegen. It provides a fluent API that allows you to build human-readable TypeScript source files from strings of code, with optional manually editable sections and tamper detection.

Sample:

Codegen script

new CodeFile("./file.ts")
  .build((builder) =>
    builder
      .addLine("import path from 'path';")
      .addLine("import fs from 'fs'")
      .addLine()
      .addManualSection("custom_imports", (builder) => builder)
      .addLine()
      .addBlock("class Steam extends Water", (builder) =>
        builder
          .addBlock("constructor()", (builder) =>
            builder.addLine("this.boil();")
          )
          .addLine()
          .addBlock("boil()", (b) =>
            b.addManualSection("boil_body", (builder) =>
              builder.add("this.temp = 100;")
            )
          )
      )
      .format()
  )
  .saveToFile();

Output

/**
 * This file is generated with manually editable sections. Only make
 * modifications between BEGIN MANUAL SECTION and END MANUAL SECTION
 * designators.
 *
 * @generated-editable Codelock<<jF8gPj9IVq16NXBAtEzJj0rrD9HR7Q6V>>
 */

import path from "path";
import fs from "fs";

/* BEGIN MANUAL SECTION custom_imports */
/* END MANUAL SECTION */

class Steam extends Water {
  constructor() {
    this.boil();
  }

  boil() {
    /* BEGIN MANUAL SECTION boil_body */
    new Magician().magic(); // This line was retrieved from the original file.ts
    /* END MANUAL SECTION */
  }
}
taneliang commented 4 years ago

As an NUS Hackers coreteam member, imma disqualify myself from the prizes. Hopefully this project gives people ideas :D

chrisgzf commented 4 years ago

Cool project!