@yuku-codegen/wasm 0.6.12 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,11 +8,12 @@ returns generated source.
8
8
 
9
9
  ```js
10
10
  import { parse } from "@yuku-parser/wasm";
11
- import { print, strip, minify } from "@yuku-codegen/wasm";
11
+ import { generate } from "@yuku-codegen/wasm";
12
12
 
13
13
  const { program } = parse("const x: number = 1", { lang: "ts" });
14
14
 
15
- print(program); // formatted source
16
- strip(program); // TypeScript stripped to JavaScript
17
- minify(program); // compact
15
+ generate(program); // formatted source
16
+ generate(program, { strip: true }); // TypeScript stripped to JavaScript
17
+ generate(program, { minify: true }); // minified
18
+ generate(program, { strip: true, minify: true }); // both
18
19
  ```
package/index.d.ts CHANGED
@@ -1,6 +1,35 @@
1
- /** Print the AST back to formatted source. */
1
+ /** Minification switches. */
2
+ export interface MinifyOptions {
3
+ /** Emit compact whitespace. */
4
+ whitespace?: boolean;
5
+ /** Apply size-reducing syntax rewrites. */
6
+ syntax?: boolean;
7
+ /** Use shortest quotes. */
8
+ quotes?: boolean;
9
+ }
10
+
11
+ /** Options for `generate`. Transformations are independent flags. */
12
+ export interface GenerateOptions {
13
+ /** Drop TypeScript-only syntax and emit plain JavaScript. */
14
+ strip?: boolean;
15
+ /** `true` enables every switch for maximum minification. */
16
+ minify?: boolean | MinifyOptions;
17
+ /** @default "pretty" */
18
+ format?: "pretty" | "compact";
19
+ /** Spaces per indentation level in pretty format. @default 2 */
20
+ indent?: number;
21
+ /** @default "preserve" */
22
+ quotes?: "preserve" | "double" | "single" | "shortest";
23
+ /** @default "some" */
24
+ comments?: boolean | "all" | "some" | "none" | "line" | "block";
25
+ }
26
+
27
+ /** Renders the AST back to source code. */
28
+ export function generate(program: any, options?: GenerateOptions): string;
29
+
30
+ /** @deprecated Use {@link generate}. */
2
31
  export function print(program: any): string;
3
- /** Print TypeScript to JavaScript, dropping TypeScript-only syntax. */
32
+ /** @deprecated Use `generate(program, { strip: true })`. */
4
33
  export function strip(program: any): string;
5
- /** Print minified (compact, comments dropped) source. */
34
+ /** @deprecated Use `generate(program, { minify: true })`. */
6
35
  export function minify(program: any): string;
package/index.js CHANGED
@@ -18,13 +18,24 @@ async function instantiate() {
18
18
  const { memory, alloc, free, codegen } = (await instantiate()).exports;
19
19
  const decoder = new TextDecoder();
20
20
 
21
- function run(op, program) {
21
+ const QUOTES = { preserve: 0, double: 1, single: 2, shortest: 3 };
22
+ const COMMENTS = { none: 0, false: 0, all: 1, true: 1, some: 2, line: 3, block: 4 };
23
+
24
+ export function generate(program, options = {}) {
25
+ const m = options.minify === true ? { whitespace: true, syntax: true, quotes: true } : (options.minify ?? {});
26
+ const flags =
27
+ (options.strip ? 1 : 0) |
28
+ (m.syntax ? 2 : 0) |
29
+ (m.whitespace || options.format === "compact" ? 4 : 0) |
30
+ ((m.quotes ? 3 : (QUOTES[options.quotes] ?? 0)) << 3) |
31
+ ((COMMENTS[options.comments] ?? 2) << 5) |
32
+ (((options.indent ?? 2) & 0xff) << 8);
22
33
  const ast = new Uint8Array(encode(program, null));
23
34
  const inPtr = alloc(ast.length || 1);
24
35
  // Growing wasm memory detaches memory.buffer, so re-view after every call.
25
36
  new Uint8Array(memory.buffer, inPtr, ast.length).set(ast);
26
37
 
27
- const ptr = codegen(inPtr, ast.length, op);
38
+ const ptr = codegen(inPtr, ast.length, flags);
28
39
  free(inPtr, ast.length || 1);
29
40
  if (ptr === 0) throw new Error("yuku-codegen-wasm: failed to generate code");
30
41
 
@@ -34,14 +45,17 @@ function run(op, program) {
34
45
  return code;
35
46
  }
36
47
 
48
+ /** @deprecated Use `generate(program)`. */
37
49
  export function print(program) {
38
- return run(0, program);
50
+ return generate(program);
39
51
  }
40
52
 
53
+ /** @deprecated Use `generate(program, { strip: true })`. */
41
54
  export function strip(program) {
42
- return run(1, program);
55
+ return generate(program, { strip: true });
43
56
  }
44
57
 
58
+ /** @deprecated Use `generate(program, { minify: true })`. */
45
59
  export function minify(program) {
46
- return run(2, program);
60
+ return generate(program, { minify: true });
47
61
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuku-codegen/wasm",
3
- "version": "0.6.12",
3
+ "version": "0.7.0",
4
4
  "description": "JavaScript/TypeScript code generator as a WebAssembly module",
5
5
  "license": "MIT",
6
6
  "repository": {
package/yuku-codegen.wasm CHANGED
Binary file