From e3b65282bd7e34fb0102d63649969da975cc032f Mon Sep 17 00:00:00 2001 From: nojaf Date: Wed, 29 Jan 2025 16:08:18 +0100 Subject: [PATCH 1/4] Copy attributes from original binding expression --- compiler/syntax/src/jsx_v4.ml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/syntax/src/jsx_v4.ml b/compiler/syntax/src/jsx_v4.ml index d2a2307508..06b1680aa4 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -1003,6 +1003,8 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding = (Pat.var @@ Location.mknoloc "ref") inner_expression else inner_expression) + (* Todo: filter out any attributes? *) + ~attrs:binding.pvb_expr.pexp_attributes in let full_expression = full_expression From 9df84d96c9d509dd90f548d9757b5c204446a030 Mon Sep 17 00:00:00 2001 From: nojaf Date: Wed, 29 Jan 2025 16:46:49 +0100 Subject: [PATCH 2/4] Add sample to test file --- compiler/syntax/src/jsx_v4.ml | 5 ++--- tests/tests/src/alias_default_value_test.mjs | 16 +++++++++++++++ tests/tests/src/alias_default_value_test.res | 21 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/compiler/syntax/src/jsx_v4.ml b/compiler/syntax/src/jsx_v4.ml index 06b1680aa4..0e364275c7 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -1002,9 +1002,8 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding = Exp.fun_ ~arity:None nolabel None (Pat.var @@ Location.mknoloc "ref") inner_expression - else inner_expression) - (* Todo: filter out any attributes? *) - ~attrs:binding.pvb_expr.pexp_attributes + else inner_expression (* Todo: filter out any attributes? *)) + ~attrs:binding.pvb_expr.pexp_attributes in let full_expression = full_expression diff --git a/tests/tests/src/alias_default_value_test.mjs b/tests/tests/src/alias_default_value_test.mjs index ba7e994132..434e2b9763 100644 --- a/tests/tests/src/alias_default_value_test.mjs +++ b/tests/tests/src/alias_default_value_test.mjs @@ -67,6 +67,21 @@ let C6 = { make: Alias_default_value_test$C6 }; +function Alias_default_value_test$C7(props) { + 'use memo'; + let username = props.username; + let count = props.count; + let times = count !== 1 ? ( + count !== 2 ? String(count) + " times" : "twice" + ) : "once"; + let name = username !== undefined && username !== "" ? username : "Anonymous"; + return "Hello " + name + ", you clicked me " + times; +} + +let C7 = { + make: Alias_default_value_test$C7 +}; + export { C0, C1, @@ -74,5 +89,6 @@ export { C3, C4, C6, + C7, } /* No side effect */ diff --git a/tests/tests/src/alias_default_value_test.res b/tests/tests/src/alias_default_value_test.res index 6d8fce486b..1ebe9f00fa 100644 --- a/tests/tests/src/alias_default_value_test.res +++ b/tests/tests/src/alias_default_value_test.res @@ -43,3 +43,24 @@ module C6 = { @react.component let make = (~comp as module(Comp: Comp), ~x as (a, b)) => Comp.xx } + +module C7 = { + @react.component + let make = + @directive("'use memo'") + (~count, ~username=?) => { + let times = switch count { + | 1 => "once" + | 2 => "twice" + | n => Belt.Int.toString(n) ++ " times" + } + + let name = switch username { + | Some("") => "Anonymous" + | Some(name) => name + | None => "Anonymous" + } + + React.string(`Hello ${name}, you clicked me ` ++ times) + } +} From 3273b943c967be394aa4e0e09100a2a1e305f568 Mon Sep 17 00:00:00 2001 From: nojaf Date: Wed, 29 Jan 2025 18:59:44 +0100 Subject: [PATCH 3/4] Remove comment --- compiler/syntax/src/jsx_v4.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/syntax/src/jsx_v4.ml b/compiler/syntax/src/jsx_v4.ml index 0e364275c7..cb5fc27470 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -1002,7 +1002,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding = Exp.fun_ ~arity:None nolabel None (Pat.var @@ Location.mknoloc "ref") inner_expression - else inner_expression (* Todo: filter out any attributes? *)) + else inner_expression) ~attrs:binding.pvb_expr.pexp_attributes in let full_expression = From 6b1ff02288cd8c7f778e520c155860a86ed1f45a Mon Sep 17 00:00:00 2001 From: nojaf Date: Wed, 29 Jan 2025 19:00:01 +0100 Subject: [PATCH 4/4] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07615aa2ef..66b7fda9a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Fix leftover assert false in code for `null != undefined`. https://github.com/rescript-lang/rescript/pull/7232 - Editor: Fix issue where completions would not show up inside of object bodies. https://github.com/rescript-lang/rescript/pull/7230 - Fix issue with pattern matching empty list which interferes with boolean optimisations. https://github.com/rescript-lang/rescript/pull/7237 +- Fix Cannot combine @react.component and @directive. https://github.com/rescript-lang/rescript/pull/7260 #### :house: Internal