Skip to content

Special JSX children error message #7044

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

Merged
merged 2 commits into from
Sep 24, 2024
Merged
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 @@ -22,6 +22,7 @@

#### :nail_care: Polish

- Improve error message when passing `children` prop to a component that doesn't accept it. https://github.com/rescript-lang/rescript-compiler/pull/7044
- Improve error messages for pattern matching on option vs non-option, and vice versa. https://github.com/rescript-lang/rescript-compiler/pull/7035
- Improve bigint literal comparison. https://github.com/rescript-lang/rescript-compiler/pull/7029
- Improve output of `@variadic` bindings. https://github.com/rescript-lang/rescript-compiler/pull/7030
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

We've found a bug for you!
/.../fixtures/component_missing_prop_children.res:6:35-42

4 │ type props<'name> = {name: 'name}
5 │
6 │ let make = (): props<'name> => {children: ""}
7 │ }
8 │

This JSX component does not accept child elements. It has no children prop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Since the React transform isn't active in the tests, mimic what the transform outputs.
module Component = {
@res.jsxComponentProps
type props<'name> = {name: 'name}

let make = (): props<'name> => {children: ""}
Copy link
Member

Choose a reason for hiding this comment

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

I don't get this. Is this really what the transform would output?

Why is the make function returning props? And not React.element?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No it's not what the transform would output, but it doesn't matter, what matters is that a record of the given type is being created, which is what the transform would do. If we want to make this clearer we could set up a test suite with the transform enabled. But I don't know whether that's cumbersome to do. Do you know?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cknitt the problem now is that React itself isn't included. There's a react_ppx folder that does similar tests with the transform and React copied into the project itself. We could mimic that, but that's a larger change.

Copy link
Member

Choose a reason for hiding this comment

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

@zth I don't think you need React, you can use the Jsx module that comes with the compiler.

E.g. something like

@jsx.component
let make = (~name) => Jsx.string(name)

should work fine.

Copy link
Member

@cknitt cknitt Sep 23, 2024

Choose a reason for hiding this comment

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

Ok, well, you don't need it in my example, but you are right, you need the externals for the JSX runtime if you want to use JSX.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cknitt let's just worry about the actual error message in this PR, and then we can figure out how to set up the React transform for this separately.

}
9 changes: 7 additions & 2 deletions jscomp/ml/error_message_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,13 @@ let print_component_name ppf (p : Path.t) =
let print_component_wrong_prop_error ppf (p : Path.t)
(_fields : Types.label_declaration list) name =
fprintf ppf "@[<v>";
fprintf ppf
"@[<2>The prop @{<error>%s@} does not belong to the JSX component " name;
(match name with
| "children" ->
fprintf ppf
"@[<2>This JSX component does not accept child elements. It has no @{<error>children@} prop "
| _ ->
fprintf ppf
"@[<2>The prop @{<error>%s@} does not belong to the JSX component " name);
print_component_name ppf p;
fprintf ppf "@]@,@,"

Expand Down