-
Notifications
You must be signed in to change notification settings - Fork 12
/
astutil.js
135 lines (105 loc) · 3.2 KB
/
astutil.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
astutil = Object.create(null);
astutil.Context = function () {
this.isAborted = false;
this.stack = [];
this.parent = null;
this.key = null;
};
astutil.Context.prototype.abort = function () {
this.isAborted = true;
};
astutil.Context.prototype.onCycleDetected = function (target) {
};
astutil.Result = function () {
this.parent = null;
this.key = null;
this.node = null;
};
astutil.Result.prototype.set = function (context, node) {
this.parent = context.parent;
this.key = context.key;
this.node = node;
};
astutil.Result.prototype.remove = function () {
if (!this.parent || (this.key === null))
return false;
if (Array.isArray(this.parent)) {
this.parent.splice(this.key, 1);
} else {
delete this.parent[this.key];
}
this.key = null;
return true;
};
astutil.Result.prototype.replaceWith = function (newNode) {
this.parent[this.key] = newNode;
};
astutil.clone = function (ast) {
return JSON.parse(JSON.stringify(ast));
};
// Mutates an AST subtree, partially in-place.
// Returns the new root (typically the original root, but it may have been replaced.)
// If you don't want to modify any of the original nodes, make a copy first.
astutil.mutate = function (root, mutator, context) {
if (!context)
context = new astutil.Context();
context.stack.push(root);
try {
var newRoot = root;
if (mutator)
newRoot = mutator(context, root);
if (typeof (newRoot) === "undefined")
newRoot = root;
if (context.isAborted)
return newRoot;
for (var k in newRoot) {
if (context.isAborted)
return newRoot;
if (!newRoot.hasOwnProperty(k))
continue;
var v = newRoot[k];
if (typeof(v) !== "object")
continue;
if (context.stack.indexOf(v) >= 0) {
context.onCycleDetected(v);
continue;
}
context.parent = newRoot;
context.key = k;
var newValue = astutil.mutate(v, mutator, context);
if (typeof (newValue) === "undefined") {
/* do nothing */ ;
} else if (newValue !== v) {
/* replace */
newRoot[k] = newValue;
}
}
return newRoot;
} finally {
context.stack.pop();
}
};
astutil.find = function (root, predicate) {
var result = new astutil.Result();
var _predicate = predicate;
if (_predicate.length === 1)
_predicate = function (context, node) {
return predicate(node);
};
astutil.mutate(root, function (context, node) {
if (_predicate(context, node)) {
result.set(context, node);
context.abort();
}
});
return result;
};
astutil.assertNoCycles = function (root) {
var context = new astutil.Context();
context.onCycleDetected = function (target) {
console.log(context.stack.map(function (n) { return n.type; }));
console.log("Cycle pointing at", target);
throw new Error("Cycle detected in AST");
};
astutil.mutate(root, null, context);
};