Spaces:
Paused
Paused
File size: 6,241 Bytes
7da7e2c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | /**
* Patch parsing utilities
* Extracted from review.ts to improve modularity
*/
import { Project, SyntaxKind } from 'ts-morph'
export interface HunkInfo {
oldStart: number
oldLength: number
newStart: number
newLength: number
oldHunk: { startLine: number; endLine: number }
newHunk: { startLine: number; endLine: number }
}
export interface ParsedPatch {
oldHunk: string
newHunk: string
oldStart: number
newStart: number
}
/**
* Validate remedy code AST
* Ensures the remedy doesn't create invalid TypeScript/JS
*/
export const validateRemedyAST = (
filename: string,
remedy: string,
project: Project
): boolean => {
if (!filename.endsWith('.ts') && !filename.endsWith('.js')) return true
try {
const sf = project.createSourceFile(`test_${Date.now()}.ts`, remedy, {
overwrite: true
})
// Check for basic validity - no unexpected class declarations in remedy
return !sf.getDescendantsOfKind(SyntaxKind.ClassDeclaration).length
} catch (e) {
return false
}
}
/**
* Split a patch into individual hunks
*/
export const splitPatch = (patch: string | null | undefined): string[] => {
if (patch == null) return []
const pattern = /(^@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*$/gm
const result: string[] = []
let match = pattern.exec(patch)
if (match != null) {
const first = match.index + match[0].length
let last = first
while ((match = pattern.exec(patch)) !== null) {
result.push(patch.substring(last, match.index).trim())
last = match.index + match[0].length
}
result.push(patch.substring(last).trim())
}
return result
}
/**
* Extract start/end line numbers from patch header
* Exported for use by review.ts
*/
export const patchStartEndLine = (patch: string): HunkInfo | null => {
const pattern = /(^@@ -(\d+),(\d+) \+(\d+),(\d+) @@).*$/gm
const match = pattern.exec(patch)
if (match != null) {
const oldStart = parseInt(match[2])
const oldLength = parseInt(match[3])
const newStart = parseInt(match[4])
const newLength = parseInt(match[5])
return {
oldStart,
oldLength,
newStart,
newLength,
oldHunk: { startLine: oldStart, endLine: oldStart + oldLength - 1 },
newHunk: { startLine: newStart, endLine: newStart + newLength - 1 }
}
}
return null
}
/**
* Parse a patch into old and new hunks
*/
export const parsePatch = (patch: string): ParsedPatch | null => {
const hunkInfo = patchStartEndLine(patch)
if (hunkInfo == null) return null
const oldHunkLines: string[] = []
const newHunkLines: string[] = []
const lines = patch.split('\n')
for (const line of lines) {
if (line.startsWith('-')) {
oldHunkLines.push(line.substring(1))
} else if (line.startsWith('+')) {
newHunkLines.push(line.substring(1))
} else if (!line.startsWith('@@')) {
oldHunkLines.push(line)
newHunkLines.push(line)
}
}
return {
oldHunk: oldHunkLines.join('\n'),
newHunk: newHunkLines.join('\n'),
oldStart: hunkInfo.oldStart,
newStart: hunkInfo.newStart
}
}
/**
* Check if a patch contains only documentation changes (comments)
*/
export const checkIfDocumentationOnly = (diff: string): boolean => {
const content = diff
.split('\n')
.filter(l => l.startsWith('+') || l.startsWith('-'))
.map(l => l.substring(1).trim())
.join('')
return content.length > 0 && !/[a-zA-Z0-9]/.test(content.replace(/\/\/|\/\*|\*|#|"""|'''/g, ''))
}
/**
* Check if a file should be included based on path filters
*/
export const shouldIncludeFile = (
filename: string,
pathFilters: { check: (path: string) => boolean } | null
): boolean => {
if (pathFilters == null) return true
return pathFilters.check(filename)
}
/**
* Check if content contains the ignore keyword
*/
export const shouldIgnoreContent = (content: string, ignoreKeyword: string): boolean => {
return content.includes(ignoreKeyword)
}
/**
* Parse a review response from AI into structured format
*/
export interface ReviewFinding {
startLine: number
endLine: number
comment: string
severity?: 'critical' | 'major' | 'minor' | 'info'
confidence?: number
remedy?: string
verified?: boolean
verificationFeedback?: string
}
export const parseReview = (
response: string,
patches: any[],
filename: string,
debug = false
): ReviewFinding[] => {
const reviews: ReviewFinding[] = []
// Match patterns like:
// [LINE_START-LINE_END] | [SEVERITY] | [TYPE] message
// or
// [LINE] | [SEVERITY] message
const pattern =
/\[(\d+)(-(\d+))?\]\s*\|\s*(critical|major|minor|info)?\s*\|\s*(\w+)?\s*[:|-]?\s*(.+?)(?=\[(\d+)|$)/gis
let match
while ((match = pattern.exec(response)) !== null) {
const startLine = parseInt(match[1])
const endLine = match[2] ? parseInt(match[2]) : startLine
const severity = (match[4] || 'minor') as NonNullable<ReviewFinding['severity']>
const type = match[5] || 'general'
const message = match[6].trim()
// Validate line numbers against patch
const validLine = patches.some(
(p: any) => startLine >= p.newStart && startLine <= p.newStart + p.newLength
)
if (validLine) {
reviews.push({
startLine,
endLine,
comment: `[${severity.toUpperCase()}] | ${type.toUpperCase()}\n\n${message}`,
severity,
confidence: 80
})
}
}
// Also look for remedy suggestions in code blocks
const remedyPattern = /```(?:\w+)?\s*\n([\s\S]*?)```/g
let remedyMatch
while ((remedyMatch = remedyPattern.exec(response)) !== null) {
const remedy = remedyMatch[1].trim()
// Associate remedy with last review if any
if (reviews.length > 0) {
reviews[reviews.length - 1].remedy = remedy
}
}
return reviews
}
/**
* Extract pattern type from a review message
*/
export const extractPatternType = (message: string): string => {
const patternTypes = [
'Security',
'Bug',
'Performance',
'Architecture',
'Style',
'Documentation',
'Testing',
'Refactoring'
]
for (const type of patternTypes) {
if (message.toLowerCase().includes(type.toLowerCase())) {
return type.toLowerCase()
}
}
return 'general'
}
|