Skip to content

Commit

Permalink
Remove cycle in printDiffs, diffLines and joinAlignedDiffs (#10818)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondnumbergenerator committed Nov 11, 2021
1 parent 7a34a69 commit 42b020f
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 203 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@

- `[expect]` Allow again `expect.Matchers` generic with single value ([#11986](https://github.com/facebook/jest/pull/11986))
- `[jest-core]` Incorrect detection of open ZLIB handles ([#12022](https://github.com/facebook/jest/pull/12022))
- `[jest-diff]` Break dependency cycle ([#10818](https://github.com/facebook/jest/pull/10818))
- `[jest-environment-jsdom]` Add `@types/jsdom` dependency ([#11999](https://github.com/facebook/jest/pull/11999))
- `[jest-environment-jsdom]` Do not reset the global.document too early on teardown ([#11871](https://github.com/facebook/jest/pull/11871))
- `[jest-transform]` Improve error and warning messages ([#11998](https://github.com/facebook/jest/pull/11998))
Expand Down
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

0 comments on commit 42b020f

Please sign in to comment.