← back to ClawCoder
src/lib/analyzer/applier.ts
40 lines
import type { Suggestion } from './types'
/**
* Apply selected suggestions to CLAUDE.md content.
*
* - If a suggestion's `before` is "missing", the `after` block is appended
* at an appropriate location (end of file, before any trailing newlines).
* - If `before` matches text in the content, it is replaced with `after`.
* - Suggestions are applied in order; later suggestions see the result of earlier ones.
*/
export function applySuggestions(
content: string,
suggestions: Suggestion[]
): string {
let result = content
for (const suggestion of suggestions) {
if (
suggestion.before === 'missing' ||
suggestion.before.startsWith('Potential ') ||
suggestion.before.startsWith('excessive ')
) {
// Append the new block at the end of the file
const trimmed = result.trimEnd()
result = trimmed + '\n\n' + suggestion.after + '\n'
} else {
// Try to find and replace the `before` text
if (result.includes(suggestion.before)) {
result = result.replace(suggestion.before, suggestion.after)
} else {
// If before text not found verbatim, just append
const trimmed = result.trimEnd()
result = trimmed + '\n\n' + suggestion.after + '\n'
}
}
}
return result
}