Skip to content

Commit 42b020f

Browse files
Remove cycle in printDiffs, diffLines and joinAlignedDiffs (#10818)
1 parent 7a34a69 commit 42b020f

File tree

4 files changed

+184
-203
lines changed

4 files changed

+184
-203
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
- `[expect]` Allow again `expect.Matchers` generic with single value ([#11986](https://github.com/facebook/jest/pull/11986))
1111
- `[jest-core]` Incorrect detection of open ZLIB handles ([#12022](https://github.com/facebook/jest/pull/12022))
12+
- `[jest-diff]` Break dependency cycle ([#10818](https://github.com/facebook/jest/pull/10818))
1213
- `[jest-environment-jsdom]` Add `@types/jsdom` dependency ([#11999](https://github.com/facebook/jest/pull/11999))
1314
- `[jest-environment-jsdom]` Do not reset the global.document too early on teardown ([#11871](https://github.com/facebook/jest/pull/11871))
1415
- `[jest-transform]` Improve error and warning messages ([#11998](https://github.com/facebook/jest/pull/11998))

packages/jest-diff/src/diffLines.ts

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,97 @@
77

88
import diff from 'diff-sequences';
99
import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
10+
import {
11+
joinAlignedDiffsExpand,
12+
joinAlignedDiffsNoExpand,
13+
} from './joinAlignedDiffs';
1014
import {normalizeDiffOptions} from './normalizeDiffOptions';
11-
import {printDiffLines} from './printDiffs';
12-
import type {DiffOptions} from './types';
15+
import type {DiffOptions, DiffOptionsNormalized} from './types';
1316

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

20+
type ChangeCounts = {
21+
a: number;
22+
b: number;
23+
};
24+
25+
const countChanges = (diffs: Array<Diff>): ChangeCounts => {
26+
let a = 0;
27+
let b = 0;
28+
29+
diffs.forEach(diff => {
30+
switch (diff[0]) {
31+
case DIFF_DELETE:
32+
a += 1;
33+
break;
34+
35+
case DIFF_INSERT:
36+
b += 1;
37+
break;
38+
}
39+
});
40+
41+
return {a, b};
42+
};
43+
44+
const printAnnotation = (
45+
{
46+
aAnnotation,
47+
aColor,
48+
aIndicator,
49+
bAnnotation,
50+
bColor,
51+
bIndicator,
52+
includeChangeCounts,
53+
omitAnnotationLines,
54+
}: DiffOptionsNormalized,
55+
changeCounts: ChangeCounts,
56+
): string => {
57+
if (omitAnnotationLines) {
58+
return '';
59+
}
60+
61+
let aRest = '';
62+
let bRest = '';
63+
64+
if (includeChangeCounts) {
65+
const aCount = String(changeCounts.a);
66+
const bCount = String(changeCounts.b);
67+
68+
// Padding right aligns the ends of the annotations.
69+
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
70+
const aAnnotationPadding = ' '.repeat(Math.max(0, baAnnotationLengthDiff));
71+
const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff));
72+
73+
// Padding left aligns the ends of the counts.
74+
const baCountLengthDiff = bCount.length - aCount.length;
75+
const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff));
76+
const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff));
77+
78+
aRest =
79+
aAnnotationPadding + ' ' + aIndicator + ' ' + aCountPadding + aCount;
80+
bRest =
81+
bAnnotationPadding + ' ' + bIndicator + ' ' + bCountPadding + bCount;
82+
}
83+
84+
return (
85+
aColor(aIndicator + ' ' + aAnnotation + aRest) +
86+
'\n' +
87+
bColor(bIndicator + ' ' + bAnnotation + bRest) +
88+
'\n\n'
89+
);
90+
};
91+
92+
export const printDiffLines = (
93+
diffs: Array<Diff>,
94+
options: DiffOptionsNormalized,
95+
): string =>
96+
printAnnotation(options, countChanges(diffs)) +
97+
(options.expand
98+
? joinAlignedDiffsExpand(diffs, options)
99+
: joinAlignedDiffsNoExpand(diffs, options));
100+
17101
// Compare two arrays of strings line-by-line. Format as comparison lines.
18102
export const diffLinesUnified = (
19103
aLines: Array<string>,

packages/jest-diff/src/joinAlignedDiffs.ts

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,99 @@
66
*/
77

88
import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
9-
import {
10-
createPatchMark,
11-
printCommonLine,
12-
printDeleteLine,
13-
printInsertLine,
14-
} from './printDiffs';
15-
import type {DiffOptionsNormalized} from './types';
9+
import type {DiffOptionsColor, DiffOptionsNormalized} from './types';
10+
11+
const formatTrailingSpaces = (
12+
line: string,
13+
trailingSpaceFormatter: DiffOptionsColor,
14+
): string => line.replace(/\s+$/, match => trailingSpaceFormatter(match));
15+
16+
const printDiffLine = (
17+
line: string,
18+
isFirstOrLast: boolean,
19+
color: DiffOptionsColor,
20+
indicator: string,
21+
trailingSpaceFormatter: DiffOptionsColor,
22+
emptyFirstOrLastLinePlaceholder: string,
23+
): string =>
24+
line.length !== 0
25+
? color(
26+
indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter),
27+
)
28+
: indicator !== ' '
29+
? color(indicator)
30+
: isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0
31+
? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder)
32+
: '';
33+
34+
const printDeleteLine = (
35+
line: string,
36+
isFirstOrLast: boolean,
37+
{
38+
aColor,
39+
aIndicator,
40+
changeLineTrailingSpaceColor,
41+
emptyFirstOrLastLinePlaceholder,
42+
}: DiffOptionsNormalized,
43+
): string =>
44+
printDiffLine(
45+
line,
46+
isFirstOrLast,
47+
aColor,
48+
aIndicator,
49+
changeLineTrailingSpaceColor,
50+
emptyFirstOrLastLinePlaceholder,
51+
);
52+
53+
const printInsertLine = (
54+
line: string,
55+
isFirstOrLast: boolean,
56+
{
57+
bColor,
58+
bIndicator,
59+
changeLineTrailingSpaceColor,
60+
emptyFirstOrLastLinePlaceholder,
61+
}: DiffOptionsNormalized,
62+
): string =>
63+
printDiffLine(
64+
line,
65+
isFirstOrLast,
66+
bColor,
67+
bIndicator,
68+
changeLineTrailingSpaceColor,
69+
emptyFirstOrLastLinePlaceholder,
70+
);
71+
72+
const printCommonLine = (
73+
line: string,
74+
isFirstOrLast: boolean,
75+
{
76+
commonColor,
77+
commonIndicator,
78+
commonLineTrailingSpaceColor,
79+
emptyFirstOrLastLinePlaceholder,
80+
}: DiffOptionsNormalized,
81+
): string =>
82+
printDiffLine(
83+
line,
84+
isFirstOrLast,
85+
commonColor,
86+
commonIndicator,
87+
commonLineTrailingSpaceColor,
88+
emptyFirstOrLastLinePlaceholder,
89+
);
90+
91+
// In GNU diff format, indexes are one-based instead of zero-based.
92+
const createPatchMark = (
93+
aStart: number,
94+
aEnd: number,
95+
bStart: number,
96+
bEnd: number,
97+
{patchColor}: DiffOptionsNormalized,
98+
): string =>
99+
patchColor(
100+
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`,
101+
);
16102

17103
// jest --no-expand
18104
//

0 commit comments

Comments
 (0)