UNPKG

ubon

Version:

Security scanner for AI-generated React/Next.js and Python apps. Catches hardcoded secrets, accessibility issues, and vulnerabilities that traditional linters miss.

71 lines (70 loc) 2.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.collectFixEdits = collectFixEdits; exports.applyFixes = applyFixes; const fs_1 = require("fs"); const path_1 = require("path"); function collectFixEdits(results) { const map = new Map(); for (const r of results) { if (!r.fixEdits || r.fixEdits.length === 0) continue; for (const e of r.fixEdits) { if (!map.has(e.file)) map.set(e.file, []); map.get(e.file).push(e); } } return Array.from(map.entries()).map(([filePath, edits]) => ({ filePath, edits })); } function applyFixes(results, directory, dryRun) { const filesWithEdits = collectFixEdits(results); let appliedEditCount = 0; const changedFiles = []; for (const { filePath, edits } of filesWithEdits) { try { const abs = (0, path_1.join)(directory, filePath); const original = (0, fs_1.readFileSync)(abs, 'utf-8'); const updated = applyEditsToContent(original, edits); if (updated !== original) { if (!dryRun) (0, fs_1.writeFileSync)(abs, updated, 'utf-8'); appliedEditCount += edits.length; changedFiles.push(filePath); } } catch { // skip file if cannot read/write } } return { changedFiles, appliedEditCount }; } function applyEditsToContent(content, edits) { // Convert line/column to absolute indices, and apply in reverse order const lineStarts = computeLineStarts(content); const normalized = edits .map(e => ({ start: positionToIndex(e.startLine, e.startColumn, lineStarts), end: positionToIndex(e.endLine, e.endColumn, lineStarts), replacement: e.replacement })) .sort((a, b) => b.start - a.start); let updated = content; for (const e of normalized) { updated = updated.slice(0, e.start) + e.replacement + updated.slice(e.end); } return updated; } function computeLineStarts(text) { const starts = [0]; for (let i = 0; i < text.length; i++) { if (text[i] === '\n') starts.push(i + 1); } return starts; } function positionToIndex(line, column, lineStarts) { const lineIndex = Math.max(1, line) - 1; const lineStart = lineStarts[Math.min(lineIndex, lineStarts.length - 1)] ?? 0; return lineStart + Math.max(0, column - 1); }