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 diff --git a/compiler/syntax/src/jsx_v4.ml b/compiler/syntax/src/jsx_v4.ml index 69ddc5c8e7..574d1efe7a 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -1286,9 +1286,21 @@ 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.\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 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..baab7be948 --- /dev/null +++ b/tests/build_tests/super_errors/expected/react_component_with_props_external.res.expected @@ -0,0 +1,13 @@ + + 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. + +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 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"