Skip to content

Commit 37059e4

Browse files
committed
deps: diff@8.0.4
(cherry picked from commit 6faa25e)
1 parent fb450ab commit 37059e4

11 files changed

Lines changed: 280 additions & 129 deletions

File tree

node_modules/diff/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ A full release may be completed by first updating the `"version"` property in pa
1919
```
2020
yarn clean
2121
yarn build
22-
yarn publish
22+
yarn npm publish
2323
```
2424

2525
After releasing, remember to:

node_modules/diff/dist/diff.js

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,46 @@
368368
function hasOnlyUnixLineEndings(string) {
369369
return !string.includes('\r\n') && string.includes('\n');
370370
}
371-
function trailingWs(string) {
371+
/**
372+
* Split a string into segments using a word segmenter, merging consecutive
373+
* segments if they are both whitespace segments. Whitespace segments can
374+
* appear adjacent to one another for two reasons:
375+
* - newlines always get their own segment
376+
* - where a diacritic is attached to a whitespace character in the text, the
377+
* segment ends after the diacritic, so e.g. " \u0300 " becomes two segments.
378+
* This function therefore runs the segmenter's .segment() method and then
379+
* merges consecutive segments of whitespace into a single part.
380+
*/
381+
function segment(string, segmenter) {
382+
const parts = [];
383+
for (const segmentObj of Array.from(segmenter.segment(string))) {
384+
const segment = segmentObj.segment;
385+
if (parts.length && (/\s/).test(parts[parts.length - 1]) && (/\s/).test(segment)) {
386+
parts[parts.length - 1] += segment;
387+
}
388+
else {
389+
parts.push(segment);
390+
}
391+
}
392+
return parts;
393+
}
394+
// The functions below take a `segmenter` argument so that, when called from
395+
// diffWords when it is using a segmenter, they can use a notion of what
396+
// constitutes "whitespace" that is consistent with the segmenter.
397+
//
398+
// USUALLY this will be identical to the result of the non-segmenter-based
399+
// logic, but it differs in at least one case: when whitespace characters are
400+
// modified by diacritics. A word segmenter considers these diacritics to be
401+
// part of the whitespace, whereas our non-segmenter-based logic does not.
402+
//
403+
// Because the segmenter-based approach necessarily requires segmenting the
404+
// entire string, we offer a leadingAndTrailingWs function to allow getting the
405+
// whitespace prefix AND whitespace suffix with a single call to the segmenter,
406+
// for efficiency's sake.
407+
function trailingWs(string, segmenter) {
408+
if (segmenter) {
409+
return leadingAndTrailingWs(string, segmenter)[1];
410+
}
372411
// Yes, this looks overcomplicated and dumb - why not replace the whole function with
373412
// return string.match(/\s*$/)[0]
374413
// you ask? Because:
@@ -388,11 +427,28 @@
388427
}
389428
return string.substring(i + 1);
390429
}
391-
function leadingWs(string) {
430+
function leadingWs(string, segmenter) {
431+
if (segmenter) {
432+
return leadingAndTrailingWs(string, segmenter)[0];
433+
}
392434
// Thankfully the annoying considerations described in trailingWs don't apply here:
393435
const match = string.match(/^\s*/);
394436
return match ? match[0] : '';
395437
}
438+
function leadingAndTrailingWs(string, segmenter) {
439+
if (!segmenter) {
440+
return [leadingWs(string), trailingWs(string)];
441+
}
442+
if (segmenter.resolvedOptions().granularity != 'word') {
443+
throw new Error('The segmenter passed must have a granularity of "word"');
444+
}
445+
const segments = segment(string, segmenter);
446+
const firstSeg = segments[0];
447+
const lastSeg = segments[segments.length - 1];
448+
const head = (/\s/).test(firstSeg) ? firstSeg : '';
449+
const tail = (/\s/).test(lastSeg) ? lastSeg : '';
450+
return [head, tail];
451+
}
396452

397453
// Based on https://en.wikipedia.org/wiki/Latin_script_in_Unicode
398454
//
@@ -458,22 +514,9 @@
458514
// We want `parts` to be an array whose elements alternate between being
459515
// pure whitespace and being pure non-whitespace. This is ALMOST what the
460516
// segments returned by a word-based Intl.Segmenter already look like,
461-
// and therefore we can ALMOST get what we want by simply doing...
462-
// parts = Array.from(segmenter.segment(value), segment => segment.segment);
463-
// ... but not QUITE, because there's of one annoying special case: every
464-
// newline character gets its own segment, instead of sharing a segment
465-
// with other surrounding whitespace. We therefore need to manually merge
466-
// consecutive segments of whitespace into a single part:
467-
parts = [];
468-
for (const segmentObj of Array.from(segmenter.segment(value))) {
469-
const segment = segmentObj.segment;
470-
if (parts.length && (/\s/).test(parts[parts.length - 1]) && (/\s/).test(segment)) {
471-
parts[parts.length - 1] += segment;
472-
}
473-
else {
474-
parts.push(segment);
475-
}
476-
}
517+
// but not quite - see explanation in the docs of our custom segment()
518+
// function.
519+
parts = segment(value, segmenter);
477520
}
478521
else {
479522
parts = value.match(tokenizeIncludingWhitespace) || [];
@@ -537,15 +580,15 @@
537580
}
538581
else {
539582
if (insertion || deletion) { // May be false at start of text
540-
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, change);
583+
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, change, options.intlSegmenter);
541584
}
542585
lastKeep = change;
543586
insertion = null;
544587
deletion = null;
545588
}
546589
});
547590
if (insertion || deletion) {
548-
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, null);
591+
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, null, options.intlSegmenter);
549592
}
550593
return changes;
551594
}
@@ -561,7 +604,7 @@
561604
}
562605
return wordDiff.diff(oldStr, newStr, options);
563606
}
564-
function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep) {
607+
function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep, segmenter) {
565608
// Before returning, we tidy up the leading and trailing whitespace of the
566609
// change objects to eliminate cases where trailing whitespace in one object
567610
// is repeated as leading whitespace in the next.
@@ -604,10 +647,8 @@
604647
// * Just a "delete"
605648
// We handle the three cases separately.
606649
if (deletion && insertion) {
607-
const oldWsPrefix = leadingWs(deletion.value);
608-
const oldWsSuffix = trailingWs(deletion.value);
609-
const newWsPrefix = leadingWs(insertion.value);
610-
const newWsSuffix = trailingWs(insertion.value);
650+
const [oldWsPrefix, oldWsSuffix] = leadingAndTrailingWs(deletion.value, segmenter);
651+
const [newWsPrefix, newWsSuffix] = leadingAndTrailingWs(insertion.value, segmenter);
611652
if (startKeep) {
612653
const commonWsPrefix = longestCommonPrefix(oldWsPrefix, newWsPrefix);
613654
startKeep.value = replaceSuffix(startKeep.value, newWsPrefix, commonWsPrefix);
@@ -629,17 +670,17 @@
629670
// whitespace and deleting duplicate leading whitespace where
630671
// present.
631672
if (startKeep) {
632-
const ws = leadingWs(insertion.value);
673+
const ws = leadingWs(insertion.value, segmenter);
633674
insertion.value = insertion.value.substring(ws.length);
634675
}
635676
if (endKeep) {
636-
const ws = leadingWs(endKeep.value);
677+
const ws = leadingWs(endKeep.value, segmenter);
637678
endKeep.value = endKeep.value.substring(ws.length);
638679
}
639680
// otherwise we've got a deletion and no insertion
640681
}
641682
else if (startKeep && endKeep) {
642-
const newWsFull = leadingWs(endKeep.value), delWsStart = leadingWs(deletion.value), delWsEnd = trailingWs(deletion.value);
683+
const newWsFull = leadingWs(endKeep.value, segmenter), [delWsStart, delWsEnd] = leadingAndTrailingWs(deletion.value, segmenter);
643684
// Any whitespace that comes straight after startKeep in both the old and
644685
// new texts, assign to startKeep and remove from the deletion.
645686
const newWsStart = longestCommonPrefix(newWsFull, delWsStart);
@@ -658,17 +699,17 @@
658699
// We are at the start of the text. Preserve all the whitespace on
659700
// endKeep, and just remove whitespace from the end of deletion to the
660701
// extent that it overlaps with the start of endKeep.
661-
const endKeepWsPrefix = leadingWs(endKeep.value);
662-
const deletionWsSuffix = trailingWs(deletion.value);
702+
const endKeepWsPrefix = leadingWs(endKeep.value, segmenter);
703+
const deletionWsSuffix = trailingWs(deletion.value, segmenter);
663704
const overlap = maximumOverlap(deletionWsSuffix, endKeepWsPrefix);
664705
deletion.value = removeSuffix(deletion.value, overlap);
665706
}
666707
else if (startKeep) {
667708
// We are at the END of the text. Preserve all the whitespace on
668709
// startKeep, and just remove whitespace from the start of deletion to
669710
// the extent that it overlaps with the end of startKeep.
670-
const startKeepWsSuffix = trailingWs(startKeep.value);
671-
const deletionWsPrefix = leadingWs(deletion.value);
711+
const startKeepWsSuffix = trailingWs(startKeep.value, segmenter);
712+
const deletionWsPrefix = leadingWs(deletion.value, segmenter);
672713
const overlap = maximumOverlap(startKeepWsSuffix, deletionWsPrefix);
673714
deletion.value = removePrefix(deletion.value, overlap);
674715
}

node_modules/diff/dist/diff.min.js

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

node_modules/diff/libcjs/diff/word.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,9 @@ var WordDiff = /** @class */ (function (_super) {
8989
// We want `parts` to be an array whose elements alternate between being
9090
// pure whitespace and being pure non-whitespace. This is ALMOST what the
9191
// segments returned by a word-based Intl.Segmenter already look like,
92-
// and therefore we can ALMOST get what we want by simply doing...
93-
// parts = Array.from(segmenter.segment(value), segment => segment.segment);
94-
// ... but not QUITE, because there's of one annoying special case: every
95-
// newline character gets its own segment, instead of sharing a segment
96-
// with other surrounding whitespace. We therefore need to manually merge
97-
// consecutive segments of whitespace into a single part:
98-
parts = [];
99-
for (var _i = 0, _a = Array.from(segmenter.segment(value)); _i < _a.length; _i++) {
100-
var segmentObj = _a[_i];
101-
var segment = segmentObj.segment;
102-
if (parts.length && (/\s/).test(parts[parts.length - 1]) && (/\s/).test(segment)) {
103-
parts[parts.length - 1] += segment;
104-
}
105-
else {
106-
parts.push(segment);
107-
}
108-
}
92+
// but not quite - see explanation in the docs of our custom segment()
93+
// function.
94+
parts = (0, string_js_1.segment)(value, segmenter);
10995
}
11096
else {
11197
parts = value.match(tokenizeIncludingWhitespace) || [];
@@ -169,15 +155,15 @@ var WordDiff = /** @class */ (function (_super) {
169155
}
170156
else {
171157
if (insertion || deletion) { // May be false at start of text
172-
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, change);
158+
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, change, options.intlSegmenter);
173159
}
174160
lastKeep = change;
175161
insertion = null;
176162
deletion = null;
177163
}
178164
});
179165
if (insertion || deletion) {
180-
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, null);
166+
dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, null, options.intlSegmenter);
181167
}
182168
return changes;
183169
};
@@ -194,7 +180,7 @@ function diffWords(oldStr, newStr, options) {
194180
}
195181
return exports.wordDiff.diff(oldStr, newStr, options);
196182
}
197-
function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep) {
183+
function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep, segmenter) {
198184
// Before returning, we tidy up the leading and trailing whitespace of the
199185
// change objects to eliminate cases where trailing whitespace in one object
200186
// is repeated as leading whitespace in the next.
@@ -237,10 +223,8 @@ function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep
237223
// * Just a "delete"
238224
// We handle the three cases separately.
239225
if (deletion && insertion) {
240-
var oldWsPrefix = (0, string_js_1.leadingWs)(deletion.value);
241-
var oldWsSuffix = (0, string_js_1.trailingWs)(deletion.value);
242-
var newWsPrefix = (0, string_js_1.leadingWs)(insertion.value);
243-
var newWsSuffix = (0, string_js_1.trailingWs)(insertion.value);
226+
var _a = (0, string_js_1.leadingAndTrailingWs)(deletion.value, segmenter), oldWsPrefix = _a[0], oldWsSuffix = _a[1];
227+
var _b = (0, string_js_1.leadingAndTrailingWs)(insertion.value, segmenter), newWsPrefix = _b[0], newWsSuffix = _b[1];
244228
if (startKeep) {
245229
var commonWsPrefix = (0, string_js_1.longestCommonPrefix)(oldWsPrefix, newWsPrefix);
246230
startKeep.value = (0, string_js_1.replaceSuffix)(startKeep.value, newWsPrefix, commonWsPrefix);
@@ -262,17 +246,17 @@ function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep
262246
// whitespace and deleting duplicate leading whitespace where
263247
// present.
264248
if (startKeep) {
265-
var ws = (0, string_js_1.leadingWs)(insertion.value);
249+
var ws = (0, string_js_1.leadingWs)(insertion.value, segmenter);
266250
insertion.value = insertion.value.substring(ws.length);
267251
}
268252
if (endKeep) {
269-
var ws = (0, string_js_1.leadingWs)(endKeep.value);
253+
var ws = (0, string_js_1.leadingWs)(endKeep.value, segmenter);
270254
endKeep.value = endKeep.value.substring(ws.length);
271255
}
272256
// otherwise we've got a deletion and no insertion
273257
}
274258
else if (startKeep && endKeep) {
275-
var newWsFull = (0, string_js_1.leadingWs)(endKeep.value), delWsStart = (0, string_js_1.leadingWs)(deletion.value), delWsEnd = (0, string_js_1.trailingWs)(deletion.value);
259+
var newWsFull = (0, string_js_1.leadingWs)(endKeep.value, segmenter), _c = (0, string_js_1.leadingAndTrailingWs)(deletion.value, segmenter), delWsStart = _c[0], delWsEnd = _c[1];
276260
// Any whitespace that comes straight after startKeep in both the old and
277261
// new texts, assign to startKeep and remove from the deletion.
278262
var newWsStart = (0, string_js_1.longestCommonPrefix)(newWsFull, delWsStart);
@@ -291,17 +275,17 @@ function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep
291275
// We are at the start of the text. Preserve all the whitespace on
292276
// endKeep, and just remove whitespace from the end of deletion to the
293277
// extent that it overlaps with the start of endKeep.
294-
var endKeepWsPrefix = (0, string_js_1.leadingWs)(endKeep.value);
295-
var deletionWsSuffix = (0, string_js_1.trailingWs)(deletion.value);
278+
var endKeepWsPrefix = (0, string_js_1.leadingWs)(endKeep.value, segmenter);
279+
var deletionWsSuffix = (0, string_js_1.trailingWs)(deletion.value, segmenter);
296280
var overlap = (0, string_js_1.maximumOverlap)(deletionWsSuffix, endKeepWsPrefix);
297281
deletion.value = (0, string_js_1.removeSuffix)(deletion.value, overlap);
298282
}
299283
else if (startKeep) {
300284
// We are at the END of the text. Preserve all the whitespace on
301285
// startKeep, and just remove whitespace from the start of deletion to
302286
// the extent that it overlaps with the end of startKeep.
303-
var startKeepWsSuffix = (0, string_js_1.trailingWs)(startKeep.value);
304-
var deletionWsPrefix = (0, string_js_1.leadingWs)(deletion.value);
287+
var startKeepWsSuffix = (0, string_js_1.trailingWs)(startKeep.value, segmenter);
288+
var deletionWsPrefix = (0, string_js_1.leadingWs)(deletion.value, segmenter);
305289
var overlap = (0, string_js_1.maximumOverlap)(startKeepWsSuffix, deletionWsPrefix);
306290
deletion.value = (0, string_js_1.removePrefix)(deletion.value, overlap);
307291
}

0 commit comments

Comments
 (0)