Skip to content

parseJsonc changes string values that contain ",}" or ",]" #2

Open

Description

@Alekla0126

parseJsonc reads VS Code themes (from .vsix packages and local theme files). It removes trailing commas with a regex over the whole text, including inside strings (jsonc.ts#L23), so a string value that contains ,} or ,] gets changed:

parseJsonc('{"a": "x,}", "b": "[1,]"}')
// today:    { a: 'x}',  b: '[1]' }
// expected: { a: 'x,}', b: '[1,]' }

What to do

  • Remove trailing commas only outside strings. The function already walks the text character by character and knows when it's inside a string (lines 6–22), so that loop is a good place to drop a comma whose next non-whitespace character is } or ], instead of the regex at the end.
  • Keep what works today: comments, // inside strings and [1, 2,] (existing test), and stripping a leading BOM.
  • Add test cases for the example above and for a BOM.

Test: pnpm test packages/themes and pnpm typecheck.

Activity

  1. bconnnnn commented on Oct 16, 2026

    @bconnnnn

    I reproduced the JSONC corruption in #2 and have a tested fix prepared.

    The problem is the final out.replace(/,(\s*[}\]])/g, '$1'): it also matches ,} and ,] inside quoted strings. I moved trailing-comma removal into the existing character scan, on } / ] only when outside a string, after the comment-removal step. This also preserves commas before closing delimiters when comments intervene.

    Added regression cases for literal ,} / ,] strings, escaped quotes, // inside strings, comments around trailing commas, and a leading BOM.

    Verification on the local patch: pnpm test packages/themes (10 passing), pnpm typecheck (pass), full pnpm test (397 passing, 4 intentionally skipped). No dependency changes.

    I have not opened a PR yet because my GitHub integration currently cannot create a fork/branch in this repository (403). The fix and tests are committed locally; I'll open a normal fork PR when write access is available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions