diff --git a/compiler/syntax/cli/res_cli.ml b/compiler/syntax/cli/res_cli.ml index 649d76cc04..1e8554e92b 100644 --- a/compiler/syntax/cli/res_cli.ml +++ b/compiler/syntax/cli/res_cli.ml @@ -165,6 +165,7 @@ module ResClflags : sig val jsx_module : string ref val jsx_mode : string ref val typechecker : bool ref + val test_ast_conversion : bool ref val parse : unit -> unit end = struct @@ -178,6 +179,7 @@ end = struct let jsx_mode = ref "automatic" let file = ref "" let typechecker = ref false + let test_ast_conversion = ref false let usage = "\n\ @@ -215,6 +217,9 @@ end = struct Arg.Unit (fun () -> typechecker := true), "Parses the ast as it would be passed to the typechecker and not the \ printer" ); + ( "-test-ast-conversion", + Arg.Unit (fun () -> test_ast_conversion := true), + "Test the ast conversion" ); ] let parse () = Arg.parse spec (fun f -> file := f) usage @@ -225,7 +230,7 @@ module CliArgProcessor = struct [@@unboxed] let process_file ~is_interface ~width ~recover ~target ~jsx_version - ~jsx_module ~jsx_mode ~typechecker filename = + ~jsx_module ~jsx_mode ~typechecker ~test_ast_conversion filename = let len = String.length filename in let process_interface = is_interface @@ -267,8 +272,17 @@ module CliArgProcessor = struct else exit 1) else let parsetree = - Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode - parse_result.parsetree + if not test_ast_conversion then parse_result.parsetree + else + let tree0 = + Ast_mapper_to0.default_mapper.signature + Ast_mapper_to0.default_mapper parse_result.parsetree + in + Ast_mapper_from0.default_mapper.signature + Ast_mapper_from0.default_mapper tree0 + in + let parsetree = + Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode parsetree in print_engine.print_interface ~width ~filename ~comments:parse_result.comments parsetree @@ -282,9 +296,19 @@ module CliArgProcessor = struct ~comments:parse_result.comments parse_result.parsetree else exit 1) else + let parsetree = + if not test_ast_conversion then parse_result.parsetree + else + let tree0 = + Ast_mapper_to0.default_mapper.structure + Ast_mapper_to0.default_mapper parse_result.parsetree + in + Ast_mapper_from0.default_mapper.structure + Ast_mapper_from0.default_mapper tree0 + in let parsetree = Jsx_ppx.rewrite_implementation ~jsx_version ~jsx_module ~jsx_mode - parse_result.parsetree + parsetree in print_engine.print_implementation ~width ~filename ~comments:parse_result.comments parsetree @@ -298,5 +322,6 @@ let () = ~width:!ResClflags.width ~recover:!ResClflags.recover ~target:!ResClflags.print ~jsx_version:!ResClflags.jsx_version ~jsx_module:!ResClflags.jsx_module ~jsx_mode:!ResClflags.jsx_mode - ~typechecker:!ResClflags.typechecker !ResClflags.file) + ~typechecker:!ResClflags.typechecker !ResClflags.file + ~test_ast_conversion:!ResClflags.test_ast_conversion) [@@raises exit] diff --git a/scripts/test_syntax.sh b/scripts/test_syntax.sh index 080a9e64a6..258b097412 100755 --- a/scripts/test_syntax.sh +++ b/scripts/test_syntax.sh @@ -42,6 +42,12 @@ while read file; do $DUNE_BIN_DIR/res_parser $file &> $(exp $file) & maybeWait done temp/files.txt +while read file; do + $DUNE_BIN_DIR/res_parser -test-ast-conversion $file &> $(exp $file) & maybeWait +done temp/files.txt while read file; do diff --git a/tests/syntax_tests/data/conversion/reason/expected/namedArgs.res.txt b/tests/syntax_tests/data/conversion/reason/expected/namedArgs.res.txt index eb0d820201..213942c98e 100644 --- a/tests/syntax_tests/data/conversion/reason/expected/namedArgs.res.txt +++ b/tests/syntax_tests/data/conversion/reason/expected/namedArgs.res.txt @@ -2,47 +2,35 @@ let wizard = Wizard.make( ~spriteSheet=wizard, ~hp=999999999999999, ~mp=50, - //~coordinates={x: 0., y:0. z: 0.}, - ~coordinates={x: 40, y: 100., z: 0.}, + ~coordinates=//~coordinates={x: 0., y:0. z: 0.}, + {x: 40, y: 100., z: 0.}, // /* c0 */ ~gpuCoordinates= /* c1 */ gpuBuffer[10] /* c2 */, // trailing ) apply( - // above ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - // below ~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, - // here ~cccccccccccccccccccccccccccccccc, ) applyOptional( - // above ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?, - // below ~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb?, - // here ~cccccccccccccccccccccccccccccccc?, ) foo( - // c0 ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int, - // c1 ~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: int, - // c2 ~cccccccccccccccccccccccccccccc: int, ) let f = ( ~isItemActive=?, - // array((name, href)) ~headers: array<(string, string)>, ~moduleName: string, - // foo ~x, - // above - /* c0 */ ~d: /* c1 */ e, // end + ~d: /* c1 */ e, ~from as // does it work hometown, ) => { @@ -56,7 +44,6 @@ let make = ( ~theme: ColorTheme.t, ~components: Mdx.Components.t, ~sidebarState: (bool, (bool => bool) => unit), - // (Sidebar, toggleSidebar) ... for toggling sidebar in mobile view ~sidebar: React.element, ~breadcrumbs: option>=?, ~children, diff --git a/tests/syntax_tests/data/printer/comments/expected/expr.res.txt b/tests/syntax_tests/data/printer/comments/expected/expr.res.txt index 7d4a0b3811..872c7c1beb 100644 --- a/tests/syntax_tests/data/printer/comments/expected/expr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/expr.res.txt @@ -218,13 +218,9 @@ let multiply = (/* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => { // test } -let multiply = (~x=/* c0 */ m1 /* c1 */, ~y=/* c2 */ m2 /* c3 */) => () +let multiply = (~x=/* c0 */ m1, ~y=/* c2 */ m2) => () -let f = ( - /* c0 */ ~greeting /* c1 */, - /* c2 */ ~from as /* c3 */ /* c4 */ hometown /* c5 */, - /* c6 */ ~x=?, -) => /* c7 */ () +let f = (~greeting, ~from as /* c2 */ /* c3 */ /* c4 */ hometown /* c5 */, ~x=?) => /* c7 */ () let multiply = (type /* c-2 */ t /* c-1 */, /* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => () let multiply = ( diff --git a/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt b/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt index 98f1bf3ee3..f6aea0b27e 100644 --- a/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/jsx.res.txt @@ -24,8 +24,8 @@ module Cite = { /> diff --git a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt index f244d48072..0f5073a2c1 100644 --- a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt @@ -2,49 +2,37 @@ let wizard = Wizard.make( ~spriteSheet=wizard, ~hp=999999999999999, ~mp=50, - //~coordinates={x: 0., y:0. z: 0.}, - ~coordinates={x: 40, y: 100., z: 0.}, - /* c0 */ ~gpuCoordinates=/* c1 */ gpuBuffer[10] /* c2 */, // trailing + ~coordinates=//~coordinates={x: 0., y:0. z: 0.}, + {x: 40, y: 100., z: 0.}, + ~gpuCoordinates=/* c0 */ /* c1 */ gpuBuffer[10] /* c2 */, // trailing ) apply( - // above ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, - // below ~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, - // here ~cccccccccccccccccccccccccccccccc, ) applyOptional( - // above ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?, - // below ~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb?, - // here ~cccccccccccccccccccccccccccccccc?, ) foo( - // c0 ~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int, - // c1 ~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: int, - // c2 ~cccccccccccccccccccccccccccccc: int, ) let f = ( ~isItemActive=?, - // array((name, href)) ~headers: array<(string, string)>, ~moduleName: string, - // foo ~x, - // above - /* c0 */ ~d: /* c1 */ e, // end - // does it work - ~from as hometown, + ~d: /* c1 */ e, + ~from as // does it work + hometown, ) => { let a = 1 let b = 2 @@ -56,7 +44,6 @@ let make = ( ~theme: ColorTheme.t, ~components: Mdx.Components.t, ~sidebarState: (bool, (bool => bool) => unit), - // (Sidebar, toggleSidebar) ... for toggling sidebar in mobile view ~sidebar: React.element, ~breadcrumbs: option>=?, ~children, diff --git a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt index e3a39acf24..e7d6b1e00f 100644 --- a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt @@ -41,15 +41,15 @@ type add = /* before */ ( /* c2 */ int /* c3 */, ) => /* before return */ int /* after */ type add = /* before */ ( - /* c0 */ ~a: int /* c1 */, - /* c2 */ ~b: int /* c3 */, + ~a: /* c0 */ int /* c1 */, + ~b: /* c2 */ int /* c3 */, ) => /* before return */ int /* after */ type multiply = /* before */ ( - /* c0 */ ~fn: ( + ~fn: /* c0 */ ( /* cinner0 */ int /* cinner1 */, /* cinner2 */ int /* cinner3 */, ) => /* cx */ int /* c1 */, - /* c2 */ ~b: int /* c3 */, + ~b: /* c2 */ int /* c3 */, ) => int /* after */ type make = ( ~activity: ActivityFlow_Activity.t, @@ -58,15 +58,15 @@ type make = ( ~next: (string, string, array) => unit, ~hapticFeedback: unit => unit, ~participants: Belt.Set.String.t, - // here + ~openPhotoFeed: // here // above - /* c0 */ ~openPhotoFeed: /* c1 */ ( - /* c2 */ ~feedId: /* c3 */ option /* c4 */, + /* c0 */ /* c1 */ ( + ~feedId: /* c2 */ /* c3 */ option /* c4 */, /* c5 */ unit /* c6 */, ) => /* c7 */ unit /* c8 */, - // only provide this when it shows a random activity - ~getAnother: unit => unit=?, + ~getAnother: // only provide this when it shows a random activity + unit => unit=?, ) => React.element //type f = /* c0 */ ~a: /* c1 */ int /* c2 */ => /* c3 */ int /* c4 */ @@ -81,10 +81,10 @@ type jsUser = /* before */ { } /* after */ external test: ( - // comment 1 - ~int: int, - // comment 2 - ~int: int, + ~int: // comment 1 + int, + ~int: // comment 2 + int, ) => unit = "test" external another_test: ( diff --git a/tests/syntax_tests/data/printer/expr/expected/Uncurried.res.txt b/tests/syntax_tests/data/printer/expr/expected/Uncurried.res.txt index bb73c5c162..bb8a97982a 100644 --- a/tests/syntax_tests/data/printer/expr/expected/Uncurried.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/Uncurried.res.txt @@ -8,7 +8,4 @@ let () = ignore(3) let foo = /* ddd */ x => x -let f = ( - // comment - ~a, -) => a +let f = (~a) => a diff --git a/tests/syntax_tests/data/printer/expr/expected/UncurriedByDefault.res.txt b/tests/syntax_tests/data/printer/expr/expected/UncurriedByDefault.res.txt index f2127546d8..6f5f13f52a 100644 --- a/tests/syntax_tests/data/printer/expr/expected/UncurriedByDefault.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/UncurriedByDefault.res.txt @@ -45,9 +45,9 @@ let _ = setTimeout(() => { }, 100) let _ = @att x => 34 -let _ = @att async x => 34 +let _ = @res.async @att async x => 34 let _ = preserveAttr(@att x => 34) -let _ = preserveAttr(@att async x => 34) +let _ = preserveAttr(@res.async @att async x => 34) let t0 = (type a b, l: list, x: a) => list{x, ...l} let t1 = (type a b, l: list, x: a) => list{x, ...l} @@ -127,9 +127,9 @@ let _ = setTimeout(() => { }, 100) let _ = @att x => 34 -let _ = @att async x => 34 +let _ = @res.async @att async x => 34 let _ = preserveAttr(@att x => 34) -let _ = preserveAttr(@att async x => 34) +let _ = preserveAttr(@res.async @att async x => 34) let t0 = (type a b, l: list, x: a) => list{x, ...l} let t1 = (type a b, l: list, x: a) => list{x, ...l} 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..52ce835fce 100644 --- a/tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt @@ -1,16 +1,18 @@ -let sequentialAwait = async () => { - let result1 = await paused("first") - nodeJsAssert.equal(result1, "first") - - let result2 = await paused("second") - nodeJsAssert.equal(result2, "second") -} +let sequentialAwait = + @res.async + async () => { + let result1 = await paused("first") + nodeJsAssert.equal(result1, "first") + + let result2 = await paused("second") + nodeJsAssert.equal(result2, "second") + } -let f = async () => () -let f = async () => () -let f = async f => f() -let f = async (a, b) => a + b -let f = async (a, b) => a + b +let f = @res.async async () => () +let f = @res.async async () => () +let f = @res.async async f => f() +let f = @res.async async (a, b) => a + b +let f = @res.async async (a, b) => a + b let maybeSomeValue = switch await fetchData(url) { | data => Some(data) @@ -32,9 +34,7 @@ user.data = await fetch() 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 -} +let withCallback = @res.async async () => @res.async async x => await x->Js.promise.resolve + 1 let () = await (await fetch(url))->(await resolve) @@ -47,7 +47,7 @@ let _ = await f let _ = await %extension let _ = await "test" let _ = await ((a, b) => a + b) -let _ = await (async (a, b) => a + b) +let _ = await (@res.async async (a, b) => a + b) let _ = await ( switch x { | A => () @@ -102,16 +102,16 @@ let _ = await { Js.Promise.resolve(x) } -let f1 = async (~x, ~y) => x + y -let f2 = async (@foo ~x, @bar ~y) => x + y -let f3 = @foo async (~x as @bar @zz z, ~y) => x + y -let f4 = async x => x -let f5 = async x => async y => 3 -let f6 = async (~x1, ~x2) => async y => 3 -let f7 = async x => async (~y) => 3 -let f8 = async (~x1, ~x2) => async (~y) => 3 -let f9 = x => async (~y) => 3 -let f10 = x => async y => 3 +let f1 = @res.async async (~x, ~y) => x + y +let f2 = @res.async async (@foo ~x, @bar ~y) => x + y +let f3 = @res.async @foo async (~x as @bar @zz z, ~y) => x + y +let f4 = @res.async async x => x +let f5 = @res.async async x => @res.async async y => 3 +let f6 = @res.async async (~x1, ~x2) => @res.async async y => 3 +let f7 = @res.async async x => @res.async async (~y) => 3 +let f8 = @res.async async (~x1, ~x2) => @res.async async (~y) => 3 +let f9 = x => @res.async async (~y) => 3 +let f10 = x => @res.async async y => 3 let f11 = (~x) => (~y) => 3 let f12 = @a (@b x) => 3 @@ -122,7 +122,7 @@ let aw = @foo (server->start)->foo let aw = (await (3 ** 4))->foo let foo = async(~a=34) -let bar = async (~a) => a + 1 +let bar = @res.async async (~a) => a + 1 let a1 = (await 3) + (await 4) let a2 = (await 3) ** (await 4) @@ -141,12 +141,12 @@ let c3 = x => @foo y => x + y type t1 = int => string => bool type t2 = (int, string) => bool -let f = async (type a, ()) => { +let f = async (type a, @res.async ()) => { await Js.Promise.resolve() } -let attr1 = @a async x => x + 1 -let attr2 = @a async (type a, ()) => (type b c, x) => 3 -let attr3 = @a (type a, ()) => async (type b c, x) => 3 -let attr4 = @a (type a, ()) => @b async (type b c, x) => 3 -let attr5: int => promise = @a @b async (type a b c, ()) => (x: a) => x +let attr1 = @res.async @a async x => x + 1 +let attr2 = @a async (type a, @res.async ()) => (type b c, x) => 3 +let attr3 = @a (type a, ()) => async (type b c, @res.async x) => 3 +let attr4 = @a (type a, ()) => @b async (type b c, @res.async x) => 3 +let attr5: int => promise = @a @b async (type a b c, @res.async ()) => (x: a) => x diff --git a/tests/syntax_tests/data/printer/expr/expected/callback.res.txt b/tests/syntax_tests/data/printer/expr/expected/callback.res.txt index a2d661ce40..e777124608 100644 --- a/tests/syntax_tests/data/printer/expr/expected/callback.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/callback.res.txt @@ -249,8 +249,8 @@ let makeEntryJobsForJournal = (journalId: string, userId: string) => { _getEntries( ~journalId, ~authHeader=MercuryService.makeAuthToken(userId), - // This has got to add a promise to a mutable queue of promises or something. - ~onData=onEntryData, + ~onData=// This has got to add a promise to a mutable queue of promises or something. + onEntryData, ~onComplete=_ => { // When the request is done, we should wait on the queue of promises to be all finished, then we can resolve. resolve() @@ -340,8 +340,8 @@ showDialog( // callback in last position, with comment in between args showDialog( dialogMessage, - // comment - ~danger=true, + ~danger=// comment + true, ~confirmText="Yes, I am sure!", ~onConfirm=() => (), ) @@ -350,8 +350,8 @@ showDialog( showDialog( dialogMessage, ~danger=true, - /* comment below */ - ~confirmText="Yes, I am sure!", + ~confirmText=/* comment below */ + "Yes, I am sure!", ~onConfirm=() => (), ) @@ -359,8 +359,8 @@ showDialog( showDialog( ~onConfirm=() => (), dialogMessage, - // comment - ~danger=true, + ~danger=// comment + true, ~confirmText="Yes, I am sure!", ) @@ -369,8 +369,8 @@ showDialog( ~onConfirm=() => (), dialogMessage, ~danger=true, - /* comment below */ - ~confirmText="Yes, I am sure!", + ~confirmText=/* comment below */ + "Yes, I am sure!", ) React.useEffect5( diff --git a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt index 5df8ca4e5a..0bcb2c14f8 100644 --- a/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/jsx.res.txt @@ -126,9 +126,10 @@ let x = <> let x =
bar)} -let optParen = {x: 3, y: ?(() => 3)} +let optParen = {x: 3, y: () => 3} let optParen = {x: 3, y: ?-3}