From 2a7245120034e379e81c1b19e325997d90572fa3 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 14 Nov 2022 08:48:57 +0100 Subject: [PATCH 1/2] PPX V4: allow uncurried `make` function and treat it as usual --- CHANGELOG.md | 3 +-- lib/4.06.1/unstable/js_compiler.ml | 12 +++++++++++ lib/4.06.1/unstable/js_playground_compiler.ml | 21 ++++++++++++++++++- lib/4.06.1/whole_compiler.ml | 21 ++++++++++++++++++- res_syntax/cli/reactjs_jsx_v4.ml | 12 +++++++++++ .../tests/ppx/react/expected/v4.res.txt | 21 +++++++++++++++++++ res_syntax/tests/ppx/react/v4.res | 10 +++++++++ 7 files changed, 96 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b41344d319..c2c2aac95e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,6 @@ #### :rocket: New Feature - Introduce experimental uncurried by default mode. Can be turned on mid-file by adding standalone annotation `@@uncurried`. For experimentation only. https://github.com/rescript-lang/rescript-compiler/pull/5796 - - Adding `@@toUncurried` to the file and reformat will convert to uncurried syntax https://github.com/rescript-lang/rescript-compiler/pull/5800 #### :boom: Breaking Change @@ -34,7 +33,7 @@ These are only breaking changes for unformatted code. - Syntax: process uncurried types explicitly in the parser/printer https://github.com/rescript-lang/rescript-compiler/pull/5784 - Syntax: process uncurried function declarations explicitly in the parser/printer https://github.com/rescript-lang/rescript-compiler/pull/5794 - +- PPX V4: allow uncurried `make` function and treat it as usual # 10.1.0-rc.5 diff --git a/lib/4.06.1/unstable/js_compiler.ml b/lib/4.06.1/unstable/js_compiler.ml index 2098cb1ccb..4678458435 100644 --- a/lib/4.06.1/unstable/js_compiler.ml +++ b/lib/4.06.1/unstable/js_compiler.ml @@ -154485,6 +154485,13 @@ let transformStructureItem ~config mapper item = React_jsx_common.raiseErrorMultipleReactComponent ~loc:pstr_loc else ( config.hasReactComponent <- true; + let binding = + match binding.pvb_expr.pexp_desc with + | Pexp_record + ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, e)], None) -> + {binding with pvb_expr = e} + | _ -> binding + in let coreTypeOfAttr = React_jsx_common.coreTypeOfAttrs binding.pvb_attributes in @@ -154897,6 +154904,11 @@ let transformSignatureItem ~config _mapper item = if config.React_jsx_common.hasReactComponent then React_jsx_common.raiseErrorMultipleReactComponent ~loc:psig_loc else config.hasReactComponent <- true; + let pval_type = + match pval_type.ptyp_desc with + | Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, [t]) -> t + | _ -> pval_type + in check_string_int_attribute_iter.signature_item check_string_int_attribute_iter item; let hasForwardRef = ref false in diff --git a/lib/4.06.1/unstable/js_playground_compiler.ml b/lib/4.06.1/unstable/js_playground_compiler.ml index 152d7727cd..28234d48e3 100644 --- a/lib/4.06.1/unstable/js_playground_compiler.ml +++ b/lib/4.06.1/unstable/js_playground_compiler.ml @@ -154485,6 +154485,13 @@ let transformStructureItem ~config mapper item = React_jsx_common.raiseErrorMultipleReactComponent ~loc:pstr_loc else ( config.hasReactComponent <- true; + let binding = + match binding.pvb_expr.pexp_desc with + | Pexp_record + ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, e)], None) -> + {binding with pvb_expr = e} + | _ -> binding + in let coreTypeOfAttr = React_jsx_common.coreTypeOfAttrs binding.pvb_attributes in @@ -154897,6 +154904,11 @@ let transformSignatureItem ~config _mapper item = if config.React_jsx_common.hasReactComponent then React_jsx_common.raiseErrorMultipleReactComponent ~loc:psig_loc else config.hasReactComponent <- true; + let pval_type = + match pval_type.ptyp_desc with + | Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, [t]) -> t + | _ -> pval_type + in check_string_int_attribute_iter.signature_item check_string_int_attribute_iter item; let hasForwardRef = ref false in @@ -168684,7 +168696,14 @@ and parseStandaloneAttribute p = let startPos = p.startPos in Parser.expect AtAt p; let attrId = parseAttributeId ~startPos p in - if attrId.txt = "uncurried" then p.uncurried_by_default <- true; + let attrId = + match attrId.txt with + | "uncurried" -> + p.uncurried_by_default <- true; + attrId + | "toUncurried" -> {attrId with txt = "uncurried"} + | _ -> attrId + in let payload = parsePayload p in (attrId, payload) diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index 17496469c3..5bf8c7db6e 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -164769,6 +164769,13 @@ let transformStructureItem ~config mapper item = React_jsx_common.raiseErrorMultipleReactComponent ~loc:pstr_loc else ( config.hasReactComponent <- true; + let binding = + match binding.pvb_expr.pexp_desc with + | Pexp_record + ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, e)], None) -> + {binding with pvb_expr = e} + | _ -> binding + in let coreTypeOfAttr = React_jsx_common.coreTypeOfAttrs binding.pvb_attributes in @@ -165181,6 +165188,11 @@ let transformSignatureItem ~config _mapper item = if config.React_jsx_common.hasReactComponent then React_jsx_common.raiseErrorMultipleReactComponent ~loc:psig_loc else config.hasReactComponent <- true; + let pval_type = + match pval_type.ptyp_desc with + | Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, [t]) -> t + | _ -> pval_type + in check_string_int_attribute_iter.signature_item check_string_int_attribute_iter item; let hasForwardRef = ref false in @@ -182116,7 +182128,14 @@ and parseStandaloneAttribute p = let startPos = p.startPos in Parser.expect AtAt p; let attrId = parseAttributeId ~startPos p in - if attrId.txt = "uncurried" then p.uncurried_by_default <- true; + let attrId = + match attrId.txt with + | "uncurried" -> + p.uncurried_by_default <- true; + attrId + | "toUncurried" -> {attrId with txt = "uncurried"} + | _ -> attrId + in let payload = parsePayload p in (attrId, payload) diff --git a/res_syntax/cli/reactjs_jsx_v4.ml b/res_syntax/cli/reactjs_jsx_v4.ml index ef478eccfe..7023bd74cf 100644 --- a/res_syntax/cli/reactjs_jsx_v4.ml +++ b/res_syntax/cli/reactjs_jsx_v4.ml @@ -817,6 +817,13 @@ let transformStructureItem ~config mapper item = React_jsx_common.raiseErrorMultipleReactComponent ~loc:pstr_loc else ( config.hasReactComponent <- true; + let binding = + match binding.pvb_expr.pexp_desc with + | Pexp_record + ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, e)], None) -> + {binding with pvb_expr = e} + | _ -> binding + in let coreTypeOfAttr = React_jsx_common.coreTypeOfAttrs binding.pvb_attributes in @@ -1229,6 +1236,11 @@ let transformSignatureItem ~config _mapper item = if config.React_jsx_common.hasReactComponent then React_jsx_common.raiseErrorMultipleReactComponent ~loc:psig_loc else config.hasReactComponent <- true; + let pval_type = + match pval_type.ptyp_desc with + | Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, [t]) -> t + | _ -> pval_type + in check_string_int_attribute_iter.signature_item check_string_int_attribute_iter item; let hasForwardRef = ref false in diff --git a/res_syntax/tests/ppx/react/expected/v4.res.txt b/res_syntax/tests/ppx/react/expected/v4.res.txt index b0e609d5e1..0a56293ff2 100644 --- a/res_syntax/tests/ppx/react/expected/v4.res.txt +++ b/res_syntax/tests/ppx/react/expected/v4.res.txt @@ -21,3 +21,24 @@ module AnotherName = { \"V4$AnotherName$anotherName" } } + +module Uncurried = { + type props<'x> = { + x: 'x, + } + + @react.component let make = ({x, _}: props<'x>) => React.string(x) + let make = { + let \"V4$Uncurried" = (props: props<_>) => make(props) + + \"V4$Uncurried" + } +} + +module type TUncurried = { + type props<'x> = { + x: 'x, + } + + let make: React.componentLike, React.element> +} diff --git a/res_syntax/tests/ppx/react/v4.res b/res_syntax/tests/ppx/react/v4.res index 27c6a6c6e6..227b8f888c 100644 --- a/res_syntax/tests/ppx/react/v4.res +++ b/res_syntax/tests/ppx/react/v4.res @@ -7,3 +7,13 @@ module AnotherName = { @react.component let anotherName = (~x) => React.string(x) } + +module Uncurried = { + @react.component + let make = (. ~x) => React.string(x) +} + +module type TUncurried = { + @react.component + let make: (. ~x: string) => React.element +} From 474afcb2ab8f1afae0ebf65b8afd3aaefe71c449 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Mon, 14 Nov 2022 08:50:30 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2c2aac95e..8fe4089d96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +33,7 @@ These are only breaking changes for unformatted code. - Syntax: process uncurried types explicitly in the parser/printer https://github.com/rescript-lang/rescript-compiler/pull/5784 - Syntax: process uncurried function declarations explicitly in the parser/printer https://github.com/rescript-lang/rescript-compiler/pull/5794 -- PPX V4: allow uncurried `make` function and treat it as usual +- PPX V4: allow uncurried `make` function and treat it like a currie one https://github.com/rescript-lang/rescript-compiler/pull/5802 # 10.1.0-rc.5