Files
openclaw/src/config/json5-comments.ts
T
lee-xydt 6ce439da25 fix(config): warn before stripping JSON5 comments on config write (#107604)
* fix(config): warn before stripping JSON5 comments on config write

Add checkCommentLossWarning in json5-comments.ts to detect JSON5 comments
before config writes. Warn via deps.logger.warn for main config writes and
via options.warn for $include file writes. Both paths support skipOutputLogs
to suppress warnings during automated operations.

Fixes #105683

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(config): remove public warn callback, route through internal sink

Fixes #105683

- Remove warn field from exported ConfigWriteOptions (P1 merge-risk)
- Default checkCommentLossWarning to console.warn when no callback
- Move include warning after hash-conflict/rejection checks
- Update tests to spy on console.warn instead of custom callback

* fix(config): warn before stripping JSON5 comments

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
Co-authored-by: Peter Steinberger <peter@steipete.me>
2026-07-14 21:39:18 -07:00

35 lines
940 B
TypeScript

function hasJSON5Comments(raw: string): boolean {
let quote: '"' | "'" | undefined;
for (let index = 0; index < raw.length; index += 1) {
const char = raw[index];
if (quote) {
if (char === "\\") {
index += 1;
} else if (char === quote) {
quote = undefined;
}
continue;
}
if (char === '"' || char === "'") {
quote = char;
continue;
}
if (char === "/" && (raw[index + 1] === "/" || raw[index + 1] === "*")) {
return true;
}
}
return false;
}
export function warnIfJSON5CommentsWillBeStripped(params: {
raw: string | null | undefined;
filePath: string;
warn?: (message: string) => void;
skipOutputLogs?: boolean;
}): void {
if (params.skipOutputLogs || typeof params.raw !== "string" || !hasJSON5Comments(params.raw)) {
return;
}
(params.warn ?? console.warn)(`Config write will strip JSON5 comments from ${params.filePath}.`);
}