Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Fix parsing of expressions with underscore _ and comments. #662

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Fix printing of comments inside empty blocks https://github.com/rescript-lang/syntax/pull/647
- Fix location issue in error messages with JSX V4 where the multiple props types are defined https://github.com/rescript-lang/syntax/pull/655
- Fix location issue in make function in JSX V4 that breaks dead code elimination https://github.com/rescript-lang/syntax/pull/660
- Fix parsing (hence pretty printing) of expressions with underscore `_` and comments.

## ReScript 10.0

Expand Down
6 changes: 4 additions & 2 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,13 @@ let processUnderscoreApplication args =
| _ -> arg
in
let args = List.map check_arg args in
let wrap exp_apply =
let wrap (exp_apply : Parsetree.expression) =
match !exp_question with
| Some {pexp_loc = loc} ->
let pattern =
Ast_helper.Pat.mk (Ppat_var (Location.mkloc hidden_var loc)) ~loc
Ast_helper.Pat.mk
(Ppat_var (Location.mkloc hidden_var loc))
~loc:Location.none
in
Ast_helper.Exp.mk (Pexp_fun (Nolabel, None, pattern, exp_apply)) ~loc
| None -> exp_apply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ let l1 =
List.length
let l2 =
([Some 1; None; Some 2] |> (List.map (fun __x -> optParam ?v:__x ()))) |>
List.length
List.length
;;fun __x ->
underscoreWithComments (fun x -> ((something ())[@ns.braces ])) __x
9 changes: 9 additions & 0 deletions tests/parsing/grammar/expressions/underscoreApply.res
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ let l1 =

let l2 =
list{Some(1), None, Some(2)} |> List.map(optParam(~v=?_, ())) |> List.length

underscoreWithComments(
// Comment 1
x => {
// Comment 2
something()
},
_,
)
6 changes: 6 additions & 0 deletions tests/printer/expr/expected/underscoreApply.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ Array.get(_, 1) + Array.get(_, 0)
let f = Array.get(_, 0)

let photo = pricedRoom["room"]["photos"] |> filterNone |> Array.get(_, 0)

underscoreWithComments(// Comment 1
x => {
// Comment 2
something()
}, _)
9 changes: 9 additions & 0 deletions tests/printer/expr/underscoreApply.res
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ Array.get(_, 1) + Array.get(_, 0)
let f = Array.get(_, 0)

let photo = pricedRoom["room"]["photos"] |> filterNone |> Array.get(_, 0)

underscoreWithComments(
// Comment 1
x => {
// Comment 2
something()
},
_,
)