Skip to content

Commit ada7100

Browse files
authored
Fix some comments disappearing in array sugar. (#5947)
* Fix some comments disappearing in array sugar. The ids for `Array.get` and `Array.set` used to desugar array access have location that overlaps with the arguments, stealing the comments. This takes care of some examples similar to the one in #5946 but not that specific example. * Original example. * Change comment attachment instead of parser location of array functions. * Update CHANGELOG.md Fixes #5946
1 parent 87d0a8d commit ada7100

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ These are only breaking changes for unformatted code.
4848
- Fix compiler ppx issue when combining `async` and uncurried application https://github.com/rescript-lang/rescript-compiler/pull/5856
4949
- Fix issue where the internal representation of uncurried types would leak when a non-function is applied in a curried way https://github.com/rescript-lang/rescript-compiler/pull/5892
5050
- In GenType, check annotations also in module types to decide whether to produce the `.gen.tsx` file https://github.com/rescript-lang/rescript-compiler/pull/5903
51+
- Fix some comments disappearing in array access expressions https://github.com/rescript-lang/rescript-compiler/pull/5947
5152

5253
#### :nail_care: Polish
5354

res_syntax/src/res_comments_table.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,17 @@ and walkExpression expr t comments =
12971297
walkExpression operand2 t inside;
12981298
(* (List.concat [inside; after]); *)
12991299
attach t.trailing operand2.pexp_loc after
1300+
| Pexp_apply
1301+
( {pexp_desc = Pexp_ident {txt = Longident.Ldot (Lident "Array", "get")}},
1302+
[(Nolabel, parentExpr); (Nolabel, memberExpr)] ) ->
1303+
walkList [Expression parentExpr; Expression memberExpr] t comments
1304+
| Pexp_apply
1305+
( {pexp_desc = Pexp_ident {txt = Longident.Ldot (Lident "Array", "set")}},
1306+
[(Nolabel, parentExpr); (Nolabel, memberExpr); (Nolabel, targetExpr)] )
1307+
->
1308+
walkList
1309+
[Expression parentExpr; Expression memberExpr; Expression targetExpr]
1310+
t comments
13001311
| Pexp_apply (callExpr, arguments) ->
13011312
let before, inside, after = partitionByLoc comments callExpr.pexp_loc in
13021313
let after =
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
a[ /* zz */ 0 ] = 7
2+
3+
let _ = (
4+
/* zz */ a
5+
)[0]
6+
7+
let _ = (
8+
a // zz
9+
)[0]
10+
11+
(
12+
incidents
13+
->Belt.Array.keep(({status}) => status === #OPEN)
14+
// This comment will vanish
15+
->Belt.SortArray.stableSortBy((a, b) =>
16+
compare(a.createdTime, b.createdTime)
17+
)
18+
)[0]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
a[/* zz */ 0] = 7
2+
3+
let _ = /* zz */ a[0]
4+
5+
let _ = a[0] // zz
6+
7+
(
8+
incidents
9+
->Belt.Array.keep(({status}) => status === #OPEN)
10+
// This comment will vanish
11+
->Belt.SortArray.stableSortBy((a, b) => compare(a.createdTime, b.createdTime))
12+
)[0]

0 commit comments

Comments
 (0)