Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error using @react.componentWithProps with external #7217

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions compiler/syntax/src/jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if the user intended to define a React component as a function of a props record, we should rather tell him to use the type React.component<props> in his external definition instead.

So maybe the message should be something like

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<props> instead.
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

We've found a bug for you!
/.../fixtures/react_component_with_props_external.res:1:1-2:40

1 │ [1;[email protected]
2 │ external make: React.element = "default"
3 │

Components cannot be defined as externals when using @react.componentWithProps. Please use @react.component instead.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@react.componentWithProps
external make: React.element = "default"
Loading