oxc-codegen 0.0.0 → 0.145.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/LICENSE +22 -0
- package/README.md +148 -0
- package/dist/index.d.ts +1362 -0
- package/dist/index.js +1 -0
- package/dist/print_js.js +54 -0
- package/dist/print_js_maps.js +55 -0
- package/dist/print_ts.js +80 -0
- package/dist/print_ts_maps.js +81 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present VoidZero Inc. & Contributors
|
|
4
|
+
Copyright (c) 2023 Boshen
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# oxc-codegen
|
|
2
|
+
|
|
3
|
+
Fast, synchronous code generation for JavaScript and TypeScript ASTs.
|
|
4
|
+
|
|
5
|
+
`oxc-codegen` turns an [ESTree](https://github.com/estree/estree) or
|
|
6
|
+
[TS-ESTree](https://typescript-eslint.io/packages/typescript-estree/) AST into formatted source
|
|
7
|
+
code. It supports JavaScript, JSX, TypeScript, and TSX.
|
|
8
|
+
|
|
9
|
+
The printer is a port of Oxc's Rust `oxc_codegen` crate. With the default options, both printers
|
|
10
|
+
produce byte-identical output: tab indentation, double-quoted strings, and no comments.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install oxc-codegen
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`oxc-codegen` is ESM-only and requires Node.js `^20.19.0` or `>=22.12.0`.
|
|
19
|
+
|
|
20
|
+
## Quick start
|
|
21
|
+
|
|
22
|
+
Pair it with [`oxc-parser`](https://www.npmjs.com/package/oxc-parser) to parse and print source code:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { printSync } from "oxc-codegen";
|
|
26
|
+
import { parseSync } from "oxc-parser";
|
|
27
|
+
|
|
28
|
+
const { program } = parseSync("input.js", "const answer=6*7");
|
|
29
|
+
const { code } = printSync(program);
|
|
30
|
+
|
|
31
|
+
console.log(code);
|
|
32
|
+
// const answer = 6 * 7;
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
You can also print a manually constructed AST:
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const program = {
|
|
39
|
+
type: "Program",
|
|
40
|
+
sourceType: "script",
|
|
41
|
+
body: [
|
|
42
|
+
{
|
|
43
|
+
type: "ExpressionStatement",
|
|
44
|
+
expression: {
|
|
45
|
+
type: "CallExpression",
|
|
46
|
+
callee: {
|
|
47
|
+
type: "MemberExpression",
|
|
48
|
+
object: { type: "Identifier", name: "console" },
|
|
49
|
+
property: { type: "Identifier", name: "log" },
|
|
50
|
+
computed: false,
|
|
51
|
+
optional: false,
|
|
52
|
+
},
|
|
53
|
+
arguments: [{ type: "Literal", value: "Hello!" }],
|
|
54
|
+
optional: false,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
console.log(printSync(program).code);
|
|
61
|
+
// console.log("Hello!");
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### TypeScript and TSX
|
|
65
|
+
|
|
66
|
+
Set `ts` when the AST can contain TypeScript nodes. For TSX, set both `ts` and `jsx`:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const { program } = parseSync("component.tsx", "const Box = <T,>(value: T) => <div>{value}</div>");
|
|
70
|
+
|
|
71
|
+
const { code } = printSync(program, {
|
|
72
|
+
ts: true,
|
|
73
|
+
jsx: true,
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## API
|
|
78
|
+
|
|
79
|
+
### `printSync(node, options?)`
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
function printSync(node: Node, options?: Options): { code: string; map: SourceMap | null };
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Prints a complete `Program` or a single statement and returns the generated source code,
|
|
86
|
+
and (when requested) a standard Source Map v3 object.
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
import { printSync } from "oxc-codegen";
|
|
90
|
+
import { parseSync } from "oxc-parser";
|
|
91
|
+
|
|
92
|
+
const sourceText = "const answer=6*7";
|
|
93
|
+
const { program } = parseSync("input.js", sourceText);
|
|
94
|
+
const { code, map } = printSync(program, {
|
|
95
|
+
sourcemap: true,
|
|
96
|
+
sourceFilename: "input.js",
|
|
97
|
+
sourceText,
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Source-map mappings require `sourceText` and nodes with valid Oxc `start` / `end` offsets.
|
|
102
|
+
A manually constructed AST without offsets can still be printed, but its source map has
|
|
103
|
+
an empty `mappings` string.
|
|
104
|
+
|
|
105
|
+
### Options
|
|
106
|
+
|
|
107
|
+
| Option | Type | Default | Description |
|
|
108
|
+
| :-------------------- | :-------- | :------ | :--------------------------------------------------------------- |
|
|
109
|
+
| `indent` | `string` | `"\t"` | Non-empty string of spaces and/or tabs used for one indent level |
|
|
110
|
+
| `startingIndentLevel` | `number` | `0` | Starting indent level, from `0` to `1000` |
|
|
111
|
+
| `jsx` | `boolean` | `false` | Enable TSX-safe printing for ambiguous TypeScript syntax |
|
|
112
|
+
| `ts` | `boolean` | `false` | Select the printer that supports TypeScript nodes |
|
|
113
|
+
| `sourcemap` | `boolean` | `false` | Return a Source Map v3 object in `map` |
|
|
114
|
+
| `sourceFilename` | `string` | `""` | Original source filename recorded in the source map |
|
|
115
|
+
| `sourceText` | `string` | - | Original text required for source-map mappings and content |
|
|
116
|
+
|
|
117
|
+
## Why pure JavaScript?
|
|
118
|
+
|
|
119
|
+
Most Oxc packages use native bindings. This package deliberately does not: when an AST already
|
|
120
|
+
lives in JavaScript, passing the entire object graph across a JS/native boundary can cost more than
|
|
121
|
+
printing it in place. `oxc-codegen` avoids that serialization and uses specialized printer builds
|
|
122
|
+
for JavaScript and TypeScript workloads.
|
|
123
|
+
|
|
124
|
+
See [DESIGN.md](https://github.com/oxc-project/oxc/blob/main/packages/codegen/DESIGN.md) for the
|
|
125
|
+
implementation details and performance constraints.
|
|
126
|
+
|
|
127
|
+
## Current limitations
|
|
128
|
+
|
|
129
|
+
- Comments are not printed.
|
|
130
|
+
- Minified output is not supported.
|
|
131
|
+
|
|
132
|
+
## Benchmarks
|
|
133
|
+
|
|
134
|
+
Representative time per `printSync` call:
|
|
135
|
+
|
|
136
|
+
| Fixture | Bytes | Time |
|
|
137
|
+
| :--------------------------- | --------: | ---------: |
|
|
138
|
+
| `tiny.js` | 26 | 0.0001 ms |
|
|
139
|
+
| `RadixUIAdoptionSection.jsx` | 2,518 | 0.0033 ms |
|
|
140
|
+
| `react.development.js` | 72,141 | 0.1138 ms |
|
|
141
|
+
| `binder.ts` | 193,077 | 0.2472 ms |
|
|
142
|
+
| `App.tsx` | 415,340 | 0.7490 ms |
|
|
143
|
+
| `lodash.js` | 544,096 | 0.4995 ms |
|
|
144
|
+
| `kitchen-sink.tsx` | 732,222 | 2.5682 ms |
|
|
145
|
+
| `antd.js` | 6,683,633 | 11.3914 ms |
|
|
146
|
+
|
|
147
|
+
These figures come from one machine and are illustrative, not a regression baseline. Results—most
|
|
148
|
+
noticeably for large fixtures such as `antd.js`—vary between runs.
|