Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove cycle in printDiffs, diffLines and joinAlignedDiffs #10818

Merged
merged 6 commits into from Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 86 additions & 2 deletions packages/jest-diff/src/diffLines.ts
Expand Up @@ -7,13 +7,97 @@

import diff from 'diff-sequences';
import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
import {
joinAlignedDiffsExpand,
joinAlignedDiffsNoExpand,
} from './joinAlignedDiffs';
import {normalizeDiffOptions} from './normalizeDiffOptions';
import {printDiffLines} from './printDiffs';
import type {DiffOptions} from './types';
import type {DiffOptions, DiffOptionsNormalized} from './types';

const isEmptyString = (lines: Array<string>) =>
lines.length === 1 && lines[0].length === 0;

type ChangeCounts = {
a: number;
b: number;
};

const countChanges = (diffs: Array<Diff>): ChangeCounts => {
let a = 0;
let b = 0;

diffs.forEach(diff => {
switch (diff[0]) {
case DIFF_DELETE:
a += 1;
break;

case DIFF_INSERT:
b += 1;
break;
}
});

return {a, b};
};

const printAnnotation = (
{
aAnnotation,
aColor,
aIndicator,
bAnnotation,
bColor,
bIndicator,
includeChangeCounts,
omitAnnotationLines,
}: DiffOptionsNormalized,
changeCounts: ChangeCounts,
): string => {
if (omitAnnotationLines) {
return '';
}

let aRest = '';
let bRest = '';

if (includeChangeCounts) {
const aCount = String(changeCounts.a);
const bCount = String(changeCounts.b);

// Padding right aligns the ends of the annotations.
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
const aAnnotationPadding = ' '.repeat(Math.max(0, baAnnotationLengthDiff));
const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff));

// Padding left aligns the ends of the counts.
const baCountLengthDiff = bCount.length - aCount.length;
const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff));
const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff));

aRest =
aAnnotationPadding + ' ' + aIndicator + ' ' + aCountPadding + aCount;
bRest =
bAnnotationPadding + ' ' + bIndicator + ' ' + bCountPadding + bCount;
}

return (
aColor(aIndicator + ' ' + aAnnotation + aRest) +
'\n' +
bColor(bIndicator + ' ' + bAnnotation + bRest) +
'\n\n'
);
};

export const printDiffLines = (
diffs: Array<Diff>,
options: DiffOptionsNormalized,
): string =>
printAnnotation(options, countChanges(diffs)) +
(options.expand
? joinAlignedDiffsExpand(diffs, options)
: joinAlignedDiffsNoExpand(diffs, options));

// Compare two arrays of strings line-by-line. Format as comparison lines.
export const diffLinesUnified = (
aLines: Array<string>,
Expand Down
100 changes: 93 additions & 7 deletions packages/jest-diff/src/joinAlignedDiffs.ts
Expand Up @@ -6,13 +6,99 @@
*/

import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
import {
createPatchMark,
printCommonLine,
printDeleteLine,
printInsertLine,
} from './printDiffs';
import type {DiffOptionsNormalized} from './types';
import type {DiffOptionsColor, DiffOptionsNormalized} from './types';

const formatTrailingSpaces = (
line: string,
trailingSpaceFormatter: DiffOptionsColor,
): string => line.replace(/\s+$/, match => trailingSpaceFormatter(match));

const printDiffLine = (
line: string,
isFirstOrLast: boolean,
color: DiffOptionsColor,
indicator: string,
trailingSpaceFormatter: DiffOptionsColor,
emptyFirstOrLastLinePlaceholder: string,
): string =>
line.length !== 0
? color(
indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter),
)
: indicator !== ' '
? color(indicator)
: isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0
? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder)
: '';

const printDeleteLine = (
line: string,
isFirstOrLast: boolean,
{
aColor,
aIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder,
}: DiffOptionsNormalized,
): string =>
printDiffLine(
line,
isFirstOrLast,
aColor,
aIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder,
);

const printInsertLine = (
line: string,
isFirstOrLast: boolean,
{
bColor,
bIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder,
}: DiffOptionsNormalized,
): string =>
printDiffLine(
line,
isFirstOrLast,
bColor,
bIndicator,
changeLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder,
);

const printCommonLine = (
line: string,
isFirstOrLast: boolean,
{
commonColor,
commonIndicator,
commonLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder,
}: DiffOptionsNormalized,
): string =>
printDiffLine(
line,
isFirstOrLast,
commonColor,
commonIndicator,
commonLineTrailingSpaceColor,
emptyFirstOrLastLinePlaceholder,
);

// In GNU diff format, indexes are one-based instead of zero-based.
const createPatchMark = (
aStart: number,
aEnd: number,
bStart: number,
bEnd: number,
{patchColor}: DiffOptionsNormalized,
): string =>
patchColor(
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`,
);

// jest --no-expand
//
Expand Down