Skip to content

Commit 5b76419

Browse files
authored
Trim trailing commas from semantic tokens output to ensure valid JSON (#1113)
* Trim trailing commas from semantic tokens output to ensure valid JSON * Add CHANGELOG entry
1 parent 9e64c73 commit 5b76419

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#### :bug: Bug fix
2121

22+
- Fix: `rescript-editor-analysis.exe semanticTokens` sometimes returned invalid JSON, which affected syntax highlighting. https://github.com/rescript-lang/rescript-vscode/pull/1113
23+
2224
- 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
2325

2426
- Fix: show existing compiler errors and warnings on file open. https://github.com/rescript-lang/rescript-vscode/pull/1103

analysis/src/SemanticTokens.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ module Token = struct
7979
^ string_of_int length ^ "," ^ tokenTypeToString type_ ^ ","
8080
^ tokenModifiersString)
8181

82+
let remove_trailing_comma buffer =
83+
let len = Buffer.length buffer in
84+
if len > 0 && Buffer.nth buffer (len - 1) = ',' then
85+
Buffer.truncate buffer (len - 1)
86+
8287
let emit e =
8388
let sortedTokens =
8489
e.tokens
@@ -87,6 +92,10 @@ module Token = struct
8792
in
8893
let buf = Buffer.create 1 in
8994
sortedTokens |> List.iter (fun t -> e |> emitToken buf t);
95+
96+
(* Valid JSON arrays cannot have trailing commas *)
97+
remove_trailing_comma buf;
98+
9099
Buffer.contents buf
91100
end
92101

0 commit comments

Comments
 (0)