Skip to content

Fix extra bracket in template literal syntax highlighting (#17327) #21187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

robobun
Copy link
Collaborator

@robobun robobun commented Jul 19, 2025

Summary

Fixes an issue where the JavaScript syntax highlighter was incorrectly adding an extra closing bracket (}) when highlighting template literal interpolations containing JSON.stringify() calls in colored terminal output.

Problem

When running code like this with colors enabled:

const result = {success:false};
const err = new Error(`error: ${JSON.stringify(result)}`);
throw err;

The error output would show an extra } character after JSON.stringify(, causing malformed syntax highlighting in the error display.

Root Cause

The bug was in the template literal parsing code in src/fmt.zig where it would always write a closing brace after processing template literal interpolation, even when a closing brace was already found and consumed from the input.

Before (buggy):

if (i < text.len and text[i] == '}') {
    i += 1;  // consume the brace
}
try writer.writeAll("}");  // always write a brace (BUG\!)

After (fixed):

if (i < text.len and text[i] == '}') {
    try writer.writeAll("}");  // only write if found
    i += 1;  // then consume it
}

Solution

  • Only write the closing } character when it's actually found in the input
  • This ensures the syntax highlighter doesn't add extra brackets to the output
  • The fix is minimal and surgical, affecting only the specific case where the bug occurred

Test plan

  • Manual reproduction confirmed the issue existed before the fix
  • Manual verification confirmed the extra bracket no longer appears after the fix
  • Created comprehensive regression test in test/regression/issue/17327.test.ts
  • Tested edge cases with nested JSON objects and arrays
  • Verified fix works with both FORCE_COLOR=1 and NO_COLOR=1 modes
  • All tests pass locally

🤖 Generated with Claude Code

Fixes an issue where the JavaScript syntax highlighter was incorrectly
adding an extra closing bracket (}) when highlighting template literal
interpolations containing JSON.stringify() calls.

The bug was in the template literal parsing code in src/fmt.zig where
it would always write a closing brace after processing template literal
interpolation, even when a closing brace was already found and consumed
from the input.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@robobun
Copy link
Collaborator Author

robobun commented Jul 19, 2025

Updated 5:58 PM PT - Jul 18th, 2025

@autofix-ci[bot], your commit 487e4a6 is building: #20962

@@ -0,0 +1,90 @@
import { expect, test } from "bun:test";
Copy link

@markovejnovic markovejnovic Jul 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would potentially suggest making these tests shorter since, as it turns out, the issue is in the highlighting of interpolated strings specifically.

I've gotten minimum reproducibility with:

import { highlightJavaScript as highlighter } from "bun:internal-for-testing";
import { expect, test } from "bun:test";
import stripAnsiColors from "strip-ansi";

test("highlighter-with-multiline-interpolated-string", () => {
  const testStr = '`foo: \${bar';
  const highlighted = highlighter(testStr);
  expect(stripAnsiColors(highlighted)).toBe(testStr);
});

test("highlighter-with-interpolated-string", () => {
  const testStr = '`foo: \${bar}';
  const highlighted = highlighter(testStr);
  expect(stripAnsiColors(highlighted)).toBe(testStr);
});

@Jarred-Sumner Jarred-Sumner merged commit 7940861 into main Jul 21, 2025
55 of 62 checks passed
@Jarred-Sumner Jarred-Sumner deleted the claude/fix-template-literal-syntax-highlighting-17327 branch July 21, 2025 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants