Skip to content

Commit 084eb40

Browse files
authored
Merge pull request #369 from huggingface/diff-max-line-length
feature: `diffMaxLineLength` to mark file diff as too big depending on diff line length
2 parents 51d19eb + bf7b52a commit 084eb40

File tree

3 files changed

+234
-4
lines changed

3 files changed

+234
-4
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ The HTML output accepts a Javascript object with configuration. Possible options
151151
- `diffStyle`: show differences level in each line: `word` or `char`, default is `word`
152152
- `diffMaxChanges`: number of changed lines after which a file diff is deemed as too big and not displayed, default is
153153
`undefined`
154-
- `diffTooBigMessage`: function allowing to customize the message in case of file diff too big (if `diffMaxChanges` is
155-
set)
154+
- `diffMaxLineLength`: number of characters in a diff line after which a file diff is deemed as too big and not
155+
displayed, default is `undefined`
156+
- `diffTooBigMessage`: function allowing to customize the message in case of file diff too big (if `diffMaxChanges` or
157+
`diffMaxLineLength` is set)
156158
- `matching`: matching level: `'lines'` for matching lines, `'words'` for matching lines and words or `'none'`, default
157159
is `none`
158160
- `matchWordsThreshold`: similarity threshold for word matching, default is `0.25`

src/__tests__/diff-parser-tests.ts

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,5 +2201,231 @@ describe('DiffParser', () => {
22012201
]
22022202
`);
22032203
});
2204+
2205+
it('should work when `diffMaxLineLength` is set and excedeed', () => {
2206+
const diff =
2207+
'diff --git a/src/core/init.js b/src/core/init.js\n' +
2208+
'index e49196a..50f310c 100644\n' +
2209+
'--- a/src/core/init.js\n' +
2210+
'+++ b/src/core/init.js\n' +
2211+
'@@ -101,7 +101,7 @@ var rootjQuery,\n' +
2212+
' // HANDLE: $(function)\n' +
2213+
' // Shortcut for document ready\n' +
2214+
' } else if ( jQuery.isFunction( selector ) ) {\n' +
2215+
'- return typeof rootjQuery.ready !== "undefined" ?\n' +
2216+
'+ return rootjQuery.ready !== undefined ?\n' +
2217+
' rootjQuery.ready( selector ) :\n' +
2218+
' // Execute immediately if ready is not present\n' +
2219+
' selector( jQuery );\n' +
2220+
'diff --git a/src/event.js b/src/event.js\n' +
2221+
'index 7336f4d..6183f70 100644\n' +
2222+
'--- a/src/event.js\n' +
2223+
'+++ b/src/event.js\n' +
2224+
'@@ -1,6 +1,5 @@\n' +
2225+
' define([\n' +
2226+
' "./core",\n' +
2227+
'- "./var/strundefined",\n' +
2228+
' "./var/rnotwhite",\n' +
2229+
' "./var/hasOwn",\n' +
2230+
' "./var/slice",\n';
2231+
const result = parse(diff, { diffMaxLineLength: 50 });
2232+
expect(result).toMatchInlineSnapshot(`
2233+
Array [
2234+
Object {
2235+
"addedLines": 0,
2236+
"blocks": Array [
2237+
Object {
2238+
"header": "Diff too big to be displayed",
2239+
"lines": Array [],
2240+
"newStartLine": 0,
2241+
"oldStartLine": 0,
2242+
"oldStartLine2": null,
2243+
},
2244+
],
2245+
"checksumAfter": "50f310c",
2246+
"checksumBefore": "e49196a",
2247+
"deletedLines": 0,
2248+
"isCombined": false,
2249+
"isGitDiff": true,
2250+
"isTooBig": true,
2251+
"language": "js",
2252+
"mode": "100644",
2253+
"newName": "src/core/init.js",
2254+
"oldName": "src/core/init.js",
2255+
},
2256+
Object {
2257+
"addedLines": 0,
2258+
"blocks": Array [
2259+
Object {
2260+
"header": "@@ -1,6 +1,5 @@",
2261+
"lines": Array [
2262+
Object {
2263+
"content": " define([",
2264+
"newNumber": 1,
2265+
"oldNumber": 1,
2266+
"type": "context",
2267+
},
2268+
Object {
2269+
"content": " \\"./core\\",",
2270+
"newNumber": 2,
2271+
"oldNumber": 2,
2272+
"type": "context",
2273+
},
2274+
Object {
2275+
"content": "- \\"./var/strundefined\\",",
2276+
"newNumber": undefined,
2277+
"oldNumber": 3,
2278+
"type": "delete",
2279+
},
2280+
Object {
2281+
"content": " \\"./var/rnotwhite\\",",
2282+
"newNumber": 3,
2283+
"oldNumber": 4,
2284+
"type": "context",
2285+
},
2286+
Object {
2287+
"content": " \\"./var/hasOwn\\",",
2288+
"newNumber": 4,
2289+
"oldNumber": 5,
2290+
"type": "context",
2291+
},
2292+
Object {
2293+
"content": " \\"./var/slice\\",",
2294+
"newNumber": 5,
2295+
"oldNumber": 6,
2296+
"type": "context",
2297+
},
2298+
],
2299+
"newStartLine": 1,
2300+
"oldStartLine": 1,
2301+
"oldStartLine2": null,
2302+
},
2303+
],
2304+
"checksumAfter": "6183f70",
2305+
"checksumBefore": "7336f4d",
2306+
"deletedLines": 1,
2307+
"isCombined": false,
2308+
"isGitDiff": true,
2309+
"language": "js",
2310+
"mode": "100644",
2311+
"newName": "src/event.js",
2312+
"oldName": "src/event.js",
2313+
},
2314+
]
2315+
`);
2316+
});
2317+
2318+
it('should work when `diffMaxLineLength` is set and excedeed, and `diffTooBigMessage` is set', () => {
2319+
const diff =
2320+
'diff --git a/src/core/init.js b/src/core/init.js\n' +
2321+
'index e49196a..50f310c 100644\n' +
2322+
'--- a/src/core/init.js\n' +
2323+
'+++ b/src/core/init.js\n' +
2324+
'@@ -101,7 +101,7 @@ var rootjQuery,\n' +
2325+
' // HANDLE: $(function)\n' +
2326+
' // Shortcut for document ready\n' +
2327+
' } else if ( jQuery.isFunction( selector ) ) {\n' +
2328+
'- return typeof rootjQuery.ready !== "undefined" ?\n' +
2329+
'+ return rootjQuery.ready !== undefined ?\n' +
2330+
' rootjQuery.ready( selector ) :\n' +
2331+
' // Execute immediately if ready is not present\n' +
2332+
' selector( jQuery );\n' +
2333+
'diff --git a/src/event.js b/src/event.js\n' +
2334+
'index 7336f4d..6183f70 100644\n' +
2335+
'--- a/src/event.js\n' +
2336+
'+++ b/src/event.js\n' +
2337+
'@@ -1,6 +1,5 @@\n' +
2338+
' define([\n' +
2339+
' "./core",\n' +
2340+
'- "./var/strundefined",\n' +
2341+
' "./var/rnotwhite",\n' +
2342+
' "./var/hasOwn",\n' +
2343+
' "./var/slice",\n';
2344+
const result = parse(diff, { diffMaxLineLength: 50, diffTooBigMessage: (i: number) => `Custom ${i}` });
2345+
expect(result).toMatchInlineSnapshot(`
2346+
Array [
2347+
Object {
2348+
"addedLines": 0,
2349+
"blocks": Array [
2350+
Object {
2351+
"header": "Custom 0",
2352+
"lines": Array [],
2353+
"newStartLine": 0,
2354+
"oldStartLine": 0,
2355+
"oldStartLine2": null,
2356+
},
2357+
],
2358+
"checksumAfter": "50f310c",
2359+
"checksumBefore": "e49196a",
2360+
"deletedLines": 0,
2361+
"isCombined": false,
2362+
"isGitDiff": true,
2363+
"isTooBig": true,
2364+
"language": "js",
2365+
"mode": "100644",
2366+
"newName": "src/core/init.js",
2367+
"oldName": "src/core/init.js",
2368+
},
2369+
Object {
2370+
"addedLines": 0,
2371+
"blocks": Array [
2372+
Object {
2373+
"header": "@@ -1,6 +1,5 @@",
2374+
"lines": Array [
2375+
Object {
2376+
"content": " define([",
2377+
"newNumber": 1,
2378+
"oldNumber": 1,
2379+
"type": "context",
2380+
},
2381+
Object {
2382+
"content": " \\"./core\\",",
2383+
"newNumber": 2,
2384+
"oldNumber": 2,
2385+
"type": "context",
2386+
},
2387+
Object {
2388+
"content": "- \\"./var/strundefined\\",",
2389+
"newNumber": undefined,
2390+
"oldNumber": 3,
2391+
"type": "delete",
2392+
},
2393+
Object {
2394+
"content": " \\"./var/rnotwhite\\",",
2395+
"newNumber": 3,
2396+
"oldNumber": 4,
2397+
"type": "context",
2398+
},
2399+
Object {
2400+
"content": " \\"./var/hasOwn\\",",
2401+
"newNumber": 4,
2402+
"oldNumber": 5,
2403+
"type": "context",
2404+
},
2405+
Object {
2406+
"content": " \\"./var/slice\\",",
2407+
"newNumber": 5,
2408+
"oldNumber": 6,
2409+
"type": "context",
2410+
},
2411+
],
2412+
"newStartLine": 1,
2413+
"oldStartLine": 1,
2414+
"oldStartLine2": null,
2415+
},
2416+
],
2417+
"checksumAfter": "6183f70",
2418+
"checksumBefore": "7336f4d",
2419+
"deletedLines": 1,
2420+
"isCombined": false,
2421+
"isGitDiff": true,
2422+
"language": "js",
2423+
"mode": "100644",
2424+
"newName": "src/event.js",
2425+
"oldName": "src/event.js",
2426+
},
2427+
]
2428+
`);
2429+
});
22042430
});
22052431
});

src/diff-parser.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface DiffParserConfig {
55
srcPrefix?: string;
66
dstPrefix?: string;
77
diffMaxChanges?: number;
8+
diffMaxLineLength?: number;
89
diffTooBigMessage?: (fileIndex: number) => string;
910
}
1011

@@ -308,8 +309,9 @@ export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFil
308309

309310
if (
310311
currentFile &&
311-
typeof config.diffMaxChanges === 'number' &&
312-
currentFile.addedLines + currentFile.deletedLines > config.diffMaxChanges
312+
((typeof config.diffMaxChanges === 'number' &&
313+
currentFile.addedLines + currentFile.deletedLines > config.diffMaxChanges) ||
314+
(typeof config.diffMaxLineLength === 'number' && line.length > config.diffMaxLineLength))
313315
) {
314316
currentFile.isTooBig = true;
315317
currentFile.addedLines = 0;

0 commit comments

Comments
 (0)