diffLines.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.printDiffLines =
  6. exports.diffLinesUnified2 =
  7. exports.diffLinesUnified =
  8. exports.diffLinesRaw =
  9. void 0;
  10. var _diffSequences = _interopRequireDefault(require('diff-sequences'));
  11. var _cleanupSemantic = require('./cleanupSemantic');
  12. var _joinAlignedDiffs = require('./joinAlignedDiffs');
  13. var _normalizeDiffOptions = require('./normalizeDiffOptions');
  14. function _interopRequireDefault(obj) {
  15. return obj && obj.__esModule ? obj : {default: obj};
  16. }
  17. /**
  18. * Copyright (c) Meta Platforms, Inc. and affiliates.
  19. *
  20. * This source code is licensed under the MIT license found in the
  21. * LICENSE file in the root directory of this source tree.
  22. */
  23. const isEmptyString = lines => lines.length === 1 && lines[0].length === 0;
  24. const countChanges = diffs => {
  25. let a = 0;
  26. let b = 0;
  27. diffs.forEach(diff => {
  28. switch (diff[0]) {
  29. case _cleanupSemantic.DIFF_DELETE:
  30. a += 1;
  31. break;
  32. case _cleanupSemantic.DIFF_INSERT:
  33. b += 1;
  34. break;
  35. }
  36. });
  37. return {
  38. a,
  39. b
  40. };
  41. };
  42. const printAnnotation = (
  43. {
  44. aAnnotation,
  45. aColor,
  46. aIndicator,
  47. bAnnotation,
  48. bColor,
  49. bIndicator,
  50. includeChangeCounts,
  51. omitAnnotationLines
  52. },
  53. changeCounts
  54. ) => {
  55. if (omitAnnotationLines) {
  56. return '';
  57. }
  58. let aRest = '';
  59. let bRest = '';
  60. if (includeChangeCounts) {
  61. const aCount = String(changeCounts.a);
  62. const bCount = String(changeCounts.b);
  63. // Padding right aligns the ends of the annotations.
  64. const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
  65. const aAnnotationPadding = ' '.repeat(Math.max(0, baAnnotationLengthDiff));
  66. const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff));
  67. // Padding left aligns the ends of the counts.
  68. const baCountLengthDiff = bCount.length - aCount.length;
  69. const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff));
  70. const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff));
  71. aRest = `${aAnnotationPadding} ${aIndicator} ${aCountPadding}${aCount}`;
  72. bRest = `${bAnnotationPadding} ${bIndicator} ${bCountPadding}${bCount}`;
  73. }
  74. const a = `${aIndicator} ${aAnnotation}${aRest}`;
  75. const b = `${bIndicator} ${bAnnotation}${bRest}`;
  76. return `${aColor(a)}\n${bColor(b)}\n\n`;
  77. };
  78. const printDiffLines = (diffs, options) =>
  79. printAnnotation(options, countChanges(diffs)) +
  80. (options.expand
  81. ? (0, _joinAlignedDiffs.joinAlignedDiffsExpand)(diffs, options)
  82. : (0, _joinAlignedDiffs.joinAlignedDiffsNoExpand)(diffs, options));
  83. // Compare two arrays of strings line-by-line. Format as comparison lines.
  84. exports.printDiffLines = printDiffLines;
  85. const diffLinesUnified = (aLines, bLines, options) =>
  86. printDiffLines(
  87. diffLinesRaw(
  88. isEmptyString(aLines) ? [] : aLines,
  89. isEmptyString(bLines) ? [] : bLines
  90. ),
  91. (0, _normalizeDiffOptions.normalizeDiffOptions)(options)
  92. );
  93. // Given two pairs of arrays of strings:
  94. // Compare the pair of comparison arrays line-by-line.
  95. // Format the corresponding lines in the pair of displayable arrays.
  96. exports.diffLinesUnified = diffLinesUnified;
  97. const diffLinesUnified2 = (
  98. aLinesDisplay,
  99. bLinesDisplay,
  100. aLinesCompare,
  101. bLinesCompare,
  102. options
  103. ) => {
  104. if (isEmptyString(aLinesDisplay) && isEmptyString(aLinesCompare)) {
  105. aLinesDisplay = [];
  106. aLinesCompare = [];
  107. }
  108. if (isEmptyString(bLinesDisplay) && isEmptyString(bLinesCompare)) {
  109. bLinesDisplay = [];
  110. bLinesCompare = [];
  111. }
  112. if (
  113. aLinesDisplay.length !== aLinesCompare.length ||
  114. bLinesDisplay.length !== bLinesCompare.length
  115. ) {
  116. // Fall back to diff of display lines.
  117. return diffLinesUnified(aLinesDisplay, bLinesDisplay, options);
  118. }
  119. const diffs = diffLinesRaw(aLinesCompare, bLinesCompare);
  120. // Replace comparison lines with displayable lines.
  121. let aIndex = 0;
  122. let bIndex = 0;
  123. diffs.forEach(diff => {
  124. switch (diff[0]) {
  125. case _cleanupSemantic.DIFF_DELETE:
  126. diff[1] = aLinesDisplay[aIndex];
  127. aIndex += 1;
  128. break;
  129. case _cleanupSemantic.DIFF_INSERT:
  130. diff[1] = bLinesDisplay[bIndex];
  131. bIndex += 1;
  132. break;
  133. default:
  134. diff[1] = bLinesDisplay[bIndex];
  135. aIndex += 1;
  136. bIndex += 1;
  137. }
  138. });
  139. return printDiffLines(
  140. diffs,
  141. (0, _normalizeDiffOptions.normalizeDiffOptions)(options)
  142. );
  143. };
  144. // Compare two arrays of strings line-by-line.
  145. exports.diffLinesUnified2 = diffLinesUnified2;
  146. const diffLinesRaw = (aLines, bLines) => {
  147. const aLength = aLines.length;
  148. const bLength = bLines.length;
  149. const isCommon = (aIndex, bIndex) => aLines[aIndex] === bLines[bIndex];
  150. const diffs = [];
  151. let aIndex = 0;
  152. let bIndex = 0;
  153. const foundSubsequence = (nCommon, aCommon, bCommon) => {
  154. for (; aIndex !== aCommon; aIndex += 1) {
  155. diffs.push(
  156. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, aLines[aIndex])
  157. );
  158. }
  159. for (; bIndex !== bCommon; bIndex += 1) {
  160. diffs.push(
  161. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, bLines[bIndex])
  162. );
  163. }
  164. for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1) {
  165. diffs.push(
  166. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_EQUAL, bLines[bIndex])
  167. );
  168. }
  169. };
  170. (0, _diffSequences.default)(aLength, bLength, isCommon, foundSubsequence);
  171. // After the last common subsequence, push remaining change items.
  172. for (; aIndex !== aLength; aIndex += 1) {
  173. diffs.push(
  174. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_DELETE, aLines[aIndex])
  175. );
  176. }
  177. for (; bIndex !== bLength; bIndex += 1) {
  178. diffs.push(
  179. new _cleanupSemantic.Diff(_cleanupSemantic.DIFF_INSERT, bLines[bIndex])
  180. );
  181. }
  182. return diffs;
  183. };
  184. exports.diffLinesRaw = diffLinesRaw;