joinAlignedDiffs.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.joinAlignedDiffsExpand = exports.joinAlignedDiffsNoExpand = void 0;
  6. var _cleanupSemantic = require('./cleanupSemantic');
  7. var _printDiffs = require('./printDiffs');
  8. /**
  9. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. */
  14. const DIFF_CONTEXT_DEFAULT = 5; // same as diffLines
  15. // jest --no-expand
  16. //
  17. // Given array of aligned strings with inverse highlight formatting,
  18. // return joined lines with diff formatting (and patch marks, if needed).
  19. const joinAlignedDiffsNoExpand = (
  20. diffs,
  21. nContextLines = DIFF_CONTEXT_DEFAULT
  22. ) => {
  23. const iLength = diffs.length;
  24. const nContextLines2 = nContextLines + nContextLines; // First pass: count output lines and see if it has patches.
  25. let jLength = iLength;
  26. let hasExcessAtStartOrEnd = false;
  27. let nExcessesBetweenChanges = 0;
  28. let i = 0;
  29. while (i !== iLength) {
  30. const iStart = i;
  31. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
  32. i += 1;
  33. }
  34. if (iStart !== i) {
  35. if (iStart === 0) {
  36. // at start
  37. if (i > nContextLines) {
  38. jLength -= i - nContextLines; // subtract excess common lines
  39. hasExcessAtStartOrEnd = true;
  40. }
  41. } else if (i === iLength) {
  42. // at end
  43. const n = i - iStart;
  44. if (n > nContextLines) {
  45. jLength -= n - nContextLines; // subtract excess common lines
  46. hasExcessAtStartOrEnd = true;
  47. }
  48. } else {
  49. // between changes
  50. const n = i - iStart;
  51. if (n > nContextLines2) {
  52. jLength -= n - nContextLines2; // subtract excess common lines
  53. nExcessesBetweenChanges += 1;
  54. }
  55. }
  56. }
  57. while (i !== iLength && diffs[i][0] !== _cleanupSemantic.DIFF_EQUAL) {
  58. i += 1;
  59. }
  60. }
  61. const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
  62. if (nExcessesBetweenChanges !== 0) {
  63. jLength += nExcessesBetweenChanges + 1; // add patch lines
  64. } else if (hasExcessAtStartOrEnd) {
  65. jLength += 1; // add patch line
  66. }
  67. const jLast = jLength - 1;
  68. const lines = [];
  69. let jPatchMark = 0; // index of placeholder line for current patch mark
  70. if (hasPatch) {
  71. lines.push(''); // placeholder line for first patch mark
  72. } // Indexes of expected or received lines in current patch:
  73. let aStart = 0;
  74. let bStart = 0;
  75. let aEnd = 0;
  76. let bEnd = 0;
  77. const pushCommonLine = line => {
  78. const j = lines.length;
  79. lines.push((0, _printDiffs.printCommonLine)(line, j === 0 || j === jLast));
  80. aEnd += 1;
  81. bEnd += 1;
  82. };
  83. const pushDeleteLine = line => {
  84. lines.push((0, _printDiffs.printDeleteLine)(line));
  85. aEnd += 1;
  86. };
  87. const pushInsertLine = line => {
  88. lines.push((0, _printDiffs.printInsertLine)(line));
  89. bEnd += 1;
  90. }; // Second pass: push lines with diff formatting (and patch marks, if needed).
  91. i = 0;
  92. while (i !== iLength) {
  93. let iStart = i;
  94. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) {
  95. i += 1;
  96. }
  97. if (iStart !== i) {
  98. if (iStart === 0) {
  99. // at beginning
  100. if (i > nContextLines) {
  101. iStart = i - nContextLines;
  102. aStart = iStart;
  103. bStart = iStart;
  104. aEnd = aStart;
  105. bEnd = bStart;
  106. }
  107. for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
  108. pushCommonLine(diffs[iCommon][1]);
  109. }
  110. } else if (i === iLength) {
  111. // at end
  112. const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i;
  113. for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
  114. pushCommonLine(diffs[iCommon][1]);
  115. }
  116. } else {
  117. // between changes
  118. const nCommon = i - iStart;
  119. if (nCommon > nContextLines2) {
  120. const iEnd = iStart + nContextLines;
  121. for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
  122. pushCommonLine(diffs[iCommon][1]);
  123. }
  124. lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
  125. aStart,
  126. aEnd,
  127. bStart,
  128. bEnd
  129. );
  130. jPatchMark = lines.length;
  131. lines.push(''); // placeholder line for next patch mark
  132. const nOmit = nCommon - nContextLines2;
  133. aStart = aEnd + nOmit;
  134. bStart = bEnd + nOmit;
  135. aEnd = aStart;
  136. bEnd = bStart;
  137. for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) {
  138. pushCommonLine(diffs[iCommon][1]);
  139. }
  140. } else {
  141. for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
  142. pushCommonLine(diffs[iCommon][1]);
  143. }
  144. }
  145. }
  146. }
  147. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_DELETE) {
  148. pushDeleteLine(diffs[i][1]);
  149. i += 1;
  150. }
  151. while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_INSERT) {
  152. pushInsertLine(diffs[i][1]);
  153. i += 1;
  154. }
  155. }
  156. if (hasPatch) {
  157. lines[jPatchMark] = (0, _printDiffs.createPatchMark)(
  158. aStart,
  159. aEnd,
  160. bStart,
  161. bEnd
  162. );
  163. }
  164. return lines.join('\n');
  165. }; // jest --expand
  166. //
  167. // Given array of aligned strings with inverse highlight formatting,
  168. // return joined lines with diff formatting.
  169. exports.joinAlignedDiffsNoExpand = joinAlignedDiffsNoExpand;
  170. const joinAlignedDiffsExpand = diffs =>
  171. diffs
  172. .map((diff, i, diffs) => {
  173. const line = diff[1];
  174. switch (diff[0]) {
  175. case _cleanupSemantic.DIFF_DELETE:
  176. return (0, _printDiffs.printDeleteLine)(line);
  177. case _cleanupSemantic.DIFF_INSERT:
  178. return (0, _printDiffs.printInsertLine)(line);
  179. default:
  180. return (0, _printDiffs.printCommonLine)(
  181. line,
  182. i === 0 || i === diffs.length - 1
  183. );
  184. }
  185. })
  186. .join('\n');
  187. exports.joinAlignedDiffsExpand = joinAlignedDiffsExpand;