19
19
* [ API] ( #api )
20
20
* [ ` toJs(tree[, options]) ` ] ( #tojstree-options )
21
21
* [ ` jsx ` ] ( #jsx )
22
+ * [ ` Handler ` ] ( #handler )
23
+ * [ ` Handlers ` ] ( #handlers )
24
+ * [ ` Options ` ] ( #options )
25
+ * [ ` Result ` ] ( #result )
26
+ * [ ` State ` ] ( #state )
22
27
* [ Examples] ( #examples )
23
28
* [ Example: source maps] ( #example-source-maps )
24
29
* [ Example: comments] ( #example-comments )
@@ -50,7 +55,7 @@ It turns JS into esast.
50
55
## Install
51
56
52
57
This package is [ ESM only] [ esm ] .
53
- In Node.js (version 14.14+, 16.0+, or 18 .0+), install with [ npm] [ ] :
58
+ In Node.js (version 14.14+ and 16 .0+), install with [ npm] [ ] :
54
59
55
60
``` sh
56
61
npm install estree-util-to-js
@@ -59,14 +64,14 @@ npm install estree-util-to-js
59
64
In Deno with [ ` esm.sh ` ] [ esmsh ] :
60
65
61
66
``` js
62
- import {toJs } from " https://esm.sh/estree-util-to-js@1"
67
+ import {toJs } from ' https://esm.sh/estree-util-to-js@1'
63
68
```
64
69
65
70
In browsers with [ ` esm.sh ` ] [ esmsh ] :
66
71
67
72
``` html
68
73
<script type =" module" >
69
- import {toJs } from " https://esm.sh/estree-util-to-js@1?bundle"
74
+ import {toJs } from ' https://esm.sh/estree-util-to-js@1?bundle'
70
75
</script >
71
76
```
72
77
@@ -79,11 +84,7 @@ import {toJs} from 'estree-util-to-js'
79
84
80
85
const file = String (await fs .readFile (' index.js' ))
81
86
82
- const tree = parse (file, {
83
- ecmaVersion: 2022 ,
84
- sourceType: ' module' ,
85
- locations: true
86
- })
87
+ const tree = parse (file, {ecmaVersion: 2022 , sourceType: ' module' , locations: true })
87
88
88
89
// @ts-expect-error: acorn is funky but it works fine.
89
90
console .log (toJs (tree))
@@ -100,46 +101,82 @@ Yields:
100
101
101
102
## API
102
103
103
- This package exports the identifiers ` toJs ` and ` jsx ` .
104
+ This package exports the identifiers [ ` jsx ` ] [ jsx ] and [ ` toJs ` ] [ tojs ] .
104
105
There is no default export.
105
106
106
107
### ` toJs(tree[, options]) `
107
108
108
- Serialize an estree ( [ ` Program ` ] [ program ] ) as JavaScript.
109
+ Serialize an estree as JavaScript.
109
110
110
- ##### ` options `
111
+ ###### Parameters
111
112
112
- Configuration (optional).
113
+ * ` tree ` ([ ` Program ` ] [ program ] )
114
+ — estree
115
+ * ` options ` ([ ` Options ` ] [ options ] )
116
+ — configuration
113
117
114
- ###### ` options.SourceMapGenerator `
118
+ ###### Returns
115
119
116
- Generate a source map by passing the ` SourceMapGenerator ` class from
117
- [ ` source-map ` ] [ source-map ] in.
118
- This works if there is positional info on nodes.
120
+ Result, optionally with source map ([ ` Result ` ] [ result ] ).
119
121
120
- ###### ` options.filePath `
122
+ ### ` jsx `
121
123
122
- Path to original input file (` string ` , example: ` path/to/input.js ` ).
123
- Only used in source map.
124
+ Map of handlers to handle the nodes of JSX extensions in JavaScript ([ ` Handlers ` ] [ handlers ] ).
124
125
125
- ###### ` options.handlers `
126
+ ### ` Handler `
126
127
127
- Object mapping node types to functions handling the corresponding nodes
128
- (` Record<string, Handler> ` ).
129
- Each ` Handler ` is passed the corresponding node and ` astring ` s internal state.
130
- See ` lib/jsx.js ` for examples.
128
+ Handle a particular node (TypeScript type).
131
129
132
- ##### Returns
130
+ ###### Parameters
133
131
134
- An object with two fields:
132
+ * ` this ` (` Generator ` )
133
+ — ` astring ` generator
134
+ * ` node ` ([ ` Node ` ] [ node ] )
135
+ — node to serialize
136
+ * ` state ` ([ ` State ` ] [ state ] )
137
+ — info passed around
135
138
136
- * ` value ` (` string ` ) — serialized JavaScript
137
- * ` map ` (` Object? ` ) — source map as (parsed) JSON, if ` SourceMapGenerator ` is
138
- passed
139
+ ###### Returns
139
140
140
- ### ` jsx `
141
+ Nothing (` void ` ).
142
+
143
+ ### ` Handlers `
144
+
145
+ Handlers of nodes (TypeScript type).
146
+
147
+ ###### Type
148
+
149
+ ``` ts
150
+ type Handlers = Partial <Record <Node [' type' ], Handler >>
151
+ ` ` `
152
+
153
+ ### ` Options `
154
+
155
+ Configuration (TypeScript type).
156
+
157
+ ###### Fields
158
+
159
+ * ` SourceMapGenerator ` ([ ` SourceMapGenerator ` ][source-map])
160
+ — generate a source map with this class
161
+ * ` filePath ` ( ` string ` )
162
+ — path to original input file
163
+ * ` handlers ` ([ ` Handlers ` ][handlers])
164
+ — extra handlers
165
+
166
+ ### ` Result `
141
167
142
- Map of handlers to handle the nodes of JSX extensions in JavaScript.
168
+ Result (TypeScript type).
169
+
170
+ ###### Fields
171
+
172
+ * ` value ` ( ` string ` )
173
+ — serialized JavaScript
174
+ * ` map ` ( ` object ` or ` undefined ` )
175
+ — source map as (parsed) JSON
176
+
177
+ ### ` State `
178
+
179
+ State from ` astring ` (TypeScript type).
143
180
144
181
## Examples
145
182
@@ -270,7 +307,8 @@ Yields:
270
307
## Types
271
308
272
309
This package is fully typed with [ TypeScript] [ ] .
273
- It exports the additional types ` Options ` , ` Handler ` , ` Handlers ` , and ` State ` .
310
+ It exports the additional types [ ` Handler ` ] [ handler ] , [ ` Handlers ` ] [ handlers ] ,
311
+ [ ` Options ` ] [ options ] , [ ` Result ` ] [ result ] , and ` State ` .
274
312
275
313
## Compatibility
276
314
@@ -351,4 +389,20 @@ abide by its terms.
351
389
352
390
[ program ] : https://github.com/estree/estree/blob/master/es2015.md#programs
353
391
392
+ [ node ] : https://github.com/estree/estree/blob/master/es5.md#node-objects
393
+
354
394
[ source-map ] : https://github.com/mozilla/source-map
395
+
396
+ [ jsx ] : #jsx
397
+
398
+ [ tojs ] : #tojstree-options
399
+
400
+ [ handler ] : #handler
401
+
402
+ [ handlers ] : #handlers
403
+
404
+ [ options ] : #options
405
+
406
+ [ state ] : #state
407
+
408
+ [ result ] : #result
0 commit comments