Skip to content

Commit 16679bc

Browse files
committed
Trim trailing commas from semantic tokens output to ensure valid JSON
1 parent 3c7558e commit 16679bc

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"data":[0,4,4,1,0,1,2,1,3,0,0,1,4,7,0,]}
1+
{"data":[0,4,4,1,0,1,2,1,3,0,0,1,4,7,0]}

0 commit comments

Comments
 (0)