Skip to content

Commit b547fb1

Browse files
committed
Add improved docs
1 parent 1f0567a commit b547fb1

File tree

4 files changed

+103
-39
lines changed

4 files changed

+103
-39
lines changed

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* @typedef {import('./lib/index.js').Options} Options
32
* @typedef {import('./lib/types.js').Handler} Handler
43
* @typedef {import('./lib/types.js').Handlers} Handlers
4+
* @typedef {import('./lib/index.js').Options} Options
5+
* @typedef {import('./lib/index.js').Result} Result
56
* @typedef {import('./lib/types.js').State} State
67
*/
78

lib/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,28 @@
1313
* @property {null | undefined} [SourceMapGenerator]
1414
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
1515
* in.
16+
* This works if there is positional info on nodes.
1617
* @property {null | undefined} [filePath]
1718
* Path to input file.
19+
* Only used in source map.
1820
*
1921
* @typedef SourceMapFieldsWithSourceMapGenerator
2022
* @property {SourceMapGenerator} SourceMapGenerator
2123
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
2224
* in.
25+
* This works if there is positional info on nodes.
2326
* @property {string | null | undefined} [filePath]
2427
* Path to input file.
28+
* Only used in source map.
2529
*
2630
* @typedef SourceMapFieldsMaybeSourceMapGenerator
2731
* @property {SourceMapGenerator | null | undefined} SourceMapGenerator
2832
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
2933
* in.
34+
* This works if there is positional info on nodes.
3035
* @property {string | null | undefined} [filePath]
3136
* Path to input file.
37+
* Only used in source map.
3238
*
3339
* @typedef {BaseFields & SourceMapFieldsWithoutSourceMapGenerator} OptionsWithoutSourceMapGenerator
3440
* @typedef {BaseFields & SourceMapFieldsWithSourceMapGenerator} OptionsWithSourceMapGenerator
@@ -81,17 +87,14 @@ const GENERATOR = astring.GENERATOR
8187
const generate = astring.generate
8288

8389
/**
84-
* Estree (and esast) utility to serialize estrees as JavaScript.
90+
* Serialize an estree as JavaScript.
8591
*
86-
* @param value
92+
* @param tree
8793
* Estree (esast).
8894
* @param options
8995
* Configuration (optional).
9096
* @returns
91-
* An object with two fields:
92-
* * `value` (`string`) — serialized JavaScript
93-
* * `map` (`Object?`) — source map as (parsed) JSON, if
94-
* `SourceMapGenerator` is passed
97+
* Result, optionally with source map.
9598
*/
9699
export const toJs =
97100
/**

lib/types.js

+6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
/**
2424
* @typedef {Record<Node['type'], Handler>} Generator
2525
* @typedef {Partial<Generator>} Handlers
26+
* Handlers for different nodes.
2627
*
2728
* @callback Handler
29+
* Handle a particular node.
2830
* @param {Generator} this
31+
* `astring` generator.
2932
* @param {any} node
33+
* Node to serialize.
3034
* @param {State} state
35+
* Info passed around.
3136
* @returns {void}
37+
* Nothing.
3238
*/
3339

3440
export {}

readme.md

+86-32
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
* [API](#api)
2020
* [`toJs(tree[, options])`](#tojstree-options)
2121
* [`jsx`](#jsx)
22+
* [`Handler`](#handler)
23+
* [`Handlers`](#handlers)
24+
* [`Options`](#options)
25+
* [`Result`](#result)
26+
* [`State`](#state)
2227
* [Examples](#examples)
2328
* [Example: source maps](#example-source-maps)
2429
* [Example: comments](#example-comments)
@@ -50,7 +55,7 @@ It turns JS into esast.
5055
## Install
5156

5257
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][]:
5459

5560
```sh
5661
npm install estree-util-to-js
@@ -59,14 +64,14 @@ npm install estree-util-to-js
5964
In Deno with [`esm.sh`][esmsh]:
6065

6166
```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'
6368
```
6469

6570
In browsers with [`esm.sh`][esmsh]:
6671

6772
```html
6873
<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'
7075
</script>
7176
```
7277

@@ -79,11 +84,7 @@ import {toJs} from 'estree-util-to-js'
7984

8085
const file = String(await fs.readFile('index.js'))
8186

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})
8788

8889
// @ts-expect-error: acorn is funky but it works fine.
8990
console.log(toJs(tree))
@@ -100,46 +101,82 @@ Yields:
100101

101102
## API
102103

103-
This package exports the identifiers `toJs` and `jsx`.
104+
This package exports the identifiers [`jsx`][jsx] and [`toJs`][tojs].
104105
There is no default export.
105106

106107
### `toJs(tree[, options])`
107108

108-
Serialize an estree ([`Program`][program]) as JavaScript.
109+
Serialize an estree as JavaScript.
109110

110-
##### `options`
111+
###### Parameters
111112

112-
Configuration (optional).
113+
* `tree` ([`Program`][program])
114+
— estree
115+
* `options` ([`Options`][options])
116+
— configuration
113117

114-
###### `options.SourceMapGenerator`
118+
###### Returns
115119

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]).
119121

120-
###### `options.filePath`
122+
### `jsx`
121123

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]).
124125

125-
###### `options.handlers`
126+
### `Handler`
126127

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).
131129

132-
##### Returns
130+
###### Parameters
133131

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
135138

136-
* `value` (`string`) — serialized JavaScript
137-
* `map` (`Object?`) — source map as (parsed) JSON, if `SourceMapGenerator` is
138-
passed
139+
###### Returns
139140

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`
141167
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).
143180
144181
## Examples
145182
@@ -270,7 +307,8 @@ Yields:
270307
## Types
271308

272309
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`.
274312

275313
## Compatibility
276314

@@ -351,4 +389,20 @@ abide by its terms.
351389

352390
[program]: https://github.com/estree/estree/blob/master/es2015.md#programs
353391

392+
[node]: https://github.com/estree/estree/blob/master/es5.md#node-objects
393+
354394
[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

Comments
 (0)