diff --git a/CHANGELOG.md b/CHANGELOG.md index 9094890bea..3cb7ec6a7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - Remove `Stdlib_Char` module for now. https://github.com/rescript-lang/rescript/pull/7367 - Convert internal JavaScript codebase into ESM, ReScript package itself is now ESM (`"type": "module"`). https://github.com/rescript-lang/rescript/pull/6899 - Add built-in support for the JavaScript `in` operator. https://github.com/rescript-lang/rescript/pull/7342 +- AST cleanup: add `Pexp_await` ast node instead of `res.await` attribute. (The attribute is still used for await on modules currently). https://github.com/rescript-lang/rescript/pull/7368 #### :nail_care: Polish diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c94e799ddc..0e7b135867 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -123,19 +123,19 @@ After adding a new file to the repository that should go into the npm package - ```sh make lib # Build compiler and standard library -./cli/bsc myTestFile.res +./cli/bsc.js myTestFile.res ``` To view the untyped tree of the file run: ```sh -./cli/bsc -dparsetree myTestFile.res +./cli/bsc.js -dparsetree myTestFile.res ``` To view the typed tree of the file run: ```sh -./cli/bsc -dtypedtree myTestFile.res +./cli/bsc.js -dtypedtree myTestFile.res ``` ### Project diff --git a/analysis/src/CompletionFrontEnd.ml b/analysis/src/CompletionFrontEnd.ml index aea7a6141d..79044efad8 100644 --- a/analysis/src/CompletionFrontEnd.ml +++ b/analysis/src/CompletionFrontEnd.ml @@ -312,11 +312,12 @@ let rec exprToContextPathInner ~(inJsxContext : bool) (e : Parsetree.expression) if List.length exprs = List.length exprsAsContextPaths then Some (CTuple exprsAsContextPaths) else None + | Pexp_await e -> exprToContextPathInner ~inJsxContext e | _ -> None and exprToContextPath ~(inJsxContext : bool) (e : Parsetree.expression) = match - ( Res_parsetree_viewer.has_await_attribute e.pexp_attributes, + ( Res_parsetree_viewer.expr_is_await e, exprToContextPathInner ~inJsxContext e ) with | true, Some ctxPath -> Some (CPAwait ctxPath) diff --git a/analysis/src/Utils.ml b/analysis/src/Utils.ml index c274b5a9fb..0a015070a7 100644 --- a/analysis/src/Utils.ml +++ b/analysis/src/Utils.ml @@ -111,6 +111,7 @@ let identifyPexp pexp = | Pexp_pack _ -> "Pexp_pack" | Pexp_extension _ -> "Pexp_extension" | Pexp_open _ -> "Pexp_open" + | Pexp_await _ -> "Pexp_await" let identifyPpat pat = match pat with diff --git a/compiler/frontend/bs_ast_mapper.ml b/compiler/frontend/bs_ast_mapper.ml index 4bcda7c534..c11ee3f207 100644 --- a/compiler/frontend/bs_ast_mapper.ml +++ b/compiler/frontend/bs_ast_mapper.ml @@ -366,6 +366,7 @@ module E = struct | Pexp_open (ovf, lid, e) -> open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e) | Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x) + | Pexp_await e -> await ~loc ~attrs (sub.expr sub e) end module P = struct diff --git a/compiler/frontend/bs_builtin_ppx.ml b/compiler/frontend/bs_builtin_ppx.ml index a279a5acaf..625d47718c 100644 --- a/compiler/frontend/bs_builtin_ppx.ml +++ b/compiler/frontend/bs_builtin_ppx.ml @@ -242,10 +242,12 @@ let expr_mapper ~async_context ~in_function_def (self : mapper) || Ast_attributes.has_await_payload attrs2 -> check_await (); result - | _ when Ast_attributes.has_await_payload e.pexp_attributes -> - check_await (); - Ast_await.create_await_expression result - | _ -> result + | _ -> ( + match result.pexp_desc with + | Pexp_await e -> + check_await (); + Ast_await.create_await_expression e + | _ -> result) let typ_mapper (self : mapper) (typ : Parsetree.core_type) = Ast_core_type_class_type.typ_mapper self typ diff --git a/compiler/ml/ast_helper.ml b/compiler/ml/ast_helper.ml index aa0c66dbfc..3863a35c3d 100644 --- a/compiler/ml/ast_helper.ml +++ b/compiler/ml/ast_helper.ml @@ -180,7 +180,7 @@ module Exp = struct let pack ?loc ?attrs a = mk ?loc ?attrs (Pexp_pack a) let open_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_open (a, b, c)) let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a) - + let await ?loc ?attrs a = mk ?loc ?attrs (Pexp_await a) let case lhs ?guard rhs = {pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs} end diff --git a/compiler/ml/ast_helper.mli b/compiler/ml/ast_helper.mli index a78e33589e..1cc3f9679d 100644 --- a/compiler/ml/ast_helper.mli +++ b/compiler/ml/ast_helper.mli @@ -210,6 +210,7 @@ module Exp : sig val extension : ?loc:loc -> ?attrs:attrs -> extension -> expression val case : pattern -> ?guard:expression -> expression -> case + val await : ?loc:loc -> ?attrs:attrs -> expression -> expression end (** Value declarations *) diff --git a/compiler/ml/ast_iterator.ml b/compiler/ml/ast_iterator.ml index 1c0bd087da..b6f4e101b9 100644 --- a/compiler/ml/ast_iterator.ml +++ b/compiler/ml/ast_iterator.ml @@ -344,6 +344,7 @@ module E = struct iter_loc sub lid; sub.expr sub e | Pexp_extension x -> sub.extension sub x + | Pexp_await e -> sub.expr sub e end module P = struct diff --git a/compiler/ml/ast_mapper.ml b/compiler/ml/ast_mapper.ml index f2055efb93..a4a70c18ee 100644 --- a/compiler/ml/ast_mapper.ml +++ b/compiler/ml/ast_mapper.ml @@ -329,6 +329,7 @@ module E = struct | Pexp_open (ovf, lid, e) -> open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e) | Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x) + | Pexp_await e -> await ~loc ~attrs (sub.expr sub e) end module P = struct diff --git a/compiler/ml/ast_mapper_from0.ml b/compiler/ml/ast_mapper_from0.ml index 1e3e3687b7..0141197bea 100644 --- a/compiler/ml/ast_mapper_from0.ml +++ b/compiler/ml/ast_mapper_from0.ml @@ -295,11 +295,30 @@ end module E = struct (* Value expressions for the core language *) - let map sub {pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} = + let has_await_attribute attrs = + List.exists + (function + | {Location.txt = "res.await"}, _ -> true + | _ -> false) + attrs + + let remove_await_attribute attrs = + List.filter + (function + | {Location.txt = "res.await"}, _ -> false + | _ -> true) + attrs + + let map sub ({pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} as e) + = let open Exp in let loc = sub.location sub loc in let attrs = sub.attributes sub attrs in match desc with + | _ when has_await_attribute attrs -> + let attrs = remove_await_attribute e.pexp_attributes in + let e = sub.expr sub {e with pexp_attributes = attrs} in + await ~loc e | Pexp_ident x -> ident ~loc ~attrs (map_loc sub x) | Pexp_constant x -> constant ~loc ~attrs (map_constant x) | Pexp_let (r, vbs, e) -> diff --git a/compiler/ml/ast_mapper_to0.ml b/compiler/ml/ast_mapper_to0.ml index cc343762fc..4b09f30b26 100644 --- a/compiler/ml/ast_mapper_to0.ml +++ b/compiler/ml/ast_mapper_to0.ml @@ -407,6 +407,13 @@ module E = struct | Pexp_open (ovf, lid, e) -> open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e) | Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x) + | Pexp_await e -> + let e = sub.expr sub e in + { + e with + pexp_attributes = + (Location.mknoloc "res.await", Pt.PStr []) :: e.pexp_attributes; + } end module P = struct diff --git a/compiler/ml/ast_uncurried.ml b/compiler/ml/ast_uncurried.ml index 16f0abbfee..3cd794e404 100644 --- a/compiler/ml/ast_uncurried.ml +++ b/compiler/ml/ast_uncurried.ml @@ -19,8 +19,3 @@ let expr_is_uncurried_fun (expr : Parsetree.expression) = match expr.pexp_desc with | Pexp_fun {arity = Some _} -> true | _ -> false - -let expr_extract_uncurried_fun (expr : Parsetree.expression) = - match expr.pexp_desc with - | Pexp_fun {arity = Some _} -> expr - | _ -> assert false diff --git a/compiler/ml/depend.ml b/compiler/ml/depend.ml index f33454d448..0854e2c5d3 100644 --- a/compiler/ml/depend.ml +++ b/compiler/ml/depend.ml @@ -289,6 +289,7 @@ let rec add_expr bv exp = | Pstr_eval ({pexp_desc = Pexp_construct (c, None)}, _) -> add bv c | _ -> handle_extension e) | Pexp_extension e -> handle_extension e + | Pexp_await e -> add_expr bv e and add_cases bv cases = List.iter (add_case bv) cases diff --git a/compiler/ml/parsetree.ml b/compiler/ml/parsetree.ml index b7db5e902b..c8a8c7d60a 100644 --- a/compiler/ml/parsetree.ml +++ b/compiler/ml/parsetree.ml @@ -313,8 +313,9 @@ and expression_desc = let open M in E let! open M in E *) | Pexp_extension of extension -(* [%id] *) -(* . *) + (* [%id] *) + (* . *) + | Pexp_await of expression and case = { (* (P -> E) or (P when E0 -> E) *) diff --git a/compiler/ml/pprintast.ml b/compiler/ml/pprintast.ml index 31cb171d81..73f31671d6 100644 --- a/compiler/ml/pprintast.ml +++ b/compiler/ml/pprintast.ml @@ -722,6 +722,7 @@ and expression ctxt f x = (expression ctxt) e | Pexp_variant (l, Some eo) -> pp f "@[<2>`%s@;%a@]" l (simple_expr ctxt) eo | Pexp_extension e -> extension ctxt f e + | Pexp_await e -> pp f "@[await@ %a@]" (simple_expr ctxt) e | _ -> expression1 ctxt f x and expression1 ctxt f x = diff --git a/compiler/ml/printast.ml b/compiler/ml/printast.ml index 777829f0c9..53e76a608c 100644 --- a/compiler/ml/printast.ml +++ b/compiler/ml/printast.ml @@ -345,6 +345,9 @@ and expression i ppf x = | Pexp_extension (s, arg) -> line i ppf "Pexp_extension \"%s\"\n" s.txt; payload i ppf arg + | Pexp_await e -> + line i ppf "Pexp_await\n"; + expression i ppf e and value_description i ppf x = line i ppf "value_description %a %a\n" fmt_string_loc x.pval_name fmt_location diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index 44bf252d3a..c3f2907aab 100644 --- a/compiler/ml/typecore.ml +++ b/compiler/ml/typecore.ml @@ -178,6 +178,7 @@ let iter_expression f e = expr e; module_expr me | Pexp_pack me -> module_expr me + | Pexp_await _ -> assert false (* should be handled earlier *) and case {pc_lhs = _; pc_guard; pc_rhs} = may expr pc_guard; expr pc_rhs @@ -3195,6 +3196,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp | _ -> raise (Error (loc, env, Invalid_extension_constructor_payload))) | Pexp_extension ext -> raise (Error_forward (Builtin_attributes.error_of_extension ext)) + | Pexp_await _ -> (* should be handled earlier *) assert false and type_function ?in_function ~arity ~async loc attrs env ty_expected_ l caselist = diff --git a/compiler/syntax/src/res_ast_debugger.ml b/compiler/syntax/src/res_ast_debugger.ml index a4d4f4a390..d53d91fae9 100644 --- a/compiler/syntax/src/res_ast_debugger.ml +++ b/compiler/syntax/src/res_ast_debugger.ml @@ -707,6 +707,7 @@ module SexpAst = struct ] | Pexp_extension ext -> Sexp.list [Sexp.atom "Pexp_extension"; extension ext] + | Pexp_await e -> Sexp.list [Sexp.atom "Pexp_await"; expression e] in Sexp.list [Sexp.atom "expression"; desc] diff --git a/compiler/syntax/src/res_core.ml b/compiler/syntax/src/res_core.ml index d2f7bad50c..97b76717e3 100644 --- a/compiler/syntax/src/res_core.ml +++ b/compiler/syntax/src/res_core.ml @@ -3307,15 +3307,12 @@ and parse_async_arrow_expression ?(arrow_attrs = []) p = and parse_await_expression p = let await_loc = mk_loc p.Parser.start_pos p.end_pos in - let await_attr = make_await_attr await_loc in Parser.expect Await p; let token_prec = Token.precedence MinusGreater in let expr = parse_binary_expr ~context:OrdinaryExpr p token_prec in - { - expr with - pexp_attributes = await_attr :: expr.pexp_attributes; - pexp_loc = {expr.pexp_loc with loc_start = await_loc.loc_start}; - } + Ast_helper.Exp.await + ~loc:{expr.pexp_loc with loc_start = await_loc.loc_start} + ~attrs:[] expr and parse_try_expression p = let start_pos = p.Parser.start_pos in diff --git a/compiler/syntax/src/res_parens.ml b/compiler/syntax/src/res_parens.ml index 2094a2c5fa..78f938710e 100644 --- a/compiler/syntax/src/res_parens.ml +++ b/compiler/syntax/src/res_parens.ml @@ -56,8 +56,7 @@ let call_expr expr = } -> Parenthesized | _ when Ast_uncurried.expr_is_uncurried_fun expr -> Parenthesized - | _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes -> - Parenthesized + | _ when ParsetreeViewer.expr_is_await expr -> Parenthesized | _ -> Nothing) let structure_expr expr = @@ -109,8 +108,7 @@ let unary_expr_operand expr = | Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ ); } -> Parenthesized - | _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes -> - Parenthesized + | _ when ParsetreeViewer.expr_is_await expr -> Parenthesized | _ -> Nothing) let binary_expr_operand ~is_lhs expr = @@ -133,8 +131,7 @@ let binary_expr_operand ~is_lhs expr = | expr when ParsetreeViewer.is_binary_expression expr -> Parenthesized | expr when ParsetreeViewer.is_ternary_expr expr -> Parenthesized | {pexp_desc = Pexp_lazy _ | Pexp_assert _} when is_lhs -> Parenthesized - | _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes -> - Parenthesized + | _ when ParsetreeViewer.expr_is_await expr -> Parenthesized | {Parsetree.pexp_attributes = attrs} -> if ParsetreeViewer.has_printable_attributes attrs then Parenthesized else Nothing) @@ -229,9 +226,7 @@ let lazy_or_assert_or_await_expr_rhs ?(in_await = false) expr = | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ ); } -> Parenthesized - | _ - when (not in_await) - && ParsetreeViewer.has_await_attribute expr.pexp_attributes -> + | _ when (not in_await) && ParsetreeViewer.expr_is_await expr -> Parenthesized | _ -> Nothing) @@ -277,8 +272,7 @@ let field_expr expr = | Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ ); } -> Parenthesized - | _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes -> - Parenthesized + | _ when ParsetreeViewer.expr_is_await expr -> Parenthesized | _ -> Nothing) let set_field_expr_rhs expr = @@ -339,8 +333,7 @@ let jsx_prop_expr expr = } when starts_with_minus x -> Parenthesized - | _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes -> - Parenthesized + | _ when ParsetreeViewer.expr_is_await expr -> Parenthesized | { Parsetree.pexp_desc = ( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _ @@ -377,8 +370,7 @@ let jsx_child_expr expr = } when starts_with_minus x -> Parenthesized - | _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes -> - Parenthesized + | _ when ParsetreeViewer.expr_is_await expr -> Parenthesized | { Parsetree.pexp_desc = ( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _ diff --git a/compiler/syntax/src/res_parsetree_viewer.ml b/compiler/syntax/src/res_parsetree_viewer.ml index 3544e9e3fc..67993b4190 100644 --- a/compiler/syntax/src/res_parsetree_viewer.ml +++ b/compiler/syntax/src/res_parsetree_viewer.ml @@ -72,6 +72,11 @@ let has_await_attribute attrs = | _ -> false) attrs +let expr_is_await e = + match e.pexp_desc with + | Pexp_await _ -> true + | _ -> false + let has_inline_record_definition_attribute attrs = List.exists (function @@ -111,12 +116,7 @@ let collect_list_expressions expr = (* (__x) => f(a, __x, c) -----> f(a, _, c) *) let rewrite_underscore_apply expr = - let expr_fun = - if Ast_uncurried.expr_is_uncurried_fun expr then - Ast_uncurried.expr_extract_uncurried_fun expr - else expr - in - match expr_fun.pexp_desc with + match expr.pexp_desc with | Pexp_fun { arg_label = Nolabel; diff --git a/compiler/syntax/src/res_parsetree_viewer.mli b/compiler/syntax/src/res_parsetree_viewer.mli index a027c78911..0cc0086dd1 100644 --- a/compiler/syntax/src/res_parsetree_viewer.mli +++ b/compiler/syntax/src/res_parsetree_viewer.mli @@ -14,6 +14,7 @@ val functor_type : list * Parsetree.module_type +val expr_is_await : Parsetree.expression -> bool val has_await_attribute : Parsetree.attributes -> bool val has_inline_record_definition_attribute : Parsetree.attributes -> bool val has_res_pat_variant_spread_attribute : Parsetree.attributes -> bool diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 3b88fe5f43..fd5cee7480 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -2810,12 +2810,8 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = return_expr_doc; ]) in - let uncurried = Ast_uncurried.expr_is_uncurried_fun e in - let e_fun = - if uncurried then Ast_uncurried.expr_extract_uncurried_fun e else e - in let printed_expression = - match e_fun.pexp_desc with + match e.pexp_desc with | Pexp_fun { arg_label = Nolabel; @@ -2825,7 +2821,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = } -> (* (__x) => f(a, __x, c) -----> f(a, _, c) *) print_expression_with_comments ~state - (ParsetreeViewer.rewrite_underscore_apply e_fun) + (ParsetreeViewer.rewrite_underscore_apply e) cmt_tbl | Pexp_fun _ | Pexp_newtype _ -> print_arrow e | Parsetree.Pexp_constant c -> @@ -3432,9 +3428,10 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = Doc.concat [Doc.text "\""; member_doc; Doc.text "\""] in Doc.group (Doc.concat [parent_doc; Doc.lbracket; member; Doc.rbracket]) - in - let expr_with_await = - if ParsetreeViewer.has_await_attribute e.pexp_attributes then + | Pexp_await e -> + let printed_expression = + print_expression_with_comments ~state e cmt_tbl + in let rhs = match Parens.lazy_or_assert_or_await_expr_rhs ~in_await:true @@ -3453,7 +3450,6 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = | Nothing -> printed_expression in Doc.concat [Doc.text "await "; rhs] - else printed_expression in let should_print_its_own_attributes = match e.pexp_desc with @@ -3467,11 +3463,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = | _ -> false in match e.pexp_attributes with - | [] -> expr_with_await + | [] -> printed_expression | attrs when not should_print_its_own_attributes -> Doc.group - (Doc.concat [print_attributes ~state attrs cmt_tbl; expr_with_await]) - | _ -> expr_with_await + (Doc.concat [print_attributes ~state attrs cmt_tbl; printed_expression]) + | _ -> printed_expression and print_pexp_fun ~state ~in_callback e cmt_tbl = let async, parameters, return_expr = ParsetreeViewer.fun_expr e in @@ -3760,11 +3756,8 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl = | [] -> doc | _ -> add_parens doc in - let is_await = - ParsetreeViewer.has_await_attribute expr.pexp_attributes - in let doc = - if is_await then + if ParsetreeViewer.expr_is_await expr then let parens = Res_parens.binary_operator_inside_await_needs_parens operator in diff --git a/tests/syntax_tests/data/parsing/grammar/expressions/expected/async.res.txt b/tests/syntax_tests/data/parsing/grammar/expressions/expected/async.res.txt index ed3e620e20..1680c2c3cc 100644 --- a/tests/syntax_tests/data/parsing/grammar/expressions/expected/async.res.txt +++ b/tests/syntax_tests/data/parsing/grammar/expressions/expected/async.res.txt @@ -1,5 +1,5 @@ let greetUser async [arity:1]userId = - ((let name = ((getUserName userId)[@res.await ]) in + ((let name = await (getUserName userId) in ({js|Hello |js} ++ name) ++ {js|!|js}) [@res.braces ]) ;;async fun [arity:1]() -> 123 @@ -23,10 +23,10 @@ let f = [@res.ternary ]) let foo = async ~a:34 let bar async [arity:1]~a = a + 1 -let ex1 = ((3)[@res.await ]) + ((4)[@res.await ]) -let ex2 = ((3)[@res.await ]) ** ((4)[@res.await ]) -let ex3 = ((foo -> (bar ~arg))[@res.await ]) -let ex4 = (((foo.bar).baz)[@res.await ]) +let ex1 = (await 3) + (await 4) +let ex2 = (await 3) ** (await 4) +let ex3 = await (foo -> (bar ~arg)) +let ex4 = await ((foo.bar).baz) let attr1 = ((async fun [arity:1]x -> x + 1)[@a ]) let attr2 = ((fun (type a) -> async fun [arity:1]() -> fun (type b) -> fun (type c) -> diff --git a/tests/syntax_tests/data/parsing/grammar/expressions/expected/await.res.txt b/tests/syntax_tests/data/parsing/grammar/expressions/expected/await.res.txt index e43414fdf0..e487c3f519 100644 --- a/tests/syntax_tests/data/parsing/grammar/expressions/expected/await.res.txt +++ b/tests/syntax_tests/data/parsing/grammar/expressions/expected/await.res.txt @@ -1,22 +1,19 @@ -;;((wait 2)[@res.await ]) +;;await (wait 2) let maybeSomeValue = - match ((fetchData url)[@res.await ]) with + match await (fetchData url) with | data -> Some data | exception JsError _ -> None -let x = ((1)[@res.await ]) + 2 -let x = ((wait 1)[@res.await ]) + ((wait 2)[@res.await ]) +let x = (await 1) + 2 +let x = (await (wait 1)) + (await (wait 2)) let () = - ((let response = ((fetch {js|/users.json|js})[@res.await ]) in - let users = ((response.json ())[@res.await ]) in - let comments = - ((((fetch {js|comment.json|js})[@res.await ]).json ()) - [@res.await ]).(0) in + ((let response = await (fetch {js|/users.json|js}) in + let users = await (response.json ()) in + let comments = (await ((await (fetch {js|comment.json|js})).json ())).(0) in Js.log2 users comments) [@res.braces ]) -let () = ((delay 10)[@res.braces ][@res.await ]) -let () = ((((delay 10)[@res.await ]); ((delay 20)[@res.await ])) - [@res.braces ]) -let forEach = ((Js.Import Belt.List.forEach)[@res.await ][@a ][@b ]) +let () = ((await (delay 10))[@res.braces ]) +let () = ((await (delay 10); await (delay 20))[@res.braces ]) +let forEach = await ((Js.Import Belt.List.forEach)[@a ][@b ]) module M = ((Belt.List)[@res.await ][@a ][@b ]) let f [arity:1]() = ((let module M = ((Belt.List)[@res.await ][@a ][@b ]) in M.forEach) diff --git a/tests/syntax_tests/data/printer/expr/asyncAwait.res b/tests/syntax_tests/data/printer/expr/asyncAwait.res index f3d6b14903..2c66ec8ad1 100644 --- a/tests/syntax_tests/data/printer/expr/asyncAwait.res +++ b/tests/syntax_tests/data/printer/expr/asyncAwait.res @@ -66,7 +66,7 @@ let _ = await (assert(x)) let _ = await promises[0] let _ = await promises["resolved"] let _ = await (promises["resolved"] = sideEffect()) -let _ = await (@attr expr) +let _ = @outer await @inner expr let _ = await module(Foo) let _ = await module(Foo: Bar) let _ = await Promise diff --git a/tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt b/tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt index 5acc579bd1..59f726f6ee 100644 --- a/tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt @@ -29,11 +29,11 @@ user.data = await fetch() {await weirdReactSuspenseApi} -let inBinaryExpression = await x->Js.Promise.resolve + 1 -let inBinaryExpression = await x->Js.Promise.resolve + await y->Js.Promise.resolve +let inBinaryExpression = (await x->Js.Promise.resolve) + 1 +let inBinaryExpression = (await x->Js.Promise.resolve) + (await y->Js.Promise.resolve) let withCallback = async () => { - async x => await x->Js.promise.resolve + 1 + async x => (await x->Js.promise.resolve) + 1 } let () = await (await fetch(url))->(await resolve) @@ -68,7 +68,7 @@ let _ = await ( let _ = await (condition() ? true : false) let _ = await f(x) let _ = await f(x) -let _ = (await (f(x): Js.Promise.t)) +let _ = await (f(x): Js.Promise.t) let _ = await ( while true { infiniteLoop() @@ -88,7 +88,7 @@ let _ = await (assert(x)) let _ = await promises[0] let _ = await promises["resolved"] let _ = await promises["resolved"] = sideEffect() -let _ = @attr await (expr) +let _ = @outer await (@inner expr) let _ = await module(Foo) let _ = await module(Foo: Bar) let _ = await Promise diff --git a/tests/tools_tests/src/expected/TestPpx.res.jsout b/tests/tools_tests/src/expected/TestPpx.res.jsout index e69de29bb2..87ea7737ee 100644 --- a/tests/tools_tests/src/expected/TestPpx.res.jsout +++ b/tests/tools_tests/src/expected/TestPpx.res.jsout @@ -0,0 +1,117 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE +'use strict'; + +let React = require("react"); + +console.log("ppx test"); + +console.log("ppx test"); + +let M = { + v: 10 +}; + +let OptionalFields = { + r: { + y: 1.0 + } +}; + +function one(x) { + return x; +} + +function two(x, y) { + return x + y | 0; +} + +let n = 6; + +let Arity = { + one: one, + two: two, + n: n +}; + +React.useState(() => 0); + +async function fpromise(promise, _x) { + return await promise; +} + +let Uncurried = {}; + +async function async_succ(x) { + return x + 1 | 0; +} + +async function async_foo(x, y) { + let a = async_succ(x); + let b = async_succ(y); + return await a + await b | 0; +} + +function add(x, y) { + return x + y | 0; +} + +function partial_add(extra) { + return 3 + extra | 0; +} + +function plus(x, y) { + return x + y | 0; +} + +let z = 3; + +let Pipe = { + plus: plus, + z: z +}; + +let concat = "ab"; + +async function test() { + return 12; +} + +async function f() { + return await test() + 1 | 0; +} + +let a = "A"; + +let b = "B"; + +let vv = 10; + +let neq = false; + +let neq2 = false; + +let eq = true; + +let eq2 = true; + +exports.a = a; +exports.b = b; +exports.M = M; +exports.vv = vv; +exports.OptionalFields = OptionalFields; +exports.Arity = Arity; +exports.fpromise = fpromise; +exports.Uncurried = Uncurried; +exports.async_succ = async_succ; +exports.async_foo = async_foo; +exports.add = add; +exports.partial_add = partial_add; +exports.Pipe = Pipe; +exports.concat = concat; +exports.neq = neq; +exports.neq2 = neq2; +exports.eq = eq; +exports.eq2 = eq2; +exports.test = test; +exports.f = f; +/* Not a pure module */ diff --git a/tests/tools_tests/test.sh b/tests/tools_tests/test.sh index 41f06fa961..cd1b32571a 100755 --- a/tests/tools_tests/test.sh +++ b/tests/tools_tests/test.sh @@ -9,7 +9,7 @@ done for file in ppx/*.res; do output="src/expected/$(basename $file).jsout" - ../../cli/bsc -ppx "../../_build/install/default/bin/rescript-tools ppx" $file > $output + ../../cli/bsc.js -ppx "../../_build/install/default/bin/rescript-tools ppx" $file > $output # # CI. We use LF, and the CI OCaml fork prints CRLF. Convert. if [ "$RUNNER_OS" == "Windows" ]; then perl -pi -e 's/\r\n/\n/g' -- $output