| | |
| | | Object.defineProperty(exports, '__esModule', { |
| | | value: true |
| | | }); |
| | | exports.joinAlignedDiffsExpand = exports.joinAlignedDiffsNoExpand = void 0; |
| | | exports.joinAlignedDiffsNoExpand = exports.joinAlignedDiffsExpand = void 0; |
| | | |
| | | var _cleanupSemantic = require('./cleanupSemantic'); |
| | | |
| | | var _printDiffs = require('./printDiffs'); |
| | | |
| | | /** |
| | | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| | |
| | | * This source code is licensed under the MIT license found in the |
| | | * LICENSE file in the root directory of this source tree. |
| | | */ |
| | | // jest --no-expand |
| | | const formatTrailingSpaces = (line, trailingSpaceFormatter) => |
| | | line.replace(/\s+$/, match => trailingSpaceFormatter(match)); |
| | | |
| | | const printDiffLine = ( |
| | | line, |
| | | isFirstOrLast, |
| | | color, |
| | | indicator, |
| | | trailingSpaceFormatter, |
| | | emptyFirstOrLastLinePlaceholder |
| | | ) => |
| | | line.length !== 0 |
| | | ? color( |
| | | indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter) |
| | | ) |
| | | : indicator !== ' ' |
| | | ? color(indicator) |
| | | : isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0 |
| | | ? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder) |
| | | : ''; |
| | | |
| | | const printDeleteLine = ( |
| | | line, |
| | | isFirstOrLast, |
| | | { |
| | | aColor, |
| | | aIndicator, |
| | | changeLineTrailingSpaceColor, |
| | | emptyFirstOrLastLinePlaceholder |
| | | } |
| | | ) => |
| | | printDiffLine( |
| | | line, |
| | | isFirstOrLast, |
| | | aColor, |
| | | aIndicator, |
| | | changeLineTrailingSpaceColor, |
| | | emptyFirstOrLastLinePlaceholder |
| | | ); |
| | | |
| | | const printInsertLine = ( |
| | | line, |
| | | isFirstOrLast, |
| | | { |
| | | bColor, |
| | | bIndicator, |
| | | changeLineTrailingSpaceColor, |
| | | emptyFirstOrLastLinePlaceholder |
| | | } |
| | | ) => |
| | | printDiffLine( |
| | | line, |
| | | isFirstOrLast, |
| | | bColor, |
| | | bIndicator, |
| | | changeLineTrailingSpaceColor, |
| | | emptyFirstOrLastLinePlaceholder |
| | | ); |
| | | |
| | | const printCommonLine = ( |
| | | line, |
| | | isFirstOrLast, |
| | | { |
| | | commonColor, |
| | | commonIndicator, |
| | | commonLineTrailingSpaceColor, |
| | | emptyFirstOrLastLinePlaceholder |
| | | } |
| | | ) => |
| | | printDiffLine( |
| | | line, |
| | | isFirstOrLast, |
| | | commonColor, |
| | | commonIndicator, |
| | | commonLineTrailingSpaceColor, |
| | | emptyFirstOrLastLinePlaceholder |
| | | ); // In GNU diff format, indexes are one-based instead of zero-based. |
| | | |
| | | const createPatchMark = (aStart, aEnd, bStart, bEnd, {patchColor}) => |
| | | patchColor( |
| | | `@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@` |
| | | ); // jest --no-expand |
| | | // |
| | | // Given array of aligned strings with inverse highlight formatting, |
| | | // return joined lines with diff formatting (and patch marks, if needed). |
| | | |
| | | const joinAlignedDiffsNoExpand = (diffs, options) => { |
| | | const iLength = diffs.length; |
| | | const nContextLines = options.contextLines; |
| | |
| | | |
| | | const pushCommonLine = line => { |
| | | const j = lines.length; |
| | | lines.push( |
| | | (0, _printDiffs.printCommonLine)(line, j === 0 || j === jLast, options) |
| | | ); |
| | | lines.push(printCommonLine(line, j === 0 || j === jLast, options)); |
| | | aEnd += 1; |
| | | bEnd += 1; |
| | | }; |
| | | |
| | | const pushDeleteLine = line => { |
| | | const j = lines.length; |
| | | lines.push( |
| | | (0, _printDiffs.printDeleteLine)(line, j === 0 || j === jLast, options) |
| | | ); |
| | | lines.push(printDeleteLine(line, j === 0 || j === jLast, options)); |
| | | aEnd += 1; |
| | | }; |
| | | |
| | | const pushInsertLine = line => { |
| | | const j = lines.length; |
| | | lines.push( |
| | | (0, _printDiffs.printInsertLine)(line, j === 0 || j === jLast, options) |
| | | ); |
| | | lines.push(printInsertLine(line, j === 0 || j === jLast, options)); |
| | | bEnd += 1; |
| | | }; // Second pass: push lines with diff formatting (and patch marks, if needed). |
| | | |
| | |
| | | pushCommonLine(diffs[iCommon][1]); |
| | | } |
| | | |
| | | lines[jPatchMark] = (0, _printDiffs.createPatchMark)( |
| | | lines[jPatchMark] = createPatchMark( |
| | | aStart, |
| | | aEnd, |
| | | bStart, |
| | |
| | | } |
| | | |
| | | if (hasPatch) { |
| | | lines[jPatchMark] = (0, _printDiffs.createPatchMark)( |
| | | aStart, |
| | | aEnd, |
| | | bStart, |
| | | bEnd, |
| | | options |
| | | ); |
| | | lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options); |
| | | } |
| | | |
| | | return lines.join('\n'); |
| | |
| | | |
| | | switch (diff[0]) { |
| | | case _cleanupSemantic.DIFF_DELETE: |
| | | return (0, _printDiffs.printDeleteLine)(line, isFirstOrLast, options); |
| | | return printDeleteLine(line, isFirstOrLast, options); |
| | | |
| | | case _cleanupSemantic.DIFF_INSERT: |
| | | return (0, _printDiffs.printInsertLine)(line, isFirstOrLast, options); |
| | | return printInsertLine(line, isFirstOrLast, options); |
| | | |
| | | default: |
| | | return (0, _printDiffs.printCommonLine)(line, isFirstOrLast, options); |
| | | return printCommonLine(line, isFirstOrLast, options); |
| | | } |
| | | }) |
| | | .join('\n'); |