Debian Codegraph @

example/add-braces

raw
1 #!/usr/bin/env node
2
3 var recast = require("recast");
4 var types = recast.types;
5 var n = types.namedTypes;
6 var b = types.builders;
7
8 require("recast").run(function(ast, callback) {
9 recast.visit(ast, {
10 visitIfStatement: function(path) {
11 var stmt = path.node;
12 stmt.consequent = fix(stmt.consequent);
13
14 var alt = stmt.alternate;
15 if (!n.IfStatement.check(alt)) {
16 stmt.alternate = fix(alt);
17 }
18
19 this.traverse(path);
20 },
21
22 visitWhileStatement: visitLoop,
23 visitForStatement: visitLoop,
24 visitForInStatement: visitLoop
25 });
26
27 callback(ast);
28 });
29
30 function visitLoop(path) {
31 var loop = path.node;
32 loop.body = fix(loop.body);
33 this.traverse(path);
34 }
35
36 function fix(clause) {
37 if (clause) {
38 if (!n.BlockStatement.check(clause)) {
39 clause = b.blockStatement([clause]);
40 }
41 }
42
43 return clause;
44 }