You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

equiv.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";;
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var types_1 = __importDefault(require("./types"));
  7. function default_1(fork) {
  8. var types = fork.use(types_1.default);
  9. var getFieldNames = types.getFieldNames;
  10. var getFieldValue = types.getFieldValue;
  11. var isArray = types.builtInTypes.array;
  12. var isObject = types.builtInTypes.object;
  13. var isDate = types.builtInTypes.Date;
  14. var isRegExp = types.builtInTypes.RegExp;
  15. var hasOwn = Object.prototype.hasOwnProperty;
  16. function astNodesAreEquivalent(a, b, problemPath) {
  17. if (isArray.check(problemPath)) {
  18. problemPath.length = 0;
  19. }
  20. else {
  21. problemPath = null;
  22. }
  23. return areEquivalent(a, b, problemPath);
  24. }
  25. astNodesAreEquivalent.assert = function (a, b) {
  26. var problemPath = [];
  27. if (!astNodesAreEquivalent(a, b, problemPath)) {
  28. if (problemPath.length === 0) {
  29. if (a !== b) {
  30. throw new Error("Nodes must be equal");
  31. }
  32. }
  33. else {
  34. throw new Error("Nodes differ in the following path: " +
  35. problemPath.map(subscriptForProperty).join(""));
  36. }
  37. }
  38. };
  39. function subscriptForProperty(property) {
  40. if (/[_$a-z][_$a-z0-9]*/i.test(property)) {
  41. return "." + property;
  42. }
  43. return "[" + JSON.stringify(property) + "]";
  44. }
  45. function areEquivalent(a, b, problemPath) {
  46. if (a === b) {
  47. return true;
  48. }
  49. if (isArray.check(a)) {
  50. return arraysAreEquivalent(a, b, problemPath);
  51. }
  52. if (isObject.check(a)) {
  53. return objectsAreEquivalent(a, b, problemPath);
  54. }
  55. if (isDate.check(a)) {
  56. return isDate.check(b) && (+a === +b);
  57. }
  58. if (isRegExp.check(a)) {
  59. return isRegExp.check(b) && (a.source === b.source &&
  60. a.global === b.global &&
  61. a.multiline === b.multiline &&
  62. a.ignoreCase === b.ignoreCase);
  63. }
  64. return a == b;
  65. }
  66. function arraysAreEquivalent(a, b, problemPath) {
  67. isArray.assert(a);
  68. var aLength = a.length;
  69. if (!isArray.check(b) || b.length !== aLength) {
  70. if (problemPath) {
  71. problemPath.push("length");
  72. }
  73. return false;
  74. }
  75. for (var i = 0; i < aLength; ++i) {
  76. if (problemPath) {
  77. problemPath.push(i);
  78. }
  79. if (i in a !== i in b) {
  80. return false;
  81. }
  82. if (!areEquivalent(a[i], b[i], problemPath)) {
  83. return false;
  84. }
  85. if (problemPath) {
  86. var problemPathTail = problemPath.pop();
  87. if (problemPathTail !== i) {
  88. throw new Error("" + problemPathTail);
  89. }
  90. }
  91. }
  92. return true;
  93. }
  94. function objectsAreEquivalent(a, b, problemPath) {
  95. isObject.assert(a);
  96. if (!isObject.check(b)) {
  97. return false;
  98. }
  99. // Fast path for a common property of AST nodes.
  100. if (a.type !== b.type) {
  101. if (problemPath) {
  102. problemPath.push("type");
  103. }
  104. return false;
  105. }
  106. var aNames = getFieldNames(a);
  107. var aNameCount = aNames.length;
  108. var bNames = getFieldNames(b);
  109. var bNameCount = bNames.length;
  110. if (aNameCount === bNameCount) {
  111. for (var i = 0; i < aNameCount; ++i) {
  112. var name = aNames[i];
  113. var aChild = getFieldValue(a, name);
  114. var bChild = getFieldValue(b, name);
  115. if (problemPath) {
  116. problemPath.push(name);
  117. }
  118. if (!areEquivalent(aChild, bChild, problemPath)) {
  119. return false;
  120. }
  121. if (problemPath) {
  122. var problemPathTail = problemPath.pop();
  123. if (problemPathTail !== name) {
  124. throw new Error("" + problemPathTail);
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. if (!problemPath) {
  131. return false;
  132. }
  133. // Since aNameCount !== bNameCount, we need to find some name that's
  134. // missing in aNames but present in bNames, or vice-versa.
  135. var seenNames = Object.create(null);
  136. for (i = 0; i < aNameCount; ++i) {
  137. seenNames[aNames[i]] = true;
  138. }
  139. for (i = 0; i < bNameCount; ++i) {
  140. name = bNames[i];
  141. if (!hasOwn.call(seenNames, name)) {
  142. problemPath.push(name);
  143. return false;
  144. }
  145. delete seenNames[name];
  146. }
  147. for (name in seenNames) {
  148. problemPath.push(name);
  149. break;
  150. }
  151. return false;
  152. }
  153. return astNodesAreEquivalent;
  154. }
  155. exports.default = default_1;
  156. module.exports = exports["default"];