UNPKG

4.5 kBMarkdownView Raw
1# AbstractSyntaxTree
2
3You might find the class useful if you want to abstract away some ast manipulations such as adding, removing, replacing the nodes and others. The test folder is a good starting point for examples of usage. PRs are highly welcome.
4
5- [x] 100% coverage
6
7## Installation
8
9`npm install --save @buxlabs/ast`
10
11## Methods
12
13### has
14
15Check if ast contains a node of given type.
16
17```javascript
18var source = 'var a = "x";';
19var ast = new AbstractSyntaxTree(source);
20ast.has('VariableDeclaration');
21```
22
23### count
24
25Count ast nodes of given type.
26
27```javascript
28var source = 'var a = "x"; var b = "y";';
29var ast = new AbstractSyntaxTree(source);
30ast.count('VariableDeclaration');
31```
32
33### find
34
35Find all nodes of given type.
36
37```javascript
38var source = 'var a = "x";';
39var ast = new AbstractSyntaxTree(source);
40ast.find('VariableDeclaration');
41```
42
43```javascript
44var source = 'var a = "x";';
45var ast = new AbstractSyntaxTree(source, { engine: 'astq' });
46ast.find(`
47 // VariableDeclarator [
48 /:id Identifier [ @name ]
49 && /:init Literal [ @value ]
50 ]
51`, { engine: 'astq' });
52```
53
54### each
55
56Iterate over all nodes of given type.
57
58```javascript
59var source = 'var a = "x";';
60var ast = new AbstractSyntaxTree(source);
61ast.each('VariableDeclaration', node => {
62 console.log(node);
63});
64```
65
66### first
67
68First first node of given type.
69
70```javascript
71var source = 'var a = "x";';
72var ast = new AbstractSyntaxTree(source);
73ast.first('VariableDeclaration');
74```
75
76### last
77
78Find last node of given type.
79
80```javascript
81var source = 'var a = "x";';
82var ast = new AbstractSyntaxTree(source);
83ast.last('VariableDeclaration');
84```
85
86### remove
87
88Remove all nodes that match the criteria.
89
90```javascript
91var source = '"use strict"; var b = 4;';
92var ast = new AbstractSyntaxTree(source);
93ast.remove({ type: 'Literal', value: 'use strict' });
94```
95
96```javascript
97var source = 'function hello () { var foo = "bar"; return "world"; }';
98var ast = new AbstractSyntaxTree(source);
99ast.remove('BlockStatement > VariableDeclaration');
100```
101
102### walk
103
104Walks over all nodes
105
106```javascript
107var source = 'var a = 1';
108var ast = new AbstractSyntaxTree(source);
109ast.walk((node, parent) => {
110 console.log(node, parent);
111});
112```
113
114### traverse
115
116Walks over all nodes
117
118```javascript
119var source = 'var a = 1';
120var ast = new AbstractSyntaxTree(source);
121ast.walk({
122 enter: function (node) {
123 console.log(node);
124 },
125 leave: function (node) {
126 console.log(node);
127 }
128});
129```
130
131### replace
132
133Replace all nodes that match the criteria.
134
135```javascript
136var source = 'var a = 1';
137var ast = new AbstractSyntaxTree(source);
138ast.replace({
139 enter: function (node) {
140 if (node.type === 'VariableDeclaration') {
141 node.kind = 'let';
142 }
143 return node;
144 }
145});
146```
147
148### prepend
149
150Prepend a node to the body.
151
152```javascript
153var source = 'var a = 1;';
154var ast = new AbstractSyntaxTree(source);
155ast.prepend({
156 type: 'ExpressionStatement',
157 expression: {
158 type: 'Literal',
159 value: 'use strict'
160 }
161});
162```
163
164### append
165
166Append a node to the body.
167
168```javascript
169var source = 'var a = 1;';
170var ast = new AbstractSyntaxTree(source);
171ast.append({
172 type: 'ExpressionStatement',
173 expression: {
174 type: 'Literal',
175 value: 'test'
176 }
177});
178```
179
180### wrap
181
182Wrap body with given node.
183
184```javascript
185var source = 'var a = 1;';
186var ast = new AbstractSyntaxTree(source);
187ast.wrap(body => {
188 return [
189 {
190 "type": "ExpressionStatement",
191 "expression": {
192 "type": "CallExpression",
193 "callee": {
194 "type": "FunctionExpression",
195 "id": null,
196 "params": [],
197 "defaults": [],
198 "body": {
199 "type": "BlockStatement",
200 "body": body
201 },
202 "rest": null,
203 "generator": false,
204 "expression": false
205 },
206 "arguments": []
207 }
208 }
209 ];
210});
211```
212
213### unwrap
214
215Change the code to the first BlockStatement body
216
217```javascript
218var source = '(function () { console.log(1); }())';
219var ast = new AbstractSyntaxTree(source);
220ast.unwrap();
221ast.toSource();
222```
223
224### template
225
226Create ast partials from templates
227
228```javascript
229var source = 'console.log(1);';
230var ast = new AbstractSyntaxTree(source);
231ast.template('var foo = <%= bar %>;' { bar: { type: 'Literal', value: 1 } });
232```
233
234### toSource / toString
235
236Convert the ast to string.
237
238```javascript
239var source = 'var a = 1;';
240var ast = new AbstractSyntaxTree(source);
241ast.toSource();
242```