From dadcf6189d922b78a05141ef2fef0787f3c62c2f Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 21 Sep 2024 11:55:35 +0200 Subject: [PATCH 1/2] special case error message when passing children to a JSX component that does not accept children --- .../component_missing_prop_children.res.expected | 11 +++++++++++ .../fixtures/component_missing_prop_children.res | 7 +++++++ jscomp/ml/error_message_utils.ml | 9 +++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 jscomp/build_tests/super_errors/expected/component_missing_prop_children.res.expected create mode 100644 jscomp/build_tests/super_errors/fixtures/component_missing_prop_children.res diff --git a/jscomp/build_tests/super_errors/expected/component_missing_prop_children.res.expected b/jscomp/build_tests/super_errors/expected/component_missing_prop_children.res.expected new file mode 100644 index 0000000000..e2f42fd177 --- /dev/null +++ b/jscomp/build_tests/super_errors/expected/component_missing_prop_children.res.expected @@ -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 \ No newline at end of file diff --git a/jscomp/build_tests/super_errors/fixtures/component_missing_prop_children.res b/jscomp/build_tests/super_errors/fixtures/component_missing_prop_children.res new file mode 100644 index 0000000000..e248742d88 --- /dev/null +++ b/jscomp/build_tests/super_errors/fixtures/component_missing_prop_children.res @@ -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: ""} +} diff --git a/jscomp/ml/error_message_utils.ml b/jscomp/ml/error_message_utils.ml index d178f2129f..23891716e9 100644 --- a/jscomp/ml/error_message_utils.ml +++ b/jscomp/ml/error_message_utils.ml @@ -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 "@["; - fprintf ppf - "@[<2>The prop @{%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 @{children@} prop " + | _ -> + fprintf ppf + "@[<2>The prop @{%s@} does not belong to the JSX component " name); print_component_name ppf p; fprintf ppf "@]@,@," From 86dc8e792d648118649fd2f64e0677ba3cfff964 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 21 Sep 2024 11:58:16 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4272471bd2..16246e9cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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