Skip to content

Commit ce53ae4

Browse files
authored
Fix semantic highlighting for array spreads, array access and dict literals (#7789)
* Don't emit tokens for Belt.Array.concatMany generated by array spread * Don't emit tokens for Primitive_dict.make generated by dict literal syntax * Don't emit tokens for Array.get generated by array access syntax * Output semantic tokens for direct usage of Belt.Array.concatMany * Don't emit tokens for Array.set generated by array mutation syntax * Add CHANGELOG entry
1 parent b1135d4 commit ce53ae4

10 files changed

+41
-6
lines changed

CHANGELOG.md

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

4545
#### :bug: Bug fix
4646

47+
- Fix semantic highlighting for array spreads, array access and dict literals. https://github.com/rescript-lang/rescript/pull/7789
4748
- Preserve `@as(...)` decorator on record fields when creating interface. https://github.com/rescript-lang/rescript/pull/7779
4849
- Fix parse error with nested record types and attributes on the field name that has the nested record type. https://github.com/rescript-lang/rescript/pull/7781
4950
- Fix ppx resolution with package inside monorepo. https://github.com/rescript-lang/rescript/pull/7776

analysis/src/SemanticTokens.ml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,32 @@ let command ~debug ~emitter ~path =
247247
match e.pexp_desc with
248248
| Pexp_ident {txt = lid; loc} ->
249249
if lid <> Lident "not" then
250-
if not loc.loc_ghost then
251-
emitter
252-
|> emitLongident ~pos:(Loc.start loc)
253-
~posEnd:(Some (Loc.end_ loc))
254-
~lid ~debug;
255-
Ast_iterator.default_iterator.expr iterator e
250+
if not loc.loc_ghost then (
251+
(* Don't emit semantic tokens for identifiers not present in source code *)
252+
let should_emit =
253+
match lid with
254+
(* Array spread (`...`) is converted to `Belt.Array.concatMany` with `@res.spread` decorator *)
255+
| Ldot (Ldot (Lident "Belt", "Array"), "concatMany") ->
256+
let has_spread_attr =
257+
e.pexp_attributes
258+
|> List.exists (fun ({Location.txt}, _) -> txt = "res.spread")
259+
in
260+
not has_spread_attr
261+
(* Dict syntax (`dict{...}`) is converted to `Primitive_dict.make` *)
262+
| Ldot (Lident "Primitive_dict", "make") -> false
263+
| Lident "Primitive_dict" -> false
264+
(* Array access (`arr[index]`) is converted to `Array.get` *)
265+
| Ldot (Lident "Array", "get") -> false
266+
(* Array mutation (`arr[index]`) is converted to `Array.set` *)
267+
| Ldot (Lident "Array", "set") -> false
268+
| _ -> true
269+
in
270+
if should_emit then
271+
emitter
272+
|> emitLongident ~pos:(Loc.start loc)
273+
~posEnd:(Some (Loc.end_ loc))
274+
~lid ~debug;
275+
Ast_iterator.default_iterator.expr iterator e)
256276
| Pexp_jsx_element (Jsx_unary_element {jsx_unary_element_tag_name = lident})
257277
->
258278
(*
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let xs = [1, 2]
2+
let ys = [...xs, 3]
3+
//^sem
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let x = xs[0]
2+
let x = xs[index]
3+
//^sem
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
xs[0] = 42
2+
//^sem
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let d = dict{"foo": bar}
2+
//^sem
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":[0,4,2,1,0,1,4,2,1,0,0,9,2,1,0]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":[0,4,1,1,0,0,4,2,1,0,1,4,1,1,0,0,4,2,1,0,0,3,5,1,0]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":[0,0,2,1,0]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":[0,4,1,1,0,0,16,3,1,0]}

0 commit comments

Comments
 (0)