Use bun.fmt for JSON escaping instead of manual implementation

Addresses CodeRabbit review comment about incomplete JSON escaping.
Uses the existing bun.fmt.formatJSONStringUTF8 utility which properly
handles all control characters and edge cases.
This commit is contained in:
Claude Bot
2025-10-12 15:44:32 +00:00
parent 54465eeb20
commit 481cabcdfb

View File

@@ -46,16 +46,8 @@ pub fn writeEvents(watcher: *Watcher, events: []Watcher.WatchEvent, changed_file
writer.print("{d}", .{timestamp}) catch return;
writer.writeAll(",\"index\":") catch return;
writer.print("{d}", .{event.index}) catch return;
writer.writeAll(",\"path\":\"") catch return;
// Escape quotes and backslashes in path
for (file_path) |c| {
if (c == '"' or c == '\\') {
writer.writeByte('\\') catch return;
}
writer.writeByte(c) catch return;
}
writer.writeAll("\"") catch return;
writer.writeAll(",\"path\":") catch return;
writer.print("{}", .{bun.fmt.formatJSONStringUTF8(file_path, .{})}) catch return;
// Write individual operation flags
writer.writeAll(",\"delete\":") catch return;
@@ -77,15 +69,7 @@ pub fn writeEvents(watcher: *Watcher, events: []Watcher.WatchEvent, changed_file
if (name_opt) |name| {
if (!first) writer.writeAll(",") catch return;
first = false;
writer.writeAll("\"") catch return;
// Escape quotes and backslashes in filename
for (name) |c| {
if (c == '"' or c == '\\') {
writer.writeByte('\\') catch return;
}
writer.writeByte(c) catch return;
}
writer.writeAll("\"") catch return;
writer.print("{}", .{bun.fmt.formatJSONStringUTF8(name, .{})}) catch return;
}
}
writer.writeAll("]}\n") catch return;