yuku-codegen 0.5.14 → 0.5.16

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 +23 -24
  2. package/encode.js +294 -46
  3. package/index.d.ts +10 -5
  4. package/index.js +21 -8
  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
  | -------- | ----------------------------------------------------------------------------------------------------- |
@@ -47,7 +47,7 @@ interface CodegenResult {
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",
@@ -66,26 +66,16 @@ const result = print(ast, {
66
66
 
67
67
  ## Source maps
68
68
 
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`. `false` (or omitting the field) disables map output.
70
-
71
- ```js
72
- import { parse } from "yuku-parser";
73
- import { print } from "yuku-codegen";
74
-
75
- const ast = parse(`const x = 1;`);
76
- const { map } = print(ast, { sourceMaps: true });
77
- ```
78
-
79
- For a map with embedded source information:
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`.
80
70
 
81
71
  ```js
82
72
  import { parse } from "yuku-parser";
83
73
  import { print } from "yuku-codegen";
84
74
 
85
75
  const source = `const greet = (name) => "Hello, " + name;`;
86
- const ast = parse(source);
76
+ const result = parse(source);
87
77
 
88
- const { code, map } = print(ast, {
78
+ const { code, map } = print(result, {
89
79
  sourceMaps: {
90
80
  file: "out.js",
91
81
  sourceFileName: "in.js",
@@ -132,15 +122,22 @@ Columns are 0-indexed UTF-16 code units, matching Chrome DevTools and consumer-s
132
122
  import { parse } from "yuku-parser";
133
123
  import { strip } from "yuku-codegen";
134
124
 
135
- const ast = parse(`const x: number = 1;`, { lang: "ts" });
136
- 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;"
137
127
  ```
138
128
 
139
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.
140
130
 
141
131
  ## Comments
142
132
 
143
- 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.
144
141
 
145
142
  | Value | Behavior |
146
143
  | --------- | --------------------------------------------------------------- |
@@ -151,11 +148,13 @@ The `comments` option selects which entries from `ast.comments` are emitted. The
151
148
  | `"block"` | Emit `/* ... */` only. |
152
149
 
153
150
  ```js
154
- const ast = parse(`// hello\nconst x = 1;`);
155
- print(ast, { comments: true }).code;
151
+ const result = parse(`// hello\nconst x = 1;`, { attachComments: true });
152
+ print(result, { comments: true }).code;
156
153
  // "// hello\nconst x = 1;"
157
154
  ```
158
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
+
159
158
  ## Minification
160
159
 
161
160
  `minify` applies size-reducing rewrites at print time:
@@ -172,7 +171,7 @@ Combine with `format: "compact"` for full minification:
172
171
  ```js
173
172
  import { minify } from "yuku-codegen";
174
173
 
175
- const { code } = minify(ast, { format: "compact" });
174
+ const { code } = minify(result, { format: "compact" });
176
175
  ```
177
176
 
178
177
  ## License