diff --git a/CHANGELOG.md b/CHANGELOG.md index 17181fdfe0..c6b241a9d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ These are only breaking changes for unformatted code. - Fix compiler ppx issue when combining `async` and uncurried application https://github.com/rescript-lang/rescript-compiler/pull/5856 - 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 - 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 +- Fix some comments disappearing in array access expressions https://github.com/rescript-lang/rescript-compiler/pull/5947 #### :nail_care: Polish diff --git a/res_syntax/src/res_comments_table.ml b/res_syntax/src/res_comments_table.ml index ed07ec2f97..f6ce468db5 100644 --- a/res_syntax/src/res_comments_table.ml +++ b/res_syntax/src/res_comments_table.ml @@ -1297,6 +1297,17 @@ and walkExpression expr t comments = walkExpression operand2 t inside; (* (List.concat [inside; after]); *) attach t.trailing operand2.pexp_loc after + | Pexp_apply + ( {pexp_desc = Pexp_ident {txt = Longident.Ldot (Lident "Array", "get")}}, + [(Nolabel, parentExpr); (Nolabel, memberExpr)] ) -> + walkList [Expression parentExpr; Expression memberExpr] t comments + | Pexp_apply + ( {pexp_desc = Pexp_ident {txt = Longident.Ldot (Lident "Array", "set")}}, + [(Nolabel, parentExpr); (Nolabel, memberExpr); (Nolabel, targetExpr)] ) + -> + walkList + [Expression parentExpr; Expression memberExpr; Expression targetExpr] + t comments | Pexp_apply (callExpr, arguments) -> let before, inside, after = partitionByLoc comments callExpr.pexp_loc in let after = diff --git a/res_syntax/tests/printer/comments/array.res b/res_syntax/tests/printer/comments/array.res new file mode 100644 index 0000000000..8882f172e8 --- /dev/null +++ b/res_syntax/tests/printer/comments/array.res @@ -0,0 +1,18 @@ +a[ /* zz */ 0 ] = 7 + +let _ = ( + /* zz */ a + )[0] + +let _ = ( + a // zz + )[0] + + ( + incidents + ->Belt.Array.keep(({status}) => status === #OPEN) + // This comment will vanish + ->Belt.SortArray.stableSortBy((a, b) => + compare(a.createdTime, b.createdTime) + ) + )[0] \ No newline at end of file diff --git a/res_syntax/tests/printer/comments/expected/array.res.txt b/res_syntax/tests/printer/comments/expected/array.res.txt new file mode 100644 index 0000000000..90ed9ed5c8 --- /dev/null +++ b/res_syntax/tests/printer/comments/expected/array.res.txt @@ -0,0 +1,12 @@ +a[/* zz */ 0] = 7 + +let _ = /* zz */ a[0] + +let _ = a[0] // zz + +( + incidents + ->Belt.Array.keep(({status}) => status === #OPEN) + // This comment will vanish + ->Belt.SortArray.stableSortBy((a, b) => compare(a.createdTime, b.createdTime)) +)[0]