Skip to content

Commit

Permalink
Flatten temporary data structures in jest-diff (#4469)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and cpojer committed Sep 12, 2017
1 parent 72af99d commit c4844a6
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions packages/jest-diff/src/diff_strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Original = {|
b: string,
|};

type Diff = {diff: string, isDifferent: boolean};
type Diff = string | null;

type Hunk = {|
lines: Array<string>,
Expand Down Expand Up @@ -159,25 +159,23 @@ const getterForChunks = (original: Original) => {

// 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);
let isDifferent = false;

return {
diff: diffLines(a, b)
.map(chunk => {
const {added, removed, value} = chunk;
if (added || removed) {
isDifferent = true;
}
const char = getDiffChar(chunk);

return splitIntoLines(value)
.map(line => formatLine(char, line, getOriginal))
.join('\n');
})
.join('\n'),
isDifferent,
};
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.
Expand Down Expand Up @@ -225,8 +223,6 @@ const formatHunks = (
? contextLines
: DIFF_CONTEXT_DEFAULT,
};
const getter = original && getterForHunks(original);
let isDifferent = false;
// Make sure the strings end with a newline.
if (!a.endsWith('\n')) {
a += '\n';
Expand All @@ -235,26 +231,27 @@ const formatHunks = (
b += '\n';
}

const oldLinesCount = (a.match(/\n/g) || []).length;
const {hunks} = structuredPatch('', '', a, b, '', '', options);
if (hunks.length === 0) {
return null;
}

return {
diff: structuredPatch('', '', a, b, '', '', options)
.hunks.map((hunk: Hunk) => {
// Hunk properties are one-based but index args are zero-based.
const getOriginal =
getter && getter(hunk.oldStart - 1, hunk.newStart - 1);
const lines = hunk.lines
.map(line => formatLine(line[0], line.slice(1), getOriginal))
.join('\n');

isDifferent = true;
return shouldShowPatchMarks(hunk, oldLinesCount)
? createPatchMark(hunk) + lines
: lines;
})
.join('\n'),
isDifferent,
};
const getter = original && getterForHunks(original);
const oldLinesCount = (a.match(/\n/g) || []).length;
return hunks
.map((hunk: Hunk) => {
// Hunk properties are one-based but index args are zero-based.
const getOriginal =
getter && getter(hunk.oldStart - 1, hunk.newStart - 1);
const lines = hunk.lines
.map(line => formatLine(line[0], line.slice(1), getOriginal))
.join('\n');

return shouldShowPatchMarks(hunk, oldLinesCount)
? createPatchMark(hunk) + lines
: lines;
})
.join('\n');
};

export default function diffStrings(
Expand All @@ -272,9 +269,5 @@ export default function diffStrings(
? formatHunks(a, b, options && options.contextLines, original)
: formatChunks(a, b, original);

if (result.isDifferent) {
return getAnnotation(options) + result.diff;
} else {
return NO_DIFF_MESSAGE;
}
return result === null ? NO_DIFF_MESSAGE : getAnnotation(options) + result;
}

0 comments on commit c4844a6

Please sign in to comment.