You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the nodes of the input AST have `loc` properties (e.g. the AST was generated with [`acorn`](https://github.com/acornjs/acorn/tree/master/acorn/#interface) with the `locations` option set), sourcemap mappings will be created.
35
38
39
+
## Built-in languages
40
+
41
+
`esrap` ships with two built-in languages — `ts()` and `tsx()` — which can print ASTs conforming to [`@typescript-eslint/types`](https://www.npmjs.com/package/@typescript-eslint/types) (which extends [ESTree](https://github.com/estree/estree)):
42
+
43
+
```js
44
+
importtsfrom'esrap/languages/ts';
45
+
importtsxfrom'esrap/languages/tsx';
46
+
```
47
+
48
+
Both languages accept an options object:
49
+
50
+
```js
51
+
const { code, map } =print(
52
+
ast,
53
+
ts({
54
+
// how string literals should be quoted — `single` (the default) or `double`
55
+
quotes:'single',
56
+
57
+
// an array of `{ type: 'Line' | 'Block', value: string, loc: { start, end } }` objects
58
+
comments: []
59
+
})
60
+
);
61
+
```
62
+
63
+
You can generate the `comments` array by, for example, using [Acorn's](https://github.com/acornjs/acorn/tree/master/acorn/#interface)`onComment` option.
64
+
65
+
## Custom languages
66
+
67
+
You can also create your own languages:
68
+
69
+
```ts
70
+
import { print, typeVisitors } from'esrap';
71
+
72
+
const language:Visitors<MyNodeType> = {
73
+
_(node, context, visit) {
74
+
// the `_` visitor handles any node type
75
+
context.write('[');
76
+
visit(node);
77
+
context.write(']');
78
+
},
79
+
List(node, context) {
80
+
// node.type === 'List'
81
+
for (const child ofnode.children) {
82
+
context.visit(child);
83
+
}
84
+
},
85
+
Foo(node, context) {
86
+
// node.type === 'Foo'
87
+
context.write('foo');
88
+
},
89
+
Bar(node, context) {
90
+
// node.type === 'Bar'
91
+
context.write('bar');
92
+
}
93
+
};
94
+
95
+
const ast:MyNodeType= {
96
+
type: 'List',
97
+
children: [{ type: 'Foo' }, { type: 'Bar' }]
98
+
};
99
+
100
+
const { code, map } =print(ast, language);
101
+
102
+
code; // `[[foo][bar]]`
103
+
```
104
+
105
+
The `context` API has several methods:
106
+
107
+
-`context.write(data: string, node?: BaseNode)` — add a string. If `node` is provided and has a standard `loc` property (with `start` and `end` properties each with a `line` and `column`), a sourcemap mapping will be created
108
+
-`context.indent()` — increase the indentation level, typically before adding a newline
109
+
-`context.newline()` — self-explanatory
110
+
-`context.margin()` — causes the next newline to be repeated (consecutive newlines are otherwise merged into one)
111
+
-`context.dedent()` — decrease the indentation level (again, typically before adding a newline)
112
+
-`context.visit(node: BaseNode)` — calls the visitor corresponding to `node.type`
-`context.measure()` — returns the number of characters contained in `context`
115
+
-`context.empty()` — returns true if the context has no content
116
+
-`context.new()` — creates a child context
117
+
-`context.append(child)` — appends a child context
118
+
119
+
In addition, `context.multiline` is `true` if the context has multiline content. (This is useful for knowing, for example, when to insert newlines between nodes.)
120
+
121
+
To understand how to wield these methods effectively, read the source code for the built-in languages.
122
+
36
123
## Options
37
124
38
125
You can pass the following options:
39
126
40
127
```js
41
-
const { code, map } =print(ast, {
128
+
const { code, map } =print(ast, ts(), {
42
129
// Populate the `sources` field of the resulting sourcemap
43
130
// (note that the AST is assumed to come from a single file)
// String to use for indentation — defaults to '\t'
54
-
indent:'',
55
-
56
-
// Whether to wrap strings in single or double quotes — defaults to 'single'.
57
-
// This only applies to string literals with no `raw` value, which generally
58
-
// means the AST node was generated programmatically, rather than parsed
59
-
// from an original source
60
-
quotes:'single',
61
-
62
-
// Overrwrite the inbuilt printers
63
-
handlers: {
64
-
...ts, // the default, imported from 'esrap/languages/ts'
65
-
CustomNode(node, state) {
66
-
state.commands.push('this is custom') // see the source code in the inbuilt languages for examples on how to make your own
67
-
}
68
-
}
141
+
indent:''
69
142
});
70
143
```
71
144
72
-
## TypeScript
73
-
74
-
`esrap` can also print TypeScript nodes, assuming they match the ESTree-like [`@typescript-eslint/types`](https://www.npmjs.com/package/@typescript-eslint/types).
0 commit comments