Skip to content

Trim trailing commas from semantic tokens output to ensure valid JSON #1113

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
merged 2 commits into from
Aug 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#### :bug: Bug fix

- Fix: `rescript-editor-analysis.exe semanticTokens` sometimes returned invalid JSON, which affected syntax highlighting. https://github.com/rescript-lang/rescript-vscode/pull/1113

- Fix: hang in `rescript-editor-analysis.exe codeAction` that sometimes prevented ReScript files from being saved in VS Code. https://github.com/rescript-lang/rescript-vscode/pull/1112

- Fix: show existing compiler errors and warnings on file open. https://github.com/rescript-lang/rescript-vscode/pull/1103
Expand Down
9 changes: 9 additions & 0 deletions analysis/src/SemanticTokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ module Token = struct
^ string_of_int length ^ "," ^ tokenTypeToString type_ ^ ","
^ tokenModifiersString)

let remove_trailing_comma buffer =
let len = Buffer.length buffer in
if len > 0 && Buffer.nth buffer (len - 1) = ',' then
Buffer.truncate buffer (len - 1)

let emit e =
let sortedTokens =
e.tokens
Expand All @@ -87,6 +92,10 @@ module Token = struct
in
let buf = Buffer.create 1 in
sortedTokens |> List.iter (fun t -> e |> emitToken buf t);

(* Valid JSON arrays cannot have trailing commas *)
remove_trailing_comma buf;

Buffer.contents buf
end

Expand Down
Loading