equivalent-to.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_ConciseMethod,
  14. AST_Conditional,
  15. AST_Debugger,
  16. AST_Definitions,
  17. AST_Destructuring,
  18. AST_Directive,
  19. AST_Do,
  20. AST_Dot,
  21. AST_EmptyStatement,
  22. AST_Expansion,
  23. AST_Export,
  24. AST_Finally,
  25. AST_For,
  26. AST_ForIn,
  27. AST_ForOf,
  28. AST_If,
  29. AST_Import,
  30. AST_ImportMeta,
  31. AST_Jump,
  32. AST_LabeledStatement,
  33. AST_Lambda,
  34. AST_LoopControl,
  35. AST_NameMapping,
  36. AST_NewTarget,
  37. AST_Node,
  38. AST_Number,
  39. AST_Object,
  40. AST_ObjectGetter,
  41. AST_ObjectKeyVal,
  42. AST_ObjectProperty,
  43. AST_ObjectSetter,
  44. AST_PrefixedTemplateString,
  45. AST_PropAccess,
  46. AST_RegExp,
  47. AST_Sequence,
  48. AST_SimpleStatement,
  49. AST_String,
  50. AST_Super,
  51. AST_Switch,
  52. AST_SwitchBranch,
  53. AST_Symbol,
  54. AST_TemplateSegment,
  55. AST_TemplateString,
  56. AST_This,
  57. AST_Toplevel,
  58. AST_Try,
  59. AST_Unary,
  60. AST_VarDef,
  61. AST_While,
  62. AST_With,
  63. AST_Yield
  64. } from "./ast.js";
  65. const shallow_cmp = (node1, node2) => {
  66. return (
  67. node1 === null && node2 === null
  68. || node1.TYPE === node2.TYPE && node1.shallow_cmp(node2)
  69. );
  70. };
  71. export const equivalent_to = (tree1, tree2) => {
  72. if (!shallow_cmp(tree1, tree2)) return false;
  73. const walk_1_state = [tree1];
  74. const walk_2_state = [tree2];
  75. const walk_1_push = walk_1_state.push.bind(walk_1_state);
  76. const walk_2_push = walk_2_state.push.bind(walk_2_state);
  77. while (walk_1_state.length && walk_2_state.length) {
  78. const node_1 = walk_1_state.pop();
  79. const node_2 = walk_2_state.pop();
  80. if (!shallow_cmp(node_1, node_2)) return false;
  81. node_1._children_backwards(walk_1_push);
  82. node_2._children_backwards(walk_2_push);
  83. if (walk_1_state.length !== walk_2_state.length) {
  84. // Different number of children
  85. return false;
  86. }
  87. }
  88. return walk_1_state.length == 0 && walk_2_state.length == 0;
  89. };
  90. // Creates a shallow compare function
  91. const mkshallow = (props) => {
  92. const comparisons = Object
  93. .keys(props)
  94. .map(key => {
  95. if (props[key] === "eq") {
  96. return `this.${key} === other.${key}`;
  97. } else if (props[key] === "exist") {
  98. return `(this.${key} == null ? other.${key} == null : this.${key} === other.${key})`;
  99. } else {
  100. throw new Error(`mkshallow: Unexpected instruction: ${props[key]}`);
  101. }
  102. })
  103. .join(" && ");
  104. return new Function("other", "return " + comparisons);
  105. };
  106. const pass_through = () => true;
  107. AST_Node.prototype.shallow_cmp = function () {
  108. throw new Error("did not find a shallow_cmp function for " + this.constructor.name);
  109. };
  110. AST_Debugger.prototype.shallow_cmp = pass_through;
  111. AST_Directive.prototype.shallow_cmp = mkshallow({ value: "eq" });
  112. AST_SimpleStatement.prototype.shallow_cmp = pass_through;
  113. AST_Block.prototype.shallow_cmp = pass_through;
  114. AST_EmptyStatement.prototype.shallow_cmp = pass_through;
  115. AST_LabeledStatement.prototype.shallow_cmp = mkshallow({ "label.name": "eq" });
  116. AST_Do.prototype.shallow_cmp = pass_through;
  117. AST_While.prototype.shallow_cmp = pass_through;
  118. AST_For.prototype.shallow_cmp = mkshallow({
  119. init: "exist",
  120. condition: "exist",
  121. step: "exist"
  122. });
  123. AST_ForIn.prototype.shallow_cmp = pass_through;
  124. AST_ForOf.prototype.shallow_cmp = pass_through;
  125. AST_With.prototype.shallow_cmp = pass_through;
  126. AST_Toplevel.prototype.shallow_cmp = pass_through;
  127. AST_Expansion.prototype.shallow_cmp = pass_through;
  128. AST_Lambda.prototype.shallow_cmp = mkshallow({
  129. is_generator: "eq",
  130. async: "eq"
  131. });
  132. AST_Destructuring.prototype.shallow_cmp = mkshallow({
  133. is_array: "eq"
  134. });
  135. AST_PrefixedTemplateString.prototype.shallow_cmp = pass_through;
  136. AST_TemplateString.prototype.shallow_cmp = pass_through;
  137. AST_TemplateSegment.prototype.shallow_cmp = mkshallow({
  138. "value": "eq"
  139. });
  140. AST_Jump.prototype.shallow_cmp = pass_through;
  141. AST_LoopControl.prototype.shallow_cmp = pass_through;
  142. AST_Await.prototype.shallow_cmp = pass_through;
  143. AST_Yield.prototype.shallow_cmp = mkshallow({
  144. is_star: "eq"
  145. });
  146. AST_If.prototype.shallow_cmp = mkshallow({
  147. alternative: "exist"
  148. });
  149. AST_Switch.prototype.shallow_cmp = pass_through;
  150. AST_SwitchBranch.prototype.shallow_cmp = pass_through;
  151. AST_Try.prototype.shallow_cmp = mkshallow({
  152. bcatch: "exist",
  153. bfinally: "exist"
  154. });
  155. AST_Catch.prototype.shallow_cmp = mkshallow({
  156. argname: "exist"
  157. });
  158. AST_Finally.prototype.shallow_cmp = pass_through;
  159. AST_Definitions.prototype.shallow_cmp = pass_through;
  160. AST_VarDef.prototype.shallow_cmp = mkshallow({
  161. value: "exist"
  162. });
  163. AST_NameMapping.prototype.shallow_cmp = pass_through;
  164. AST_Import.prototype.shallow_cmp = mkshallow({
  165. imported_name: "exist",
  166. imported_names: "exist"
  167. });
  168. AST_ImportMeta.prototype.shallow_cmp = pass_through;
  169. AST_Export.prototype.shallow_cmp = mkshallow({
  170. exported_definition: "exist",
  171. exported_value: "exist",
  172. exported_names: "exist",
  173. module_name: "eq",
  174. is_default: "eq",
  175. });
  176. AST_Call.prototype.shallow_cmp = pass_through;
  177. AST_Sequence.prototype.shallow_cmp = pass_through;
  178. AST_PropAccess.prototype.shallow_cmp = pass_through;
  179. AST_Chain.prototype.shallow_cmp = pass_through;
  180. AST_Dot.prototype.shallow_cmp = mkshallow({
  181. property: "eq"
  182. });
  183. AST_Unary.prototype.shallow_cmp = mkshallow({
  184. operator: "eq"
  185. });
  186. AST_Binary.prototype.shallow_cmp = mkshallow({
  187. operator: "eq"
  188. });
  189. AST_Conditional.prototype.shallow_cmp = pass_through;
  190. AST_Array.prototype.shallow_cmp = pass_through;
  191. AST_Object.prototype.shallow_cmp = pass_through;
  192. AST_ObjectProperty.prototype.shallow_cmp = pass_through;
  193. AST_ObjectKeyVal.prototype.shallow_cmp = mkshallow({
  194. key: "eq"
  195. });
  196. AST_ObjectSetter.prototype.shallow_cmp = mkshallow({
  197. static: "eq"
  198. });
  199. AST_ObjectGetter.prototype.shallow_cmp = mkshallow({
  200. static: "eq"
  201. });
  202. AST_ConciseMethod.prototype.shallow_cmp = mkshallow({
  203. static: "eq",
  204. is_generator: "eq",
  205. async: "eq",
  206. });
  207. AST_Class.prototype.shallow_cmp = mkshallow({
  208. name: "exist",
  209. extends: "exist",
  210. });
  211. AST_ClassProperty.prototype.shallow_cmp = mkshallow({
  212. static: "eq"
  213. });
  214. AST_Symbol.prototype.shallow_cmp = mkshallow({
  215. name: "eq"
  216. });
  217. AST_NewTarget.prototype.shallow_cmp = pass_through;
  218. AST_This.prototype.shallow_cmp = pass_through;
  219. AST_Super.prototype.shallow_cmp = pass_through;
  220. AST_String.prototype.shallow_cmp = mkshallow({
  221. value: "eq"
  222. });
  223. AST_Number.prototype.shallow_cmp = mkshallow({
  224. value: "eq"
  225. });
  226. AST_BigInt.prototype.shallow_cmp = mkshallow({
  227. value: "eq"
  228. });
  229. AST_RegExp.prototype.shallow_cmp = function (other) {
  230. return (
  231. this.value.flags === other.value.flags
  232. && this.value.source === other.value.source
  233. );
  234. };
  235. AST_Atom.prototype.shallow_cmp = pass_through;