feature: new diffMaxLineLength

Mark a file diff as too big if diff line length > threshold
This commit is contained in:
Pierric Cistac 2021-03-11 18:10:14 -05:00
parent 51d19ebc2e
commit a4e619efa4
No known key found for this signature in database
GPG key ID: 9D98B3AF93C7362D
3 changed files with 246 additions and 2 deletions

View file

@ -151,8 +151,10 @@ The HTML output accepts a Javascript object with configuration. Possible options
- `diffStyle`: show differences level in each line: `word` or `char`, default is `word`
- `diffMaxChanges`: number of changed lines after which a file diff is deemed as too big and not displayed, default is
`undefined`
- `diffTooBigMessage`: function allowing to customize the message in case of file diff too big (if `diffMaxChanges` is
set)
- `diffMaxLineLength`: number of characters in a diff line after which a file diff is deemed as too big and not
displayed, default is `undefined`
- `diffTooBigMessage`: function allowing to customize the message in case of file diff too big (if `diffMaxChanges` or
`diffMaxLineLength` is set)
- `matching`: matching level: `'lines'` for matching lines, `'words'` for matching lines and words or `'none'`, default
is `none`
- `matchWordsThreshold`: similarity threshold for word matching, default is `0.25`

View file

@ -2201,5 +2201,231 @@ describe('DiffParser', () => {
]
`);
});
it('should work when `diffMaxLineLength` is set and excedeed', () => {
const diff =
'diff --git a/src/core/init.js b/src/core/init.js\n' +
'index e49196a..50f310c 100644\n' +
'--- a/src/core/init.js\n' +
'+++ b/src/core/init.js\n' +
'@@ -101,7 +101,7 @@ var rootjQuery,\n' +
' // HANDLE: $(function)\n' +
' // Shortcut for document ready\n' +
' } else if ( jQuery.isFunction( selector ) ) {\n' +
'- return typeof rootjQuery.ready !== "undefined" ?\n' +
'+ return rootjQuery.ready !== undefined ?\n' +
' rootjQuery.ready( selector ) :\n' +
' // Execute immediately if ready is not present\n' +
' selector( jQuery );\n' +
'diff --git a/src/event.js b/src/event.js\n' +
'index 7336f4d..6183f70 100644\n' +
'--- a/src/event.js\n' +
'+++ b/src/event.js\n' +
'@@ -1,6 +1,5 @@\n' +
' define([\n' +
' "./core",\n' +
'- "./var/strundefined",\n' +
' "./var/rnotwhite",\n' +
' "./var/hasOwn",\n' +
' "./var/slice",\n';
const result = parse(diff, { diffMaxLineLength: 50 });
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"addedLines": 0,
"blocks": Array [
Object {
"header": "Diff too big to be displayed",
"lines": Array [],
"newStartLine": 0,
"oldStartLine": 0,
"oldStartLine2": null,
},
],
"checksumAfter": "50f310c",
"checksumBefore": "e49196a",
"deletedLines": 0,
"isCombined": false,
"isGitDiff": true,
"isTooBig": true,
"language": "js",
"mode": "100644",
"newName": "src/core/init.js",
"oldName": "src/core/init.js",
},
Object {
"addedLines": 0,
"blocks": Array [
Object {
"header": "@@ -1,6 +1,5 @@",
"lines": Array [
Object {
"content": " define([",
"newNumber": 1,
"oldNumber": 1,
"type": "context",
},
Object {
"content": " \\"./core\\",",
"newNumber": 2,
"oldNumber": 2,
"type": "context",
},
Object {
"content": "- \\"./var/strundefined\\",",
"newNumber": undefined,
"oldNumber": 3,
"type": "delete",
},
Object {
"content": " \\"./var/rnotwhite\\",",
"newNumber": 3,
"oldNumber": 4,
"type": "context",
},
Object {
"content": " \\"./var/hasOwn\\",",
"newNumber": 4,
"oldNumber": 5,
"type": "context",
},
Object {
"content": " \\"./var/slice\\",",
"newNumber": 5,
"oldNumber": 6,
"type": "context",
},
],
"newStartLine": 1,
"oldStartLine": 1,
"oldStartLine2": null,
},
],
"checksumAfter": "6183f70",
"checksumBefore": "7336f4d",
"deletedLines": 1,
"isCombined": false,
"isGitDiff": true,
"language": "js",
"mode": "100644",
"newName": "src/event.js",
"oldName": "src/event.js",
},
]
`);
});
it('should work when `diffMaxLineLength` is set and excedeed, and `diffTooBigMessage` is set', () => {
const diff =
'diff --git a/src/core/init.js b/src/core/init.js\n' +
'index e49196a..50f310c 100644\n' +
'--- a/src/core/init.js\n' +
'+++ b/src/core/init.js\n' +
'@@ -101,7 +101,7 @@ var rootjQuery,\n' +
' // HANDLE: $(function)\n' +
' // Shortcut for document ready\n' +
' } else if ( jQuery.isFunction( selector ) ) {\n' +
'- return typeof rootjQuery.ready !== "undefined" ?\n' +
'+ return rootjQuery.ready !== undefined ?\n' +
' rootjQuery.ready( selector ) :\n' +
' // Execute immediately if ready is not present\n' +
' selector( jQuery );\n' +
'diff --git a/src/event.js b/src/event.js\n' +
'index 7336f4d..6183f70 100644\n' +
'--- a/src/event.js\n' +
'+++ b/src/event.js\n' +
'@@ -1,6 +1,5 @@\n' +
' define([\n' +
' "./core",\n' +
'- "./var/strundefined",\n' +
' "./var/rnotwhite",\n' +
' "./var/hasOwn",\n' +
' "./var/slice",\n';
const result = parse(diff, { diffMaxLineLength: 50, diffTooBigMessage: (i: number) => `Custom ${i}` });
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"addedLines": 0,
"blocks": Array [
Object {
"header": "Custom 0",
"lines": Array [],
"newStartLine": 0,
"oldStartLine": 0,
"oldStartLine2": null,
},
],
"checksumAfter": "50f310c",
"checksumBefore": "e49196a",
"deletedLines": 0,
"isCombined": false,
"isGitDiff": true,
"isTooBig": true,
"language": "js",
"mode": "100644",
"newName": "src/core/init.js",
"oldName": "src/core/init.js",
},
Object {
"addedLines": 0,
"blocks": Array [
Object {
"header": "@@ -1,6 +1,5 @@",
"lines": Array [
Object {
"content": " define([",
"newNumber": 1,
"oldNumber": 1,
"type": "context",
},
Object {
"content": " \\"./core\\",",
"newNumber": 2,
"oldNumber": 2,
"type": "context",
},
Object {
"content": "- \\"./var/strundefined\\",",
"newNumber": undefined,
"oldNumber": 3,
"type": "delete",
},
Object {
"content": " \\"./var/rnotwhite\\",",
"newNumber": 3,
"oldNumber": 4,
"type": "context",
},
Object {
"content": " \\"./var/hasOwn\\",",
"newNumber": 4,
"oldNumber": 5,
"type": "context",
},
Object {
"content": " \\"./var/slice\\",",
"newNumber": 5,
"oldNumber": 6,
"type": "context",
},
],
"newStartLine": 1,
"oldStartLine": 1,
"oldStartLine2": null,
},
],
"checksumAfter": "6183f70",
"checksumBefore": "7336f4d",
"deletedLines": 1,
"isCombined": false,
"isGitDiff": true,
"language": "js",
"mode": "100644",
"newName": "src/event.js",
"oldName": "src/event.js",
},
]
`);
});
});
});

View file

@ -5,6 +5,7 @@ export interface DiffParserConfig {
srcPrefix?: string;
dstPrefix?: string;
diffMaxChanges?: number;
diffMaxLineLength?: number;
diffTooBigMessage?: (fileIndex: number) => string;
}
@ -306,6 +307,21 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
return;
}
if (currentFile && typeof config.diffMaxLineLength === 'number' && line.length > config.diffMaxLineLength) {
currentFile.isTooBig = true;
currentFile.addedLines = 0;
currentFile.deletedLines = 0;
currentFile.blocks = [];
currentBlock = null;
const message =
typeof config.diffTooBigMessage === 'function'
? config.diffTooBigMessage(files.length)
: 'Diff too big to be displayed';
startBlock(message);
return;
}
if (
currentFile &&
typeof config.diffMaxChanges === 'number' &&