|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { Diff } from './cleanupSemantic' |
| 9 | +import { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT } from './cleanupSemantic' |
| 10 | +import type { DiffOptionsColor, DiffOptionsNormalized } from './types' |
| 11 | + |
| 12 | +function formatTrailingSpaces(line: string, |
| 13 | + trailingSpaceFormatter: DiffOptionsColor): string { |
| 14 | + return line.replace(/\s+$/, match => trailingSpaceFormatter(match)) |
| 15 | +} |
| 16 | + |
| 17 | +function printDiffLine(line: string, |
| 18 | + isFirstOrLast: boolean, |
| 19 | + color: DiffOptionsColor, |
| 20 | + indicator: string, |
| 21 | + trailingSpaceFormatter: DiffOptionsColor, |
| 22 | + emptyFirstOrLastLinePlaceholder: string): string { |
| 23 | + return line.length !== 0 |
| 24 | + ? color( |
| 25 | + `${indicator} ${formatTrailingSpaces(line, trailingSpaceFormatter)}`, |
| 26 | + ) |
| 27 | + : indicator !== ' ' |
| 28 | + ? color(indicator) |
| 29 | + : (isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0) |
| 30 | + ? color(`${indicator} ${emptyFirstOrLastLinePlaceholder}`) |
| 31 | + : '' |
| 32 | +} |
| 33 | + |
| 34 | +function printDeleteLine(line: string, |
| 35 | + isFirstOrLast: boolean, |
| 36 | + { |
| 37 | + aColor, |
| 38 | + aIndicator, |
| 39 | + changeLineTrailingSpaceColor, |
| 40 | + emptyFirstOrLastLinePlaceholder, |
| 41 | + }: DiffOptionsNormalized): string { |
| 42 | + return printDiffLine( |
| 43 | + line, |
| 44 | + isFirstOrLast, |
| 45 | + aColor, |
| 46 | + aIndicator, |
| 47 | + changeLineTrailingSpaceColor, |
| 48 | + emptyFirstOrLastLinePlaceholder, |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +function printInsertLine(line: string, |
| 53 | + isFirstOrLast: boolean, |
| 54 | + { |
| 55 | + bColor, |
| 56 | + bIndicator, |
| 57 | + changeLineTrailingSpaceColor, |
| 58 | + emptyFirstOrLastLinePlaceholder, |
| 59 | + }: DiffOptionsNormalized): string { |
| 60 | + return printDiffLine( |
| 61 | + line, |
| 62 | + isFirstOrLast, |
| 63 | + bColor, |
| 64 | + bIndicator, |
| 65 | + changeLineTrailingSpaceColor, |
| 66 | + emptyFirstOrLastLinePlaceholder, |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +function printCommonLine(line: string, |
| 71 | + isFirstOrLast: boolean, |
| 72 | + { |
| 73 | + commonColor, |
| 74 | + commonIndicator, |
| 75 | + commonLineTrailingSpaceColor, |
| 76 | + emptyFirstOrLastLinePlaceholder, |
| 77 | + }: DiffOptionsNormalized): string { |
| 78 | + return printDiffLine( |
| 79 | + line, |
| 80 | + isFirstOrLast, |
| 81 | + commonColor, |
| 82 | + commonIndicator, |
| 83 | + commonLineTrailingSpaceColor, |
| 84 | + emptyFirstOrLastLinePlaceholder, |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +// In GNU diff format, indexes are one-based instead of zero-based. |
| 89 | +function createPatchMark(aStart: number, |
| 90 | + aEnd: number, |
| 91 | + bStart: number, |
| 92 | + bEnd: number, |
| 93 | + { patchColor }: DiffOptionsNormalized): string { |
| 94 | + return patchColor( |
| 95 | + `@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`, |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +// jest --no-expand |
| 100 | +// |
| 101 | +// Given array of aligned strings with inverse highlight formatting, |
| 102 | +// return joined lines with diff formatting (and patch marks, if needed). |
| 103 | +export function joinAlignedDiffsNoExpand(diffs: Array<Diff>, |
| 104 | + options: DiffOptionsNormalized): string { |
| 105 | + const iLength = diffs.length |
| 106 | + const nContextLines = options.contextLines |
| 107 | + const nContextLines2 = nContextLines + nContextLines |
| 108 | + |
| 109 | + // First pass: count output lines and see if it has patches. |
| 110 | + let jLength = iLength |
| 111 | + let hasExcessAtStartOrEnd = false |
| 112 | + let nExcessesBetweenChanges = 0 |
| 113 | + let i = 0 |
| 114 | + while (i !== iLength) { |
| 115 | + const iStart = i |
| 116 | + while (i !== iLength && diffs[i][0] === DIFF_EQUAL) |
| 117 | + i += 1 |
| 118 | + |
| 119 | + if (iStart !== i) { |
| 120 | + if (iStart === 0) { |
| 121 | + // at start |
| 122 | + if (i > nContextLines) { |
| 123 | + jLength -= i - nContextLines // subtract excess common lines |
| 124 | + hasExcessAtStartOrEnd = true |
| 125 | + } |
| 126 | + } |
| 127 | + else if (i === iLength) { |
| 128 | + // at end |
| 129 | + const n = i - iStart |
| 130 | + if (n > nContextLines) { |
| 131 | + jLength -= n - nContextLines // subtract excess common lines |
| 132 | + hasExcessAtStartOrEnd = true |
| 133 | + } |
| 134 | + } |
| 135 | + else { |
| 136 | + // between changes |
| 137 | + const n = i - iStart |
| 138 | + if (n > nContextLines2) { |
| 139 | + jLength -= n - nContextLines2 // subtract excess common lines |
| 140 | + nExcessesBetweenChanges += 1 |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + while (i !== iLength && diffs[i][0] !== DIFF_EQUAL) |
| 146 | + i += 1 |
| 147 | + } |
| 148 | + |
| 149 | + const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd |
| 150 | + if (nExcessesBetweenChanges !== 0) |
| 151 | + jLength += nExcessesBetweenChanges + 1 // add patch lines |
| 152 | + else if (hasExcessAtStartOrEnd) |
| 153 | + jLength += 1 // add patch line |
| 154 | + |
| 155 | + const jLast = jLength - 1 |
| 156 | + |
| 157 | + const lines: Array<string> = [] |
| 158 | + |
| 159 | + let jPatchMark = 0 // index of placeholder line for current patch mark |
| 160 | + if (hasPatch) |
| 161 | + lines.push('') // placeholder line for first patch mark |
| 162 | + |
| 163 | + // Indexes of expected or received lines in current patch: |
| 164 | + let aStart = 0 |
| 165 | + let bStart = 0 |
| 166 | + let aEnd = 0 |
| 167 | + let bEnd = 0 |
| 168 | + |
| 169 | + const pushCommonLine = (line: string): void => { |
| 170 | + const j = lines.length |
| 171 | + lines.push(printCommonLine(line, j === 0 || j === jLast, options)) |
| 172 | + aEnd += 1 |
| 173 | + bEnd += 1 |
| 174 | + } |
| 175 | + |
| 176 | + const pushDeleteLine = (line: string): void => { |
| 177 | + const j = lines.length |
| 178 | + lines.push(printDeleteLine(line, j === 0 || j === jLast, options)) |
| 179 | + aEnd += 1 |
| 180 | + } |
| 181 | + |
| 182 | + const pushInsertLine = (line: string): void => { |
| 183 | + const j = lines.length |
| 184 | + lines.push(printInsertLine(line, j === 0 || j === jLast, options)) |
| 185 | + bEnd += 1 |
| 186 | + } |
| 187 | + |
| 188 | + // Second pass: push lines with diff formatting (and patch marks, if needed). |
| 189 | + i = 0 |
| 190 | + while (i !== iLength) { |
| 191 | + let iStart = i |
| 192 | + while (i !== iLength && diffs[i][0] === DIFF_EQUAL) |
| 193 | + i += 1 |
| 194 | + |
| 195 | + if (iStart !== i) { |
| 196 | + if (iStart === 0) { |
| 197 | + // at beginning |
| 198 | + if (i > nContextLines) { |
| 199 | + iStart = i - nContextLines |
| 200 | + aStart = iStart |
| 201 | + bStart = iStart |
| 202 | + aEnd = aStart |
| 203 | + bEnd = bStart |
| 204 | + } |
| 205 | + |
| 206 | + for (let iCommon = iStart; iCommon !== i; iCommon += 1) |
| 207 | + pushCommonLine(diffs[iCommon][1]) |
| 208 | + } |
| 209 | + else if (i === iLength) { |
| 210 | + // at end |
| 211 | + const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i |
| 212 | + |
| 213 | + for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) |
| 214 | + pushCommonLine(diffs[iCommon][1]) |
| 215 | + } |
| 216 | + else { |
| 217 | + // between changes |
| 218 | + const nCommon = i - iStart |
| 219 | + |
| 220 | + if (nCommon > nContextLines2) { |
| 221 | + const iEnd = iStart + nContextLines |
| 222 | + |
| 223 | + for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) |
| 224 | + pushCommonLine(diffs[iCommon][1]) |
| 225 | + |
| 226 | + lines[jPatchMark] = createPatchMark( |
| 227 | + aStart, |
| 228 | + aEnd, |
| 229 | + bStart, |
| 230 | + bEnd, |
| 231 | + options, |
| 232 | + ) |
| 233 | + jPatchMark = lines.length |
| 234 | + lines.push('') // placeholder line for next patch mark |
| 235 | + |
| 236 | + const nOmit = nCommon - nContextLines2 |
| 237 | + aStart = aEnd + nOmit |
| 238 | + bStart = bEnd + nOmit |
| 239 | + aEnd = aStart |
| 240 | + bEnd = bStart |
| 241 | + |
| 242 | + for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) |
| 243 | + pushCommonLine(diffs[iCommon][1]) |
| 244 | + } |
| 245 | + else { |
| 246 | + for (let iCommon = iStart; iCommon !== i; iCommon += 1) |
| 247 | + pushCommonLine(diffs[iCommon][1]) |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + while (i !== iLength && diffs[i][0] === DIFF_DELETE) { |
| 253 | + pushDeleteLine(diffs[i][1]) |
| 254 | + i += 1 |
| 255 | + } |
| 256 | + |
| 257 | + while (i !== iLength && diffs[i][0] === DIFF_INSERT) { |
| 258 | + pushInsertLine(diffs[i][1]) |
| 259 | + i += 1 |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + if (hasPatch) |
| 264 | + lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options) |
| 265 | + |
| 266 | + return lines.join('\n') |
| 267 | +} |
| 268 | + |
| 269 | +// jest --expand |
| 270 | +// |
| 271 | +// Given array of aligned strings with inverse highlight formatting, |
| 272 | +// return joined lines with diff formatting. |
| 273 | +export function joinAlignedDiffsExpand(diffs: Array<Diff>, |
| 274 | + options: DiffOptionsNormalized): string { |
| 275 | + return diffs |
| 276 | + .map((diff: Diff, i: number, diffs: Array<Diff>): string => { |
| 277 | + const line = diff[1] |
| 278 | + const isFirstOrLast = i === 0 || i === diffs.length - 1 |
| 279 | + |
| 280 | + switch (diff[0]) { |
| 281 | + case DIFF_DELETE: |
| 282 | + return printDeleteLine(line, isFirstOrLast, options) |
| 283 | + |
| 284 | + case DIFF_INSERT: |
| 285 | + return printInsertLine(line, isFirstOrLast, options) |
| 286 | + |
| 287 | + default: |
| 288 | + return printCommonLine(line, isFirstOrLast, options) |
| 289 | + } |
| 290 | + }) |
| 291 | + .join('\n') |
| 292 | +} |
0 commit comments