yuku-codegen 0.5.11 → 0.5.15

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.
Files changed (5) hide show
  1. package/README.md +26 -17
  2. package/encode.js +294 -50
  3. package/index.d.ts +10 -5
  4. package/index.js +29 -12
  5. package/package.json +13 -13
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A high-performance JavaScript and TypeScript code generator written in Zig, powered by [Yuku](https://github.com/yuku-toolchain/yuku).
4
4
 
5
- Renders an ESTree / TypeScript-ESTree AST back to source code, with optional Source Map V3 output. The input is exactly the `ParseResult` produced by [`yuku-parser`](https://www.npmjs.com/package/yuku-parser).
5
+ Renders an ESTree / TypeScript-ESTree AST back to source code, with optional Source Map V3 output.
6
6
 
7
7
  ## Install
8
8
 
@@ -16,15 +16,15 @@ npm install yuku-codegen
16
16
  import { parse } from "yuku-parser";
17
17
  import { print } from "yuku-codegen";
18
18
 
19
- const ast = parse("const x = 1 + 2;");
20
- const { code } = print(ast);
19
+ const result = parse("const x = 1 + 2;");
20
+ const { code } = print(result);
21
21
 
22
22
  console.log(code); // "const x = 1 + 2;"
23
23
  ```
24
24
 
25
25
  ## API
26
26
 
27
- Three entry points share the same options and result shape. Each accepts the full `ParseResult` from `yuku-parser`.
27
+ All three entry points take the `ParseResult` returned by [`yuku-parser`](https://www.npmjs.com/package/yuku-parser) and share the same options and result shape.
28
28
 
29
29
  | Function | Behavior |
30
30
  | -------- | ----------------------------------------------------------------------------------------------------- |
@@ -42,17 +42,17 @@ interface CodegenResult {
42
42
  }
43
43
  ```
44
44
 
45
- `errors` is empty on a clean run. `map` is non-null only when `sourceMaps` is set.
45
+ `errors` is empty on a clean run. `map` is non-null only when `sourceMaps` is enabled.
46
46
 
47
47
  ## Options
48
48
 
49
49
  ```js
50
- const result = print(ast, {
50
+ const out = print(result, {
51
51
  format: "pretty",
52
52
  indent: 2,
53
53
  quotes: "double",
54
54
  comments: "some",
55
- sourceMaps: { sourceFileName: "in.js" },
55
+ sourceMaps: true,
56
56
  });
57
57
  ```
58
58
 
@@ -62,20 +62,20 @@ const result = print(ast, {
62
62
  | `indent` | `number` | `2` | Spaces per indentation level. Applies in pretty mode only. |
63
63
  | `quotes` | `"double" \| "single"` | `"double"` | Quote style for emitted string literals. |
64
64
  | `comments` | `boolean \| "some" \| "line" \| "block"` | `"some"` | Comment passthrough filter. See [Comments](#comments). |
65
- | `sourceMaps` | `SourceMapOptions` | _disabled_ | Pass an object to emit a Source Map V3 alongside the code. Omit to skip. |
65
+ | `sourceMaps` | `boolean \| SourceMapOptions` | `false` | `true` emits a Source Map V3 with default metadata; pass an object for fine-grained control. See [Source maps](#source-maps). |
66
66
 
67
67
  ## Source maps
68
68
 
69
- Pass `sourceMaps` to emit a Source Map V3 alongside the generated code.
69
+ Set `sourceMaps: true` for a Source Map V3 with default metadata, or pass a `SourceMapOptions` object to fill in `file`, `sources`, `sourcesContent`, and `sourceRoot`. Source maps are read off `result.lineStarts`, so the `ParseResult` must come from `yuku-parser`.
70
70
 
71
71
  ```js
72
72
  import { parse } from "yuku-parser";
73
73
  import { print } from "yuku-codegen";
74
74
 
75
75
  const source = `const greet = (name) => "Hello, " + name;`;
76
- const ast = parse(source);
76
+ const result = parse(source);
77
77
 
78
- const { code, map } = print(ast, {
78
+ const { code, map } = print(result, {
79
79
  sourceMaps: {
80
80
  file: "out.js",
81
81
  sourceFileName: "in.js",
@@ -122,15 +122,22 @@ Columns are 0-indexed UTF-16 code units, matching Chrome DevTools and consumer-s
122
122
  import { parse } from "yuku-parser";
123
123
  import { strip } from "yuku-codegen";
124
124
 
125
- const ast = parse(`const x: number = 1;`, { lang: "ts" });
126
- console.log(strip(ast).code); // "const x = 1;"
125
+ const result = parse(`const x: number = 1;`, { lang: "ts" });
126
+ console.log(strip(result).code); // "const x = 1;"
127
127
  ```
128
128
 
129
129
  Type annotations, type aliases, interfaces, and other type-only constructs are dropped. Constructs that have no clean JavaScript equivalent (`enum`, `namespace`, `import = require()`, `export =`) are reported in `errors` and elided. The output is always syntactically valid JavaScript.
130
130
 
131
131
  ## Comments
132
132
 
133
- The `comments` option selects which entries from `ast.comments` are emitted. The default is `"some"`, which matches the bundler convention of keeping legal banners, JSDoc, and tree-shaking annotations while dropping plain noise.
133
+ Comments live on the AST nodes they were attached to during parsing. To preserve them, parse with `attachComments: true`:
134
+
135
+ ```js
136
+ const result = parse(source, { attachComments: true });
137
+ print(result).code;
138
+ ```
139
+
140
+ The `comments` option then selects which attached comments are emitted. The default is `"some"`, which matches the bundler convention of keeping legal banners, JSDoc, and tree-shaking annotations while dropping plain noise.
134
141
 
135
142
  | Value | Behavior |
136
143
  | --------- | --------------------------------------------------------------- |
@@ -141,11 +148,13 @@ The `comments` option selects which entries from `ast.comments` are emitted. The
141
148
  | `"block"` | Emit `/* ... */` only. |
142
149
 
143
150
  ```js
144
- const ast = parse(`// hello\nconst x = 1;`);
145
- print(ast, { comments: true }).code;
151
+ const result = parse(`// hello\nconst x = 1;`, { attachComments: true });
152
+ print(result, { comments: true }).code;
146
153
  // "// hello\nconst x = 1;"
147
154
  ```
148
155
 
156
+ Because comments are attached to nodes, they survive AST transforms: move or replace a node and its comments come with it. See the [yuku-parser comments docs](https://www.npmjs.com/package/yuku-parser#comments) for the full design rationale.
157
+
149
158
  ## Minification
150
159
 
151
160
  `minify` applies size-reducing rewrites at print time:
@@ -162,7 +171,7 @@ Combine with `format: "compact"` for full minification:
162
171
  ```js
163
172
  import { minify } from "yuku-codegen";
164
173
 
165
- const { code } = minify(ast, { format: "compact" });
174
+ const { code } = minify(result, { format: "compact" });
166
175
  ```
167
176
 
168
177
  ## License