From d1c8c6375fa4ebd4c2d98ecec4d3bfd74c510704 Mon Sep 17 00:00:00 2001 From: mununki Date: Tue, 24 Dec 2024 17:48:02 +0900 Subject: [PATCH 1/5] Add error using @react.componentWithProps with external --- compiler/syntax/src/jsx_v4.ml | 13 ++++++++++--- ...react_component_with_props_external.res.expected | 9 +++++++++ .../react_component_with_props_external.res | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/react_component_with_props_external.res diff --git a/compiler/syntax/src/jsx_v4.ml b/compiler/syntax/src/jsx_v4.ml index 69ddc5c8e7..fb03af3e74 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -1286,9 +1286,16 @@ let transform_structure_item ~config item = pstr_desc = Pstr_primitive ({pval_attributes; pval_type} as value_description); } as pstr -> ( - match List.filter Jsx_common.has_attr pval_attributes with - | [] -> [item] - | [_] -> + match + ( List.filter Jsx_common.has_attr pval_attributes, + List.filter Jsx_common.has_attr_with_props pval_attributes ) + with + | [], [] -> [item] + | [], [_] -> + Jsx_common.raise_error ~loc:pstr_loc + "Components cannot be defined as externals when using \ + @react.componentWithProps. Please use @react.component instead." + | [_], [] -> check_multiple_components ~config ~loc:pstr_loc; check_string_int_attribute_iter.structure_item check_string_int_attribute_iter item; diff --git a/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected b/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected new file mode 100644 index 0000000000..4c18621544 --- /dev/null +++ b/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected @@ -0,0 +1,9 @@ + + We've found a bug for you! + /.../fixtures/react_component_with_props_external.res:1:1-2:40 + + 1 │ @react.componentWithProps + 2 │ external make: React.element = "default" + 3 │ + + Components cannot be defined as externals when using @react.componentWithProps. Please use @react.component instead. \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/react_component_with_props_external.res b/tests/build_tests/super_errors/fixtures/react_component_with_props_external.res new file mode 100644 index 0000000000..12132be123 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/react_component_with_props_external.res @@ -0,0 +1,2 @@ +@react.componentWithProps +external make: React.element = "default" From 71edff285c8174bafbc71735e70f68eef93f61a6 Mon Sep 17 00:00:00 2001 From: mununki Date: Tue, 24 Dec 2024 17:54:43 +0900 Subject: [PATCH 2/5] wildcard pattern --- 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 fb03af3e74..db8cee965a 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -1291,7 +1291,7 @@ let transform_structure_item ~config item = List.filter Jsx_common.has_attr_with_props pval_attributes ) with | [], [] -> [item] - | [], [_] -> + | _, [_] -> Jsx_common.raise_error ~loc:pstr_loc "Components cannot be defined as externals when using \ @react.componentWithProps. Please use @react.component instead." From 0f31864541d052051c2e0b54e9f11bbc41048471 Mon Sep 17 00:00:00 2001 From: mununki Date: Tue, 24 Dec 2024 17:55:31 +0900 Subject: [PATCH 3/5] change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f357a8720..7d34fc1504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ - Fix exponential notation syntax. https://github.com/rescript-lang/rescript/pull/7174 - Fix bug where a ref assignment is moved ouside a conditional. https://github.com/rescript-lang/rescript/pull/7176 - Fix nullable to opt conversion. https://github.com/rescript-lang/rescript/pull/7193 +- Raise error when defining external React components with `@react.componentWithProps`. https://github.com/rescript-lang/rescript/pull/7217 #### :house: Internal From c99aae2c1a9ed62f296da4391d3ea6082759bbf9 Mon Sep 17 00:00:00 2001 From: mununki Date: Mon, 30 Dec 2024 13:12:32 +0900 Subject: [PATCH 4/5] fix error message --- compiler/syntax/src/jsx_v4.ml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/syntax/src/jsx_v4.ml b/compiler/syntax/src/jsx_v4.ml index db8cee965a..574d1efe7a 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -1294,7 +1294,12 @@ let transform_structure_item ~config item = | _, [_] -> Jsx_common.raise_error ~loc:pstr_loc "Components cannot be defined as externals when using \ - @react.componentWithProps. Please use @react.component instead." + @react.componentWithProps.\n\n\ + If you intended to define an external for a React component using a \ + props type,\n\ + use the type React.component instead.\n\ + Alternatively, use @react.component for an external definition with \ + labeled arguments." | [_], [] -> check_multiple_components ~config ~loc:pstr_loc; check_string_int_attribute_iter.structure_item From 4e2d358949a7217ca8cd23e9c94768103bfa3aa5 Mon Sep 17 00:00:00 2001 From: mununki Date: Mon, 30 Dec 2024 17:16:31 +0900 Subject: [PATCH 5/5] update error message --- .../react_component_with_props_external.res.expected | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected b/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected index 4c18621544..baab7be948 100644 --- a/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected +++ b/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected @@ -6,4 +6,8 @@ 2 │ external make: React.element = "default" 3 │ - Components cannot be defined as externals when using @react.componentWithProps. Please use @react.component instead. \ No newline at end of file + Components cannot be defined as externals when using @react.componentWithProps. + +If you intended to define an external for a React component using a props type, +use the type React.component instead. +Alternatively, use @react.component for an external definition with labeled arguments. \ No newline at end of file