MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / extractValidLineRanges

Function extractValidLineRanges

src/codeScan/util/diffLineRanges.ts:60–149  ·  view source on GitHub ↗
(unifiedDiff: string)

Source from the content-addressed store, hash-verified

58 * ```
59 */
60export function extractValidLineRanges(unifiedDiff: string): FileLineRanges {
61 const ranges: FileLineRanges = new Map();
62
63 if (!unifiedDiff || unifiedDiff.trim() === '') {
64 return ranges;
65 }
66
67 const lines = unifiedDiff.split('\n');
68 let currentFile: string | null = null;
69 let currentRanges: LineRange[] = [];
70 let currentNewLine = 0;
71 let hunkStartLine = 0;
72
73 for (const line of lines) {
74 // Match file header: diff --git a/path b/path
75 // or +++ b/path (for new file path)
76 const fileMatch = line.match(/^\+\+\+ b\/(.+)$/);
77 if (fileMatch) {
78 // Close current hunk if we have one (before switching files)
79 if (currentFile && hunkStartLine > 0 && currentNewLine > hunkStartLine) {
80 currentRanges.push({
81 start: hunkStartLine,
82 end: currentNewLine - 1,
83 });
84 }
85
86 // Save previous file's ranges
87 if (currentFile && currentRanges.length > 0) {
88 ranges.set(currentFile, currentRanges);
89 }
90
91 currentFile = fileMatch[1];
92 currentRanges = [];
93 currentNewLine = 0;
94 hunkStartLine = 0;
95 continue;
96 }
97
98 // Match hunk header: @@ -old,count +new,count @@
99 const hunkHeader = parseHunkHeader(line);
100 if (hunkHeader && currentFile) {
101 // Save the previous hunk's range if we have one
102 if (hunkStartLine > 0 && currentNewLine > hunkStartLine) {
103 currentRanges.push({
104 start: hunkStartLine,
105 end: currentNewLine - 1,
106 });
107 }
108
109 // Start tracking new hunk
110 hunkStartLine = hunkHeader.newStart;
111 currentNewLine = hunkHeader.newStart;
112
113 // For hunks with 0 lines in new file (pure deletion), don't start a range
114 if (hunkHeader.newCount === 0) {
115 hunkStartLine = 0;
116 }
117 continue;

Callers 2

getPRDiffRangesFunction · 0.90

Calls 4

parseHunkHeaderFunction · 0.90
splitMethod · 0.80
pushMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…