Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81164 views
1
var types = require("./lib/types");
2
var parse = require("./lib/parser").parse;
3
var Printer = require("./lib/printer").Printer;
4
5
function print(node, options) {
6
return new Printer(options).print(node);
7
}
8
9
function prettyPrint(node, options) {
10
return new Printer(options).printGenerically(node);
11
}
12
13
function run(transformer, options) {
14
return runFile(process.argv[2], transformer, options);
15
}
16
17
function runFile(path, transformer, options) {
18
require("fs").readFile(path, "utf-8", function(err, code) {
19
if (err) {
20
console.error(err);
21
return;
22
}
23
24
runString(code, transformer, options);
25
});
26
}
27
28
function defaultWriteback(output) {
29
process.stdout.write(output);
30
}
31
32
function runString(code, transformer, options) {
33
var writeback = options && options.writeback || defaultWriteback;
34
transformer(parse(code, options), function(node) {
35
writeback(print(node, options).code);
36
});
37
}
38
39
Object.defineProperties(exports, {
40
/**
41
* Parse a string of code into an augmented syntax tree suitable for
42
* arbitrary modification and reprinting.
43
*/
44
parse: {
45
enumerable: true,
46
value: parse
47
},
48
49
/**
50
* Traverse and potentially modify an abstract syntax tree using a
51
* convenient visitor syntax:
52
*
53
* recast.visit(ast, {
54
* names: [],
55
* visitIdentifier: function(path) {
56
* var node = path.value;
57
* this.visitor.names.push(node.name);
58
* this.traverse(path);
59
* }
60
* });
61
*/
62
visit: {
63
enumerable: true,
64
value: types.visit
65
},
66
67
/**
68
* Reprint a modified syntax tree using as much of the original source
69
* code as possible.
70
*/
71
print: {
72
enumerable: true,
73
value: print
74
},
75
76
/**
77
* Print without attempting to reuse any original source code.
78
*/
79
prettyPrint: {
80
enumerable: false,
81
value: prettyPrint
82
},
83
84
/**
85
* Customized version of require("ast-types").
86
*/
87
types: {
88
enumerable: false,
89
value: types
90
},
91
92
/**
93
* Convenient command-line interface (see e.g. example/add-braces).
94
*/
95
run: {
96
enumerable: false,
97
value: run
98
}
99
});
100
101