Skip to content

Commit 81f4b2f

Browse files
authored
Trim trailing commas from semantic tokens output to ensure valid JSON (#7750)
* Add semantic tokens snapshot test * Check not_compiled analysis test snapshots * Trim trailing commas from semantic tokens output to ensure valid JSON * Add CHANGELOG entry
1 parent 93b103d commit 81f4b2f

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
- Fix formatting of nested records in `.resi` files. https://github.com/rescript-lang/rescript/pull/7741
2424
- Don't format and don't check formatting of dependencies. https://github.com/rescript-lang/rescript/pull/7748
25+
- Fix `rescript-editor-analysis semanticTokens` returning invalid JSON in certain cases. https://github.com/rescript-lang/rescript/pull/7750
2526

2627
#### :memo: Documentation
2728

analysis/src/Commands.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ let test ~path =
498498
let currentFile = createCurrentFile () in
499499
DumpAst.dump ~pos:(line, col) ~currentFile;
500500
Sys.remove currentFile
501+
| "sem" -> SemanticTokens.semanticTokens ~currentFile:path
501502
| _ -> ());
502503
print_newline ())
503504
in

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let make = () => {
2+
<span className={ />
3+
}
4+
//^sem
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":[0,4,4,1,0,1,2,1,3,0,0,1,4,7,0]}

tests/analysis_tests/tests/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ warningYellow='\033[0;33m'
2020
successGreen='\033[0;32m'
2121
reset='\033[0m'
2222

23-
diff=$(git ls-files --modified src/expected)
23+
diff=$(git ls-files --modified src/expected not_compiled/expected)
2424
if [[ $diff = "" ]]; then
2525
printf "${successGreen}✅ No analysis_tests snapshot changes detected.${reset}\n"
2626
else
2727
printf "${warningYellow}⚠️ The analysis_tests snapshot doesn't match. Double check that the output is correct, run 'make test-analysis' and stage the diff.\n${diff}\n${reset}"
28-
git --no-pager diff src/expected
28+
git --no-pager diff src/expected not_compiled/expected
2929
exit 1
3030
fi

0 commit comments

Comments
 (0)