forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_strings.js
270 lines (228 loc) · 7.99 KB
/
diff_strings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
import chalk from 'chalk';
import {diffLines, structuredPatch} from 'diff';
import {NO_DIFF_MESSAGE} from './constants.js';
const DIFF_CONTEXT_DEFAULT = 5;
export type DiffOptions = {|
aAnnotation?: string,
bAnnotation?: string,
expand?: boolean,
contextLines?: number,
|};
type Original = {|
a: string,
b: string,
|};
type Diff = string | null;
type Hunk = {|
lines: Array<string>,
newLines: number,
newStart: number,
oldLines: number,
oldStart: number,
|};
type DIFF_DIGIT = -1 | 1 | 0; // removed | added | equal
// Given diff digit, return array which consists of:
// if compared line is removed or added: corresponding original line
// if compared line is equal: original received and expected lines
type GetOriginal = (digit: DIFF_DIGIT) => Array<string>;
// Given chunk, return diff character.
const getDiffChar = (chunk): string =>
chunk.removed ? '-' : chunk.added ? '+' : ' ';
// Given diff character in line of hunk or computed from properties of chunk.
const getDiffDigit = (char: string): DIFF_DIGIT =>
char === '-' ? -1 : char === '+' ? 1 : 0;
// Color for text of line.
const getColor = (digit: DIFF_DIGIT, onlyIndentationChanged?: boolean) => {
if (digit === -1) {
return chalk.green; // removed
}
if (digit === 1) {
return chalk.red; // added
}
return onlyIndentationChanged ? chalk.cyan : chalk.dim;
};
// Do NOT color leading or trailing spaces if original lines are equal:
// Background color for leading or trailing spaces.
const getBgColor = (digit: DIFF_DIGIT, onlyIndentationChanged?: boolean) =>
digit === 0 && !onlyIndentationChanged ? chalk.bgYellow : chalk.inverse;
// ONLY trailing if expected value is snapshot or multiline string.
const highlightTrailingSpaces = (line: string, bgColor: Function): string =>
line.replace(/\s+$/, bgColor('$&'));
// BOTH leading AND trailing if expected value is data structure.
const highlightLeadingTrailingSpaces = (
line: string,
bgColor: Function,
): string =>
// If line consists of ALL spaces: highlight all of them.
highlightTrailingSpaces(line, bgColor).replace(
// If line has an ODD length of leading spaces: highlight only the LAST.
/^(\s\s)*(\s)(?=[^\s])/,
'$1' + bgColor('$2'),
);
const getAnnotation = (options: ?DiffOptions): string =>
chalk.green('- ' + ((options && options.aAnnotation) || 'Expected')) +
'\n' +
chalk.red('+ ' + ((options && options.bAnnotation) || 'Received')) +
'\n\n';
// Given string, return array of its lines.
const splitIntoLines = string => {
const lines = string.split('\n');
if (lines.length !== 0 && lines[lines.length - 1] === '') {
lines.pop();
}
return lines;
};
// Given diff character and compared line, return original line with colors.
const formatLine = (
char: string,
lineCompared: string,
getOriginal?: GetOriginal,
) => {
const digit = getDiffDigit(char);
if (getOriginal) {
// Compared without indentation if expected value is data structure.
const lineArray = getOriginal(digit);
const lineOriginal = lineArray[0];
const onlyIndentationChanged =
digit === 0 && lineOriginal.length !== lineArray[1].length;
return getColor(digit, onlyIndentationChanged)(
char +
' ' +
// Prepend indentation spaces from original to compared line.
lineOriginal.slice(0, lineOriginal.length - lineCompared.length) +
highlightLeadingTrailingSpaces(
lineCompared,
getBgColor(digit, onlyIndentationChanged),
),
);
}
// Format compared line when expected is snapshot or multiline string.
return getColor(digit)(
char + ' ' + highlightTrailingSpaces(lineCompared, getBgColor(digit)),
);
};
// Given original lines, return callback function
// which given diff digit, returns array.
const getterForChunks = (original: Original) => {
const linesExpected = splitIntoLines(original.a);
const linesReceived = splitIntoLines(original.b);
let iExpected = 0;
let iReceived = 0;
return (digit: DIFF_DIGIT) => {
if (digit === -1) {
return [linesExpected[iExpected++]];
}
if (digit === 1) {
return [linesReceived[iReceived++]];
}
// Because compared line is equal: original received and expected lines.
return [linesReceived[iReceived++], linesExpected[iExpected++]];
};
};
// jest --expand
const formatChunks = (a: string, b: string, original?: Original): Diff => {
const chunks = diffLines(a, b);
if (chunks.every(chunk => !chunk.removed && !chunk.added)) {
return null;
}
const getOriginal = original && getterForChunks(original);
return chunks
.reduce((lines, chunk) => {
const char = getDiffChar(chunk);
splitIntoLines(chunk.value).forEach(line => {
lines.push(formatLine(char, line, getOriginal));
});
return lines;
}, [])
.join('\n');
};
// Only show patch marks ("@@ ... @@") if the diff is big.
// To determine this, we need to compare either the original string (a) to
// `hunk.oldLines` or a new string to `hunk.newLines`.
// If the `oldLinesCount` is greater than `hunk.oldLines`
// we can be sure that at least 1 line has been "hidden".
const shouldShowPatchMarks = (hunk: Hunk, oldLinesCount: number): boolean =>
oldLinesCount > hunk.oldLines;
const createPatchMark = (hunk: Hunk): string => {
const markOld = `-${hunk.oldStart},${hunk.oldLines}`;
const markNew = `+${hunk.newStart},${hunk.newLines}`;
return chalk.yellow(`@@ ${markOld} ${markNew} @@`);
};
// Given original lines, return callback function which given indexes for hunk,
// returns another callback function which given diff digit, returns array.
const getterForHunks = (original: Original) => {
const linesExpected = splitIntoLines(original.a);
const linesReceived = splitIntoLines(original.b);
return (iExpected: number, iReceived: number) => (digit: DIFF_DIGIT) => {
if (digit === -1) {
return [linesExpected[iExpected++]];
}
if (digit === 1) {
return [linesReceived[iReceived++]];
}
// Because compared line is equal: original received and expected lines.
return [linesReceived[iReceived++], linesExpected[iExpected++]];
};
};
// jest --no-expand
const formatHunks = (
a: string,
b: string,
contextLines?: number,
original?: Original,
): Diff => {
const options = {
context:
typeof contextLines === 'number' && contextLines >= 0
? contextLines
: DIFF_CONTEXT_DEFAULT,
};
const {hunks} = structuredPatch('', '', a, b, '', '', options);
if (hunks.length === 0) {
return null;
}
const getter = original && getterForHunks(original);
const oldLinesCount = (a.match(/\n/g) || []).length;
return hunks
.reduce((lines, hunk: Hunk) => {
if (shouldShowPatchMarks(hunk, oldLinesCount)) {
lines.push(createPatchMark(hunk));
}
// Hunk properties are one-based but index args are zero-based.
const getOriginal =
getter && getter(hunk.oldStart - 1, hunk.newStart - 1);
hunk.lines.forEach(line => {
lines.push(formatLine(line[0], line.slice(1), getOriginal));
});
return lines;
}, [])
.join('\n');
};
export default function diffStrings(
a: string,
b: string,
options: ?DiffOptions,
original?: Original,
): string {
// Because `formatHunks` and `formatChunks` ignore one trailing newline,
// always append newline to strings:
a += '\n';
b += '\n';
// `diff` uses the Myers LCS diff algorithm which runs in O(n+d^2) time
// (where "d" is the edit distance) and can get very slow for large edit
// distances. Mitigate the cost by switching to a lower-resolution diff
// whenever linebreaks are involved.
const result =
options && options.expand === false
? formatHunks(a, b, options && options.contextLines, original)
: formatChunks(a, b, original);
return result === null ? NO_DIFF_MESSAGE : getAnnotation(options) + result;
}