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