source: imaps-frontend/node_modules/terser/lib/equivalent-to.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 8.1 KB
Line 
1import {
2 AST_Array,
3 AST_Atom,
4 AST_Await,
5 AST_BigInt,
6 AST_Binary,
7 AST_Block,
8 AST_Call,
9 AST_Catch,
10 AST_Chain,
11 AST_Class,
12 AST_ClassProperty,
13 AST_ConciseMethod,
14 AST_Conditional,
15 AST_Debugger,
16 AST_Definitions,
17 AST_Destructuring,
18 AST_Directive,
19 AST_Do,
20 AST_Dot,
21 AST_DotHash,
22 AST_EmptyStatement,
23 AST_Expansion,
24 AST_Export,
25 AST_Finally,
26 AST_For,
27 AST_ForIn,
28 AST_ForOf,
29 AST_If,
30 AST_Import,
31 AST_ImportMeta,
32 AST_Jump,
33 AST_LabeledStatement,
34 AST_Lambda,
35 AST_LoopControl,
36 AST_NameMapping,
37 AST_NewTarget,
38 AST_Node,
39 AST_Number,
40 AST_Object,
41 AST_ObjectGetter,
42 AST_ObjectKeyVal,
43 AST_ObjectProperty,
44 AST_ObjectSetter,
45 AST_PrefixedTemplateString,
46 AST_PropAccess,
47 AST_RegExp,
48 AST_Sequence,
49 AST_SimpleStatement,
50 AST_String,
51 AST_Super,
52 AST_Switch,
53 AST_SwitchBranch,
54 AST_Symbol,
55 AST_TemplateSegment,
56 AST_TemplateString,
57 AST_This,
58 AST_Toplevel,
59 AST_Try,
60 AST_Unary,
61 AST_VarDef,
62 AST_While,
63 AST_With,
64 AST_Yield
65} from "./ast.js";
66
67const shallow_cmp = (node1, node2) => {
68 return (
69 node1 === null && node2 === null
70 || node1.TYPE === node2.TYPE && node1.shallow_cmp(node2)
71 );
72};
73
74export const equivalent_to = (tree1, tree2) => {
75 if (!shallow_cmp(tree1, tree2)) return false;
76 const walk_1_state = [tree1];
77 const walk_2_state = [tree2];
78
79 const walk_1_push = walk_1_state.push.bind(walk_1_state);
80 const walk_2_push = walk_2_state.push.bind(walk_2_state);
81
82 while (walk_1_state.length && walk_2_state.length) {
83 const node_1 = walk_1_state.pop();
84 const node_2 = walk_2_state.pop();
85
86 if (!shallow_cmp(node_1, node_2)) return false;
87
88 node_1._children_backwards(walk_1_push);
89 node_2._children_backwards(walk_2_push);
90
91 if (walk_1_state.length !== walk_2_state.length) {
92 // Different number of children
93 return false;
94 }
95 }
96
97 return walk_1_state.length == 0 && walk_2_state.length == 0;
98};
99
100const pass_through = () => true;
101
102AST_Node.prototype.shallow_cmp = function () {
103 throw new Error("did not find a shallow_cmp function for " + this.constructor.name);
104};
105
106AST_Debugger.prototype.shallow_cmp = pass_through;
107
108AST_Directive.prototype.shallow_cmp = function(other) {
109 return this.value === other.value;
110};
111
112AST_SimpleStatement.prototype.shallow_cmp = pass_through;
113
114AST_Block.prototype.shallow_cmp = pass_through;
115
116AST_EmptyStatement.prototype.shallow_cmp = pass_through;
117
118AST_LabeledStatement.prototype.shallow_cmp = function(other) {
119 return this.label.name === other.label.name;
120};
121
122AST_Do.prototype.shallow_cmp = pass_through;
123
124AST_While.prototype.shallow_cmp = pass_through;
125
126AST_For.prototype.shallow_cmp = function(other) {
127 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);
128};
129
130AST_ForIn.prototype.shallow_cmp = pass_through;
131
132AST_ForOf.prototype.shallow_cmp = pass_through;
133
134AST_With.prototype.shallow_cmp = pass_through;
135
136AST_Toplevel.prototype.shallow_cmp = pass_through;
137
138AST_Expansion.prototype.shallow_cmp = pass_through;
139
140AST_Lambda.prototype.shallow_cmp = function(other) {
141 return this.is_generator === other.is_generator && this.async === other.async;
142};
143
144AST_Destructuring.prototype.shallow_cmp = function(other) {
145 return this.is_array === other.is_array;
146};
147
148AST_PrefixedTemplateString.prototype.shallow_cmp = pass_through;
149
150AST_TemplateString.prototype.shallow_cmp = pass_through;
151
152AST_TemplateSegment.prototype.shallow_cmp = function(other) {
153 return this.value === other.value;
154};
155
156AST_Jump.prototype.shallow_cmp = pass_through;
157
158AST_LoopControl.prototype.shallow_cmp = pass_through;
159
160AST_Await.prototype.shallow_cmp = pass_through;
161
162AST_Yield.prototype.shallow_cmp = function(other) {
163 return this.is_star === other.is_star;
164};
165
166AST_If.prototype.shallow_cmp = function(other) {
167 return this.alternative == null ? other.alternative == null : this.alternative === other.alternative;
168};
169
170AST_Switch.prototype.shallow_cmp = pass_through;
171
172AST_SwitchBranch.prototype.shallow_cmp = pass_through;
173
174AST_Try.prototype.shallow_cmp = function(other) {
175 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);
176};
177
178AST_Catch.prototype.shallow_cmp = function(other) {
179 return this.argname == null ? other.argname == null : this.argname === other.argname;
180};
181
182AST_Finally.prototype.shallow_cmp = pass_through;
183
184AST_Definitions.prototype.shallow_cmp = pass_through;
185
186AST_VarDef.prototype.shallow_cmp = function(other) {
187 return this.value == null ? other.value == null : this.value === other.value;
188};
189
190AST_NameMapping.prototype.shallow_cmp = pass_through;
191
192AST_Import.prototype.shallow_cmp = function(other) {
193 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);
194};
195
196AST_ImportMeta.prototype.shallow_cmp = pass_through;
197
198AST_Export.prototype.shallow_cmp = function(other) {
199 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;
200};
201
202AST_Call.prototype.shallow_cmp = pass_through;
203
204AST_Sequence.prototype.shallow_cmp = pass_through;
205
206AST_PropAccess.prototype.shallow_cmp = pass_through;
207
208AST_Chain.prototype.shallow_cmp = pass_through;
209
210AST_Dot.prototype.shallow_cmp = function(other) {
211 return this.property === other.property;
212};
213
214AST_DotHash.prototype.shallow_cmp = function(other) {
215 return this.property === other.property;
216};
217
218AST_Unary.prototype.shallow_cmp = function(other) {
219 return this.operator === other.operator;
220};
221
222AST_Binary.prototype.shallow_cmp = function(other) {
223 return this.operator === other.operator;
224};
225
226AST_Conditional.prototype.shallow_cmp = pass_through;
227
228AST_Array.prototype.shallow_cmp = pass_through;
229
230AST_Object.prototype.shallow_cmp = pass_through;
231
232AST_ObjectProperty.prototype.shallow_cmp = pass_through;
233
234AST_ObjectKeyVal.prototype.shallow_cmp = function(other) {
235 return this.key === other.key;
236};
237
238AST_ObjectSetter.prototype.shallow_cmp = function(other) {
239 return this.static === other.static;
240};
241
242AST_ObjectGetter.prototype.shallow_cmp = function(other) {
243 return this.static === other.static;
244};
245
246AST_ConciseMethod.prototype.shallow_cmp = function(other) {
247 return this.static === other.static && this.is_generator === other.is_generator && this.async === other.async;
248};
249
250AST_Class.prototype.shallow_cmp = function(other) {
251 return (this.name == null ? other.name == null : this.name === other.name) && (this.extends == null ? other.extends == null : this.extends === other.extends);
252};
253
254AST_ClassProperty.prototype.shallow_cmp = function(other) {
255 return this.static === other.static;
256};
257
258AST_Symbol.prototype.shallow_cmp = function(other) {
259 return this.name === other.name;
260};
261
262AST_NewTarget.prototype.shallow_cmp = pass_through;
263
264AST_This.prototype.shallow_cmp = pass_through;
265
266AST_Super.prototype.shallow_cmp = pass_through;
267
268AST_String.prototype.shallow_cmp = function(other) {
269 return this.value === other.value;
270};
271
272AST_Number.prototype.shallow_cmp = function(other) {
273 return this.value === other.value;
274};
275
276AST_BigInt.prototype.shallow_cmp = function(other) {
277 return this.value === other.value;
278};
279
280AST_RegExp.prototype.shallow_cmp = function (other) {
281 return (
282 this.value.flags === other.value.flags
283 && this.value.source === other.value.source
284 );
285};
286
287AST_Atom.prototype.shallow_cmp = pass_through;
Note: See TracBrowser for help on using the repository browser.