-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: use "concordance" package to display diff instead of using cus…
…tom diff (#2828)
- Loading branch information
1 parent
287dc20
commit 446308d
Showing
29 changed files
with
333 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1 @@ | ||
import * as diff from 'diff' | ||
|
||
export interface DiffOptions { | ||
outputTruncateLength?: number | ||
outputDiffLines?: number | ||
showLegend?: boolean | ||
} | ||
|
||
function formatLine(line: string, maxWidth: number) { | ||
return line.slice(0, maxWidth) + (line.length > maxWidth ? '…' : '') | ||
} | ||
|
||
export function unifiedDiff(actual: string, expected: string, options: DiffOptions = {}) { | ||
if (actual === expected) | ||
return '' | ||
|
||
const { outputTruncateLength = 80, outputDiffLines, showLegend = true } = options | ||
|
||
const indent = ' ' | ||
const diffLimit = outputDiffLines || 15 | ||
|
||
const counts = { | ||
'+': 0, | ||
'-': 0, | ||
} | ||
let previousState: '-' | '+' | null = null | ||
let previousCount = 0 | ||
function preprocess(line: string) { | ||
if (!line || line.match(/\\ No newline/)) | ||
return | ||
|
||
const char = line[0] as '+' | '-' | ||
if ('-+'.includes(char)) { | ||
if (previousState !== char) { | ||
previousState = char | ||
previousCount = 0 | ||
} | ||
previousCount++ | ||
counts[char]++ | ||
if (previousCount === diffLimit) | ||
return `${char} ...` | ||
else if (previousCount > diffLimit) | ||
return | ||
} | ||
return line | ||
} | ||
|
||
const msg = diff.createPatch('string', expected, actual) | ||
const lines = msg.split('\n').slice(5).map(preprocess).filter(Boolean) as string[] | ||
const isCompact = counts['+'] === 1 && counts['-'] === 1 && lines.length === 2 | ||
|
||
let formatted = lines.map((line: string) => { | ||
line = line.replace(/\\"/g, '"') | ||
if (line[0] === '-') { | ||
line = formatLine(line.slice(1), outputTruncateLength) | ||
if (isCompact) | ||
return line | ||
return `- ${formatLine(line, outputTruncateLength)}` | ||
} | ||
if (line[0] === '+') { | ||
line = formatLine(line.slice(1), outputTruncateLength) | ||
if (isCompact) | ||
return line | ||
return `+ ${formatLine(line, outputTruncateLength)}` | ||
} | ||
if (line.match(/@@/)) | ||
return '--' | ||
return ` ${line}` | ||
}) | ||
|
||
if (showLegend) { | ||
// Compact mode | ||
if (isCompact) { | ||
formatted = [ | ||
`- Expected ${formatted[0]}`, | ||
`+ Received ${formatted[1]}`, | ||
] | ||
} | ||
else { | ||
if (formatted[0].includes('"')) | ||
formatted[0] = formatted[0].replace('"', '') | ||
|
||
const last = formatted.length - 1 | ||
if (formatted[last].endsWith('"')) | ||
formatted[last] = formatted[last].slice(0, formatted[last].length - 1) | ||
|
||
formatted.unshift( | ||
`- Expected - ${counts['-']}`, | ||
`+ Received + ${counts['+']}`, | ||
'', | ||
) | ||
} | ||
} | ||
|
||
return formatted.map(i => indent + i).join('\n') | ||
} | ||
export { unifiedDiff } from '@vitest/utils/diff' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.