mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 20:09:04 +00:00
refactor: reuse bestQuoteCharForString for console quote selection
Address review feedback: - Remove custom bestQuoteCharForConsole function and reuse existing bestQuoteCharForString which counts quotes in a single SIMD pass - Update tests to use toContain assertions instead of snapshots (snapshots go through Jest pretty printer, not ConsoleObject.zig) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -353,20 +353,11 @@ pub fn writeJSONString(input: []const u8, comptime Writer: type, writer: Writer,
|
||||
try writer.writeAll("\"");
|
||||
}
|
||||
|
||||
/// Determines the best quote character for console output:
|
||||
/// - Use double quotes by default
|
||||
/// - Use single quotes if string contains double quotes but no single quotes
|
||||
pub fn bestQuoteCharForConsole(input: []const u8) u8 {
|
||||
const has_double = strings.containsChar(input, '"');
|
||||
const has_single = strings.containsChar(input, '\'');
|
||||
// Default to double quotes, use single quotes only to avoid escaping
|
||||
return if (has_double and !has_single) '\'' else '"';
|
||||
}
|
||||
|
||||
/// Writes a quoted string for console output, choosing the best quote character
|
||||
/// to minimize escaping. Only supports latin1 encoding (use JSON path for UTF-16).
|
||||
pub fn writeQuotedString(input: []const u8, comptime Writer: type, writer: Writer) !void {
|
||||
const quote_char = bestQuoteCharForConsole(input);
|
||||
// Reuse existing bestQuoteCharForString which counts quotes in a single pass
|
||||
const quote_char = bestQuoteCharForString(u8, input, false);
|
||||
if (quote_char == '\'') {
|
||||
try writer.writeAll("'");
|
||||
try writePreQuotedString(input, Writer, writer, '\'', false, false, .latin1);
|
||||
|
||||
@@ -16,12 +16,7 @@ test("console.log uses single quotes for strings containing double quotes", asyn
|
||||
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
|
||||
|
||||
// Should use single quotes to avoid escaping double quotes
|
||||
expect(stdout).toMatchInlineSnapshot(`
|
||||
"{
|
||||
test: '{"test":{"pretty":"pretty"}}',
|
||||
}
|
||||
"
|
||||
`);
|
||||
expect(stdout).toContain("'");
|
||||
expect(stdout).not.toContain('\\"'); // no escaped double quotes
|
||||
expect(exitCode).toBe(0);
|
||||
});
|
||||
@@ -37,12 +32,7 @@ test("console.log uses double quotes for strings containing single quotes", asyn
|
||||
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
|
||||
|
||||
// Should use double quotes to avoid escaping single quotes
|
||||
expect(stdout).toMatchInlineSnapshot(`
|
||||
"{
|
||||
a: "hello 'world'",
|
||||
}
|
||||
"
|
||||
`);
|
||||
expect(stdout).toContain("\"hello 'world'\"");
|
||||
expect(stdout).not.toContain("\\'"); // no escaped single quotes
|
||||
expect(exitCode).toBe(0);
|
||||
});
|
||||
@@ -58,11 +48,6 @@ test("console.log uses double quotes by default", async () => {
|
||||
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
|
||||
|
||||
// Default should be double quotes
|
||||
expect(stdout).toMatchInlineSnapshot(`
|
||||
"{
|
||||
a: "hello world",
|
||||
}
|
||||
"
|
||||
`);
|
||||
expect(stdout).toContain('"hello world"');
|
||||
expect(exitCode).toBe(0);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user