@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 +5 -4
- package/index.d.ts +32 -3
- package/index.js +19 -5
- package/package.json +1 -1
- package/yuku-codegen.wasm +0 -0
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 {
|
|
11
|
+
import { generate } from "@yuku-codegen/wasm";
|
|
12
12
|
|
|
13
13
|
const { program } = parse("const x: number = 1", { lang: "ts" });
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
32
|
+
/** @deprecated Use `generate(program, { strip: true })`. */
|
|
4
33
|
export function strip(program: any): string;
|
|
5
|
-
/**
|
|
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
|
-
|
|
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,
|
|
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
|
|
50
|
+
return generate(program);
|
|
39
51
|
}
|
|
40
52
|
|
|
53
|
+
/** @deprecated Use `generate(program, { strip: true })`. */
|
|
41
54
|
export function strip(program) {
|
|
42
|
-
return
|
|
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
|
|
60
|
+
return generate(program, { minify: true });
|
|
47
61
|
}
|
package/package.json
CHANGED
package/yuku-codegen.wasm
CHANGED
|
Binary file
|