Skip to content

Commit ad8ed8b

Browse files
committed
Trim trailing commas from semantic tokens output to ensure valid JSON
1 parent 9e64c73 commit ad8ed8b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

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)