From 84f9e3eef736b61a4b50032d73c546a41c0c0c48 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Wed, 28 Sep 2022 15:21:34 +0200 Subject: [PATCH] Fix parsing of expressions with underscore `_` and comments. Fixes https://github.com/rescript-lang/syntax/issues/661 --- CHANGELOG.md | 1 + src/res_core.ml | 6 ++++-- .../grammar/expressions/expected/underscoreApply.res.txt | 4 +++- tests/parsing/grammar/expressions/underscoreApply.res | 9 +++++++++ tests/printer/expr/expected/underscoreApply.res.txt | 6 ++++++ tests/printer/expr/underscoreApply.res | 9 +++++++++ 6 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c576e5d..94e96972 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/res_core.ml b/src/res_core.ml index aa2d6318..49d075c5 100644 --- a/src/res_core.ml +++ b/src/res_core.ml @@ -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 diff --git a/tests/parsing/grammar/expressions/expected/underscoreApply.res.txt b/tests/parsing/grammar/expressions/expected/underscoreApply.res.txt index 06a6fe3d..998bd53c 100644 --- a/tests/parsing/grammar/expressions/expected/underscoreApply.res.txt +++ b/tests/parsing/grammar/expressions/expected/underscoreApply.res.txt @@ -14,4 +14,6 @@ let l1 = List.length let l2 = ([Some 1; None; Some 2] |> (List.map (fun __x -> optParam ?v:__x ()))) |> - List.length \ No newline at end of file + List.length +;;fun __x -> + underscoreWithComments (fun x -> ((something ())[@ns.braces ])) __x \ No newline at end of file diff --git a/tests/parsing/grammar/expressions/underscoreApply.res b/tests/parsing/grammar/expressions/underscoreApply.res index a84844dd..e19bd9cf 100644 --- a/tests/parsing/grammar/expressions/underscoreApply.res +++ b/tests/parsing/grammar/expressions/underscoreApply.res @@ -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() + }, + _, +) diff --git a/tests/printer/expr/expected/underscoreApply.res.txt b/tests/printer/expr/expected/underscoreApply.res.txt index 3a5d8731..1be54d85 100644 --- a/tests/printer/expr/expected/underscoreApply.res.txt +++ b/tests/printer/expr/expected/underscoreApply.res.txt @@ -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() +}, _) diff --git a/tests/printer/expr/underscoreApply.res b/tests/printer/expr/underscoreApply.res index 7dda0745..bbd2cecf 100644 --- a/tests/printer/expr/underscoreApply.res +++ b/tests/printer/expr/underscoreApply.res @@ -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() + }, + _, +)