1 | # AbstractSyntaxTree
|
2 |
|
3 | You 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 |
|
15 | Check if ast contains a node of given type.
|
16 |
|
17 | ```javascript
|
18 | var source = 'var a = "x";';
|
19 | var ast = new AbstractSyntaxTree(source);
|
20 | ast.has('VariableDeclaration');
|
21 | ```
|
22 |
|
23 | ### count
|
24 |
|
25 | Count ast nodes of given type.
|
26 |
|
27 | ```javascript
|
28 | var source = 'var a = "x"; var b = "y";';
|
29 | var ast = new AbstractSyntaxTree(source);
|
30 | ast.count('VariableDeclaration');
|
31 | ```
|
32 |
|
33 | ### find
|
34 |
|
35 | Find all nodes of given type.
|
36 |
|
37 | ```javascript
|
38 | var source = 'var a = "x";';
|
39 | var ast = new AbstractSyntaxTree(source);
|
40 | ast.find('VariableDeclaration');
|
41 | ```
|
42 |
|
43 | ### each
|
44 |
|
45 | Iterate over all nodes of given type.
|
46 |
|
47 | ```javascript
|
48 | var source = 'var a = "x";';
|
49 | var ast = new AbstractSyntaxTree(source);
|
50 | ast.each('VariableDeclaration', node => {
|
51 | console.log(node);
|
52 | });
|
53 | ```
|
54 |
|
55 | ### first
|
56 |
|
57 | First first node of given type.
|
58 |
|
59 | ```javascript
|
60 | var source = 'var a = "x";';
|
61 | var ast = new AbstractSyntaxTree(source);
|
62 | ast.first('VariableDeclaration');
|
63 | ```
|
64 |
|
65 | ### last
|
66 |
|
67 | Find last node of given type.
|
68 |
|
69 | ```javascript
|
70 | var source = 'var a = "x";';
|
71 | var ast = new AbstractSyntaxTree(source);
|
72 | ast.last('VariableDeclaration');
|
73 | ```
|
74 |
|
75 | ### remove
|
76 |
|
77 | Remove all nodes that match the criteria.
|
78 |
|
79 | ```javascript
|
80 | var source = '"use strict"; var b = 4;';
|
81 | var ast = new AbstractSyntaxTree(source);
|
82 | ast.remove({ type: 'Literal', value: 'use strict' });
|
83 | ```
|
84 |
|
85 | ```javascript
|
86 | var source = 'function hello () { var foo = "bar"; return "world"; }';
|
87 | var ast = new AbstractSyntaxTree(source);
|
88 | ast.remove('BlockStatement > VariableDeclaration');
|
89 | ```
|
90 |
|
91 | ### walk
|
92 |
|
93 | Walks over all nodes
|
94 |
|
95 | ```javascript
|
96 | var source = 'var a = 1';
|
97 | var ast = new AbstractSyntaxTree(source);
|
98 | ast.walk((node, parent) => {
|
99 | console.log(node, parent);
|
100 | });
|
101 | ```
|
102 |
|
103 | ### traverse
|
104 |
|
105 | Walks over all nodes
|
106 |
|
107 | ```javascript
|
108 | var source = 'var a = 1';
|
109 | var ast = new AbstractSyntaxTree(source);
|
110 | ast.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 |
|
122 | Replace all nodes that match the criteria.
|
123 |
|
124 | ```javascript
|
125 | var source = 'var a = 1';
|
126 | var ast = new AbstractSyntaxTree(source);
|
127 | ast.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 |
|
139 | Prepend a node to the body.
|
140 |
|
141 | ```javascript
|
142 | var source = 'var a = 1;';
|
143 | var ast = new AbstractSyntaxTree(source);
|
144 | ast.prepend({
|
145 | type: 'ExpressionStatement',
|
146 | expression: {
|
147 | type: 'Literal',
|
148 | value: 'use strict'
|
149 | }
|
150 | });
|
151 | ```
|
152 |
|
153 | ### append
|
154 |
|
155 | Append a node to the body.
|
156 |
|
157 | ```javascript
|
158 | var source = 'var a = 1;';
|
159 | var ast = new AbstractSyntaxTree(source);
|
160 | ast.append({
|
161 | type: 'ExpressionStatement',
|
162 | expression: {
|
163 | type: 'Literal',
|
164 | value: 'test'
|
165 | }
|
166 | });
|
167 | ```
|
168 |
|
169 | ### wrap
|
170 |
|
171 | Wrap body with given node.
|
172 |
|
173 | ```javascript
|
174 | var source = 'var a = 1;';
|
175 | var ast = new AbstractSyntaxTree(source);
|
176 | ast.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 |
|
204 | Change the code to the first BlockStatement body
|
205 |
|
206 | ```javascript
|
207 | var source = '(function () { console.log(1); }())';
|
208 | var ast = new AbstractSyntaxTree(source);
|
209 | ast.unwrap();
|
210 | ast.toSource();
|
211 | ```
|
212 |
|
213 | ### template
|
214 |
|
215 | Create ast partials from templates
|
216 |
|
217 | ```javascript
|
218 | var source = 'console.log(1);';
|
219 | var ast = new AbstractSyntaxTree(source);
|
220 | ast.template('var foo = <%= bar %>;' { bar: { type: 'Literal', value: 1 } });
|
221 | ```
|
222 |
|
223 | ### toSource / toString
|
224 |
|
225 | Convert the ast to string.
|
226 |
|
227 | ```javascript
|
228 | var source = 'var a = 1;';
|
229 | var ast = new AbstractSyntaxTree(source);
|
230 | ast.toSource();
|
231 | ```
|