equivalent-to.js
8.1 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {
AST_Array,
AST_Atom,
AST_Await,
AST_BigInt,
AST_Binary,
AST_Block,
AST_Call,
AST_Catch,
AST_Chain,
AST_Class,
AST_ClassProperty,
AST_ConciseMethod,
AST_Conditional,
AST_Debugger,
AST_Definitions,
AST_Destructuring,
AST_Directive,
AST_Do,
AST_Dot,
AST_DotHash,
AST_EmptyStatement,
AST_Expansion,
AST_Export,
AST_Finally,
AST_For,
AST_ForIn,
AST_ForOf,
AST_If,
AST_Import,
AST_ImportMeta,
AST_Jump,
AST_LabeledStatement,
AST_Lambda,
AST_LoopControl,
AST_NameMapping,
AST_NewTarget,
AST_Node,
AST_Number,
AST_Object,
AST_ObjectGetter,
AST_ObjectKeyVal,
AST_ObjectProperty,
AST_ObjectSetter,
AST_PrefixedTemplateString,
AST_PropAccess,
AST_RegExp,
AST_Sequence,
AST_SimpleStatement,
AST_String,
AST_Super,
AST_Switch,
AST_SwitchBranch,
AST_Symbol,
AST_TemplateSegment,
AST_TemplateString,
AST_This,
AST_Toplevel,
AST_Try,
AST_Unary,
AST_VarDef,
AST_While,
AST_With,
AST_Yield
} from "./ast.js";
const shallow_cmp = (node1, node2) => {
return (
node1 === null && node2 === null
|| node1.TYPE === node2.TYPE && node1.shallow_cmp(node2)
);
};
export const equivalent_to = (tree1, tree2) => {
if (!shallow_cmp(tree1, tree2)) return false;
const walk_1_state = [tree1];
const walk_2_state = [tree2];
const walk_1_push = walk_1_state.push.bind(walk_1_state);
const walk_2_push = walk_2_state.push.bind(walk_2_state);
while (walk_1_state.length && walk_2_state.length) {
const node_1 = walk_1_state.pop();
const node_2 = walk_2_state.pop();
if (!shallow_cmp(node_1, node_2)) return false;
node_1._children_backwards(walk_1_push);
node_2._children_backwards(walk_2_push);
if (walk_1_state.length !== walk_2_state.length) {
// Different number of children
return false;
}
}
return walk_1_state.length == 0 && walk_2_state.length == 0;
};
const pass_through = () => true;
AST_Node.prototype.shallow_cmp = function () {
throw new Error("did not find a shallow_cmp function for " + this.constructor.name);
};
AST_Debugger.prototype.shallow_cmp = pass_through;
AST_Directive.prototype.shallow_cmp = function(other) {
return this.value === other.value;
};
AST_SimpleStatement.prototype.shallow_cmp = pass_through;
AST_Block.prototype.shallow_cmp = pass_through;
AST_EmptyStatement.prototype.shallow_cmp = pass_through;
AST_LabeledStatement.prototype.shallow_cmp = function(other) {
return this.label.name === other.label.name;
};
AST_Do.prototype.shallow_cmp = pass_through;
AST_While.prototype.shallow_cmp = pass_through;
AST_For.prototype.shallow_cmp = function(other) {
return (this.init == null ? other.init == null : this.init === other.init) && (this.condition == null ? other.condition == null : this.condition === other.condition) && (this.step == null ? other.step == null : this.step === other.step);
};
AST_ForIn.prototype.shallow_cmp = pass_through;
AST_ForOf.prototype.shallow_cmp = pass_through;
AST_With.prototype.shallow_cmp = pass_through;
AST_Toplevel.prototype.shallow_cmp = pass_through;
AST_Expansion.prototype.shallow_cmp = pass_through;
AST_Lambda.prototype.shallow_cmp = function(other) {
return this.is_generator === other.is_generator && this.async === other.async;
};
AST_Destructuring.prototype.shallow_cmp = function(other) {
return this.is_array === other.is_array;
};
AST_PrefixedTemplateString.prototype.shallow_cmp = pass_through;
AST_TemplateString.prototype.shallow_cmp = pass_through;
AST_TemplateSegment.prototype.shallow_cmp = function(other) {
return this.value === other.value;
};
AST_Jump.prototype.shallow_cmp = pass_through;
AST_LoopControl.prototype.shallow_cmp = pass_through;
AST_Await.prototype.shallow_cmp = pass_through;
AST_Yield.prototype.shallow_cmp = function(other) {
return this.is_star === other.is_star;
};
AST_If.prototype.shallow_cmp = function(other) {
return this.alternative == null ? other.alternative == null : this.alternative === other.alternative;
};
AST_Switch.prototype.shallow_cmp = pass_through;
AST_SwitchBranch.prototype.shallow_cmp = pass_through;
AST_Try.prototype.shallow_cmp = function(other) {
return (this.body === other.body) && (this.bcatch == null ? other.bcatch == null : this.bcatch === other.bcatch) && (this.bfinally == null ? other.bfinally == null : this.bfinally === other.bfinally);
};
AST_Catch.prototype.shallow_cmp = function(other) {
return this.argname == null ? other.argname == null : this.argname === other.argname;
};
AST_Finally.prototype.shallow_cmp = pass_through;
AST_Definitions.prototype.shallow_cmp = pass_through;
AST_VarDef.prototype.shallow_cmp = function(other) {
return this.value == null ? other.value == null : this.value === other.value;
};
AST_NameMapping.prototype.shallow_cmp = pass_through;
AST_Import.prototype.shallow_cmp = function(other) {
return (this.imported_name == null ? other.imported_name == null : this.imported_name === other.imported_name) && (this.imported_names == null ? other.imported_names == null : this.imported_names === other.imported_names);
};
AST_ImportMeta.prototype.shallow_cmp = pass_through;
AST_Export.prototype.shallow_cmp = function(other) {
return (this.exported_definition == null ? other.exported_definition == null : this.exported_definition === other.exported_definition) && (this.exported_value == null ? other.exported_value == null : this.exported_value === other.exported_value) && (this.exported_names == null ? other.exported_names == null : this.exported_names === other.exported_names) && this.module_name === other.module_name && this.is_default === other.is_default;
};
AST_Call.prototype.shallow_cmp = pass_through;
AST_Sequence.prototype.shallow_cmp = pass_through;
AST_PropAccess.prototype.shallow_cmp = pass_through;
AST_Chain.prototype.shallow_cmp = pass_through;
AST_Dot.prototype.shallow_cmp = function(other) {
return this.property === other.property;
};
AST_DotHash.prototype.shallow_cmp = function(other) {
return this.property === other.property;
};
AST_Unary.prototype.shallow_cmp = function(other) {
return this.operator === other.operator;
};
AST_Binary.prototype.shallow_cmp = function(other) {
return this.operator === other.operator;
};
AST_Conditional.prototype.shallow_cmp = pass_through;
AST_Array.prototype.shallow_cmp = pass_through;
AST_Object.prototype.shallow_cmp = pass_through;
AST_ObjectProperty.prototype.shallow_cmp = pass_through;
AST_ObjectKeyVal.prototype.shallow_cmp = function(other) {
return this.key === other.key;
};
AST_ObjectSetter.prototype.shallow_cmp = function(other) {
return this.static === other.static;
};
AST_ObjectGetter.prototype.shallow_cmp = function(other) {
return this.static === other.static;
};
AST_ConciseMethod.prototype.shallow_cmp = function(other) {
return this.static === other.static && this.is_generator === other.is_generator && this.async === other.async;
};
AST_Class.prototype.shallow_cmp = function(other) {
return (this.name == null ? other.name == null : this.name === other.name) && (this.extends == null ? other.extends == null : this.extends === other.extends);
};
AST_ClassProperty.prototype.shallow_cmp = function(other) {
return this.static === other.static;
};
AST_Symbol.prototype.shallow_cmp = function(other) {
return this.name === other.name;
};
AST_NewTarget.prototype.shallow_cmp = pass_through;
AST_This.prototype.shallow_cmp = pass_through;
AST_Super.prototype.shallow_cmp = pass_through;
AST_String.prototype.shallow_cmp = function(other) {
return this.value === other.value;
};
AST_Number.prototype.shallow_cmp = function(other) {
return this.value === other.value;
};
AST_BigInt.prototype.shallow_cmp = function(other) {
return this.value === other.value;
};
AST_RegExp.prototype.shallow_cmp = function (other) {
return (
this.value.flags === other.value.flags
&& this.value.source === other.value.source
);
};
AST_Atom.prototype.shallow_cmp = pass_through;