equivalent-to.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. const shallow_cmp = (node1, node2) => {
  71. return (
  72. node1 === null && node2 === null
  73. || node1.TYPE === node2.TYPE && node1.shallow_cmp(node2)
  74. );
  75. };
  76. export const equivalent_to = (tree1, tree2) => {
  77. if (!shallow_cmp(tree1, tree2)) return false;
  78. const walk_1_state = [tree1];
  79. const walk_2_state = [tree2];
  80. const walk_1_push = walk_1_state.push.bind(walk_1_state);
  81. const walk_2_push = walk_2_state.push.bind(walk_2_state);
  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. if (!shallow_cmp(node_1, node_2)) return false;
  86. node_1._children_backwards(walk_1_push);
  87. node_2._children_backwards(walk_2_push);
  88. if (walk_1_state.length !== walk_2_state.length) {
  89. // Different number of children
  90. return false;
  91. }
  92. }
  93. return walk_1_state.length == 0 && walk_2_state.length == 0;
  94. };
  95. const pass_through = () => true;
  96. AST_Node.prototype.shallow_cmp = function () {
  97. throw new Error("did not find a shallow_cmp function for " + this.constructor.name);
  98. };
  99. AST_Debugger.prototype.shallow_cmp = pass_through;
  100. AST_Directive.prototype.shallow_cmp = function(other) {
  101. return this.value === other.value;
  102. };
  103. AST_SimpleStatement.prototype.shallow_cmp = pass_through;
  104. AST_Block.prototype.shallow_cmp = pass_through;
  105. AST_EmptyStatement.prototype.shallow_cmp = pass_through;
  106. AST_LabeledStatement.prototype.shallow_cmp = function(other) {
  107. return this.label.name === other.label.name;
  108. };
  109. AST_Do.prototype.shallow_cmp = pass_through;
  110. AST_While.prototype.shallow_cmp = pass_through;
  111. AST_For.prototype.shallow_cmp = function(other) {
  112. 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);
  113. };
  114. AST_ForIn.prototype.shallow_cmp = pass_through;
  115. AST_ForOf.prototype.shallow_cmp = pass_through;
  116. AST_With.prototype.shallow_cmp = pass_through;
  117. AST_Toplevel.prototype.shallow_cmp = pass_through;
  118. AST_Expansion.prototype.shallow_cmp = pass_through;
  119. AST_Lambda.prototype.shallow_cmp = function(other) {
  120. return this.is_generator === other.is_generator && this.async === other.async;
  121. };
  122. AST_Destructuring.prototype.shallow_cmp = function(other) {
  123. return this.is_array === other.is_array;
  124. };
  125. AST_PrefixedTemplateString.prototype.shallow_cmp = pass_through;
  126. AST_TemplateString.prototype.shallow_cmp = pass_through;
  127. AST_TemplateSegment.prototype.shallow_cmp = function(other) {
  128. return this.value === other.value;
  129. };
  130. AST_Jump.prototype.shallow_cmp = pass_through;
  131. AST_LoopControl.prototype.shallow_cmp = pass_through;
  132. AST_Await.prototype.shallow_cmp = pass_through;
  133. AST_Yield.prototype.shallow_cmp = function(other) {
  134. return this.is_star === other.is_star;
  135. };
  136. AST_If.prototype.shallow_cmp = function(other) {
  137. return this.alternative == null ? other.alternative == null : this.alternative === other.alternative;
  138. };
  139. AST_Switch.prototype.shallow_cmp = pass_through;
  140. AST_SwitchBranch.prototype.shallow_cmp = pass_through;
  141. AST_Try.prototype.shallow_cmp = function(other) {
  142. 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);
  143. };
  144. AST_Catch.prototype.shallow_cmp = function(other) {
  145. return this.argname == null ? other.argname == null : this.argname === other.argname;
  146. };
  147. AST_Finally.prototype.shallow_cmp = pass_through;
  148. AST_DefinitionsLike.prototype.shallow_cmp = pass_through;
  149. AST_VarDefLike.prototype.shallow_cmp = function(other) {
  150. return this.value == null ? other.value == null : this.value === other.value;
  151. };
  152. AST_NameMapping.prototype.shallow_cmp = pass_through;
  153. AST_Import.prototype.shallow_cmp = function(other) {
  154. return (this.imported_name || null) === (other.imported_name || null)
  155. && (this.imported_names || null) === (other.imported_names || null)
  156. && (this.attributes || null) === (other.attributes || null)
  157. && (this.phase || null) === (other.phase || null);
  158. };
  159. AST_ImportMeta.prototype.shallow_cmp = pass_through;
  160. AST_DynamicImport.prototype.shallow_cmp = function(other) {
  161. return (this.phase || null) === (other.phase || null) && this.args.length === other.args.length;
  162. };
  163. AST_Export.prototype.shallow_cmp = function(other) {
  164. 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;
  165. };
  166. AST_Call.prototype.shallow_cmp = pass_through;
  167. AST_Sequence.prototype.shallow_cmp = pass_through;
  168. AST_PropAccess.prototype.shallow_cmp = pass_through;
  169. AST_Chain.prototype.shallow_cmp = pass_through;
  170. AST_Dot.prototype.shallow_cmp = function(other) {
  171. return (
  172. this.property === other.property
  173. && !!this.quote === !!other.quote
  174. );
  175. };
  176. AST_DotHash.prototype.shallow_cmp = function(other) {
  177. return this.property === other.property;
  178. };
  179. AST_Unary.prototype.shallow_cmp = function(other) {
  180. return this.operator === other.operator;
  181. };
  182. AST_Binary.prototype.shallow_cmp = function(other) {
  183. return this.operator === other.operator;
  184. };
  185. AST_PrivateIn.prototype.shallow_cmp = pass_through;
  186. AST_Conditional.prototype.shallow_cmp = pass_through;
  187. AST_Array.prototype.shallow_cmp = pass_through;
  188. AST_Object.prototype.shallow_cmp = pass_through;
  189. AST_ObjectProperty.prototype.shallow_cmp = pass_through;
  190. AST_ObjectKeyVal.prototype.shallow_cmp = function(other) {
  191. return this.key === other.key && this.quote === other.quote;
  192. };
  193. AST_ObjectSetter.prototype.shallow_cmp = function(other) {
  194. return this.static === other.static;
  195. };
  196. AST_ObjectGetter.prototype.shallow_cmp = function(other) {
  197. return this.static === other.static;
  198. };
  199. AST_ConciseMethod.prototype.shallow_cmp = function(other) {
  200. return this.static === other.static;
  201. };
  202. AST_PrivateMethod.prototype.shallow_cmp = function(other) {
  203. return this.static === other.static;
  204. };
  205. AST_Class.prototype.shallow_cmp = function(other) {
  206. return (this.name == null ? other.name == null : this.name === other.name) && (this.extends == null ? other.extends == null : this.extends === other.extends);
  207. };
  208. AST_ClassProperty.prototype.shallow_cmp = function(other) {
  209. return this.static === other.static
  210. && (typeof this.key === "string"
  211. ? this.key === other.key
  212. : true /* AST_Node handled elsewhere */);
  213. };
  214. AST_ClassPrivateProperty.prototype.shallow_cmp = function(other) {
  215. return this.static === other.static;
  216. };
  217. AST_Symbol.prototype.shallow_cmp = function(other) {
  218. return this.name === other.name;
  219. };
  220. AST_NewTarget.prototype.shallow_cmp = pass_through;
  221. AST_This.prototype.shallow_cmp = pass_through;
  222. AST_Super.prototype.shallow_cmp = pass_through;
  223. AST_String.prototype.shallow_cmp = function(other) {
  224. return this.value === other.value;
  225. };
  226. AST_Number.prototype.shallow_cmp = function(other) {
  227. return this.value === other.value;
  228. };
  229. AST_BigInt.prototype.shallow_cmp = function(other) {
  230. return this.value === other.value;
  231. };
  232. AST_RegExp.prototype.shallow_cmp = function (other) {
  233. return (
  234. this.value.flags === other.value.flags
  235. && this.value.source === other.value.source
  236. );
  237. };
  238. AST_Atom.prototype.shallow_cmp = pass_through;