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:
Claude Bot
2025-12-01 01:48:51 +00:00
parent 149ace4ba6
commit bdd8d705bb
2 changed files with 5 additions and 29 deletions

View File

@@ -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);

View File

@@ -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);
});