UNPKG

4.5 kBMarkdownView Raw
1# AbstractSyntaxTree
2
3[![Codeship](https://img.shields.io/codeship/b5a42860-956a-0135-6dcd-229add4b057c/master.svg)]() [![NSP Status](https://nodesecurity.io/orgs/buxlabs/projects/4b0bfe3e-43d0-4597-b407-dd44cec2f3d6/badge)](https://nodesecurity.io/orgs/buxlabs/projects/4b0bfe3e-43d0-4597-b407-dd44cec2f3d6)
4
5You 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.
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### each
44
45Iterate over all nodes of given type.
46
47```javascript
48var source = 'var a = "x";';
49var ast = new AbstractSyntaxTree(source);
50ast.each('VariableDeclaration', node => {
51 console.log(node);
52});
53```
54
55### first
56
57First first node of given type.
58
59```javascript
60var source = 'var a = "x";';
61var ast = new AbstractSyntaxTree(source);
62ast.first('VariableDeclaration');
63```
64
65### last
66
67Find last node of given type.
68
69```javascript
70var source = 'var a = "x";';
71var ast = new AbstractSyntaxTree(source);
72ast.last('VariableDeclaration');
73```
74
75### remove
76
77Remove all nodes that match the criteria.
78
79```javascript
80var source = '"use strict"; var b = 4;';
81var ast = new AbstractSyntaxTree(source);
82ast.remove({ type: 'Literal', value: 'use strict' });
83```
84
85```javascript
86var source = 'function hello () { var foo = "bar"; return "world"; }';
87var ast = new AbstractSyntaxTree(source);
88ast.remove('BlockStatement > VariableDeclaration');
89```
90
91### walk
92
93Walks over all nodes
94
95```javascript
96var source = 'var a = 1';
97var ast = new AbstractSyntaxTree(source);
98ast.walk((node, parent) => {
99 console.log(node, parent);
100});
101```
102
103### traverse
104
105Walks over all nodes
106
107```javascript
108var source = 'var a = 1';
109var ast = new AbstractSyntaxTree(source);
110ast.walk({
111 enter: function (node) {
112 console.log(node);
113 },
114 leave: function (node) {
115 console.log(node);
116 }
117});
118```
119
120### replace
121
122Replace all nodes that match the criteria.
123
124```javascript
125var source = 'var a = 1';
126var ast = new AbstractSyntaxTree(source);
127ast.replace({
128 enter: function (node) {
129 if (node.type === 'VariableDeclaration') {
130 node.kind = 'let';
131 }
132 return node;
133 }
134});
135```
136
137### prepend
138
139Prepend a node to the body.
140
141```javascript
142var source = 'var a = 1;';
143var ast = new AbstractSyntaxTree(source);
144ast.prepend({
145 type: 'ExpressionStatement',
146 expression: {
147 type: 'Literal',
148 value: 'use strict'
149 }
150});
151```
152
153### append
154
155Append a node to the body.
156
157```javascript
158var source = 'var a = 1;';
159var ast = new AbstractSyntaxTree(source);
160ast.append({
161 type: 'ExpressionStatement',
162 expression: {
163 type: 'Literal',
164 value: 'test'
165 }
166});
167```
168
169### wrap
170
171Wrap body with given node.
172
173```javascript
174var source = 'var a = 1;';
175var ast = new AbstractSyntaxTree(source);
176ast.wrap(body => {
177 return [
178 {
179 "type": "ExpressionStatement",
180 "expression": {
181 "type": "CallExpression",
182 "callee": {
183 "type": "FunctionExpression",
184 "id": null,
185 "params": [],
186 "defaults": [],
187 "body": {
188 "type": "BlockStatement",
189 "body": body
190 },
191 "rest": null,
192 "generator": false,
193 "expression": false
194 },
195 "arguments": []
196 }
197 }
198 ];
199});
200```
201
202### unwrap
203
204Change the code to the first BlockStatement body
205
206```javascript
207var source = '(function () { console.log(1); }())';
208var ast = new AbstractSyntaxTree(source);
209ast.unwrap();
210ast.toSource();
211```
212
213### template
214
215Create ast partials from templates
216
217```javascript
218var source = 'console.log(1);';
219var ast = new AbstractSyntaxTree(source);
220ast.template('var foo = <%= bar %>;' { bar: { type: 'Literal', value: 1 } });
221```
222
223### toSource / toString
224
225Convert the ast to string.
226
227```javascript
228var source = 'var a = 1;';
229var ast = new AbstractSyntaxTree(source);
230ast.toSource();
231```