MCPcopy
hub / github.com/jestjs/jest / diff_cleanupSemanticLossless

Function diff_cleanupSemanticLossless

packages/jest-diff/src/cleanupSemantic.ts:294–411  ·  view source on GitHub ↗
(diffs: Array<Diff>)

Source from the content-addressed store, hash-verified

292 * @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
293 */
294var diff_cleanupSemanticLossless = function(diffs: Array<Diff>) {
295 /**
296 * Given two strings, compute a score representing whether the internal
297 * boundary falls on logical boundaries.
298 * Scores range from 6 (best) to 0 (worst).
299 * Closure, but does not reference any external variables.
300 * @param {string} one First string.
301 * @param {string} two Second string.
302 * @return {number} The score.
303 * @private
304 */
305 function diff_cleanupSemanticScore_(one: string, two: string): number {
306 if (!one || !two) {
307 // Edges are the best.
308 return 6;
309 }
310
311 // Each port of this function behaves slightly differently due to
312 // subtle differences in each language's definition of things like
313 // 'whitespace'. Since this function's purpose is largely cosmetic,
314 // the choice has been made to use each language's native features
315 // rather than force total conformity.
316 var char1 = one.charAt(one.length - 1);
317 var char2 = two.charAt(0);
318 var nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_);
319 var nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_);
320 var whitespace1 = nonAlphaNumeric1 &&
321 char1.match(whitespaceRegex_);
322 var whitespace2 = nonAlphaNumeric2 &&
323 char2.match(whitespaceRegex_);
324 var lineBreak1 = whitespace1 &&
325 char1.match(linebreakRegex_);
326 var lineBreak2 = whitespace2 &&
327 char2.match(linebreakRegex_);
328 var blankLine1 = lineBreak1 &&
329 one.match(blanklineEndRegex_);
330 var blankLine2 = lineBreak2 &&
331 two.match(blanklineStartRegex_);
332
333 if (blankLine1 || blankLine2) {
334 // Five points for blank lines.
335 return 5;
336 } else if (lineBreak1 || lineBreak2) {
337 // Four points for line breaks.
338 return 4;
339 } else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
340 // Three points for end of sentences.
341 return 3;
342 } else if (whitespace1 || whitespace2) {
343 // Two points for whitespace.
344 return 2;
345 } else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
346 // One point for non-alphanumeric.
347 return 1;
348 }
349 return 0;
350 }
351

Callers 1

diff_cleanupSemanticFunction · 0.85

Calls 2

diff_commonSuffixFunction · 0.85

Tested by

no test coverage detected