fix(json/comments): validateJSONArrayFile 先驗證再寫入避免毀損原檔、findingRow 對空值防呆
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
fbb74092cb
commit
402630fa69
+4
-2
@@ -107,8 +107,10 @@ export async function validateJSONArrayFile(fullPath, label, repairer = repairJS
|
||||
try {
|
||||
const original = readJSONText(fullPath, label);
|
||||
const repaired = await repairer(fullPath, label, original);
|
||||
fs.writeFileSync(fullPath, repaired.endsWith('\n') ? repaired : `${repaired}\n`, 'utf8');
|
||||
JSON.parse(readJSONText(fullPath, label));
|
||||
const normalized = repaired.endsWith('\n') ? repaired : `${repaired}\n`;
|
||||
// 先驗證修復結果是否為合法 JSON;無效就在寫檔前丟出,避免用毀損內容覆寫原檔。
|
||||
JSON.parse(normalized);
|
||||
fs.writeFileSync(fullPath, normalized, 'utf8');
|
||||
ok(`${label} 已由 AI 修正並通過再次驗證`);
|
||||
return { exists: true, valid: true, repaired: true };
|
||||
} catch (repairErr) {
|
||||
|
||||
Reference in New Issue
Block a user