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

JSX4: Fix missing attributes from props type #5971

Merged
merged 3 commits into from
Feb 4, 2023
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 @@ -53,6 +53,7 @@ These are only breaking changes for unformatted code.
- Parser: fix location of variable when function definition `{v => ...}` is enclosed in braces https://github.com/rescript-lang/rescript-compiler/pull/5949
- Fix issue where error messages related to non-existent props were displayed without location information https://github.com/rescript-lang/rescript-compiler/pull/5960
- Fix type inference issue with uncurried functions applied to a single unit argument. The issue was introduced in https://github.com/rescript-lang/rescript-compiler/pull/5907 when adding support to `foo()` when `foo` has only optional arguments. https://github.com/rescript-lang/rescript-compiler/pull/5970
- Fix issue where uncurried functions were incorrectly converting the type of a prop given as a default value to curried https://github.com/rescript-lang/rescript-compiler/pull/5971

#### :nail_care: Polish

Expand Down
13 changes: 2 additions & 11 deletions res_syntax/src/reactjs_jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -719,19 +719,10 @@ let argToType ~newtypes ~(typeConstraints : core_type option) types
in
match (type_, name, default) with
| Some type_, name, _ when isOptional name ->
( true,
getLabel name,
attrs,
loc,
{type_ with ptyp_attributes = optionalAttrs} )
:: types
(true, getLabel name, attrs, loc, type_) :: types
| Some type_, name, _ -> (false, getLabel name, attrs, loc, type_) :: types
| None, name, _ when isOptional name ->
( true,
getLabel name,
attrs,
loc,
Typ.var ~loc ~attrs:optionalAttrs (safeTypeFromValue name) )
(true, getLabel name, attrs, loc, Typ.var ~loc (safeTypeFromValue name))
:: types
| None, name, _ when isLabelled name ->
(false, getLabel name, attrs, loc, Typ.var ~loc (safeTypeFromValue name))
Expand Down
59 changes: 59 additions & 0 deletions res_syntax/tests/ppx/react/expected/uncurriedProps.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@@jsxConfig({version: 4})
type props<'a> = {a?: 'a}

@react.component
let make = ({?a, _}: props<(. unit) => unit>) => {
let a = switch a {
| Some(a) => a
| None => (. ()) => ()
}

React.null
}
let make = {
let \"UncurriedProps" = (props: props<_>) => make(props)

\"UncurriedProps"
}

let func = (~callback: (. string, bool, bool) => unit=(. _, _, _) => (), ()) => {
let _ = callback
}

func(~callback=(. str, a, b) => {
let _ = str
let _ = a
let _ = b
}, ())

module Foo = {
type props<'callback> = {callback?: 'callback}

@react.component
let make = ({?callback, _}: props<(. string, bool, bool) => unit>) => {
let callback = switch callback {
| Some(callback) => callback
| None => (. _, _, _) => ()
}

{
React.null
}
}
let make = {
let \"UncurriedProps$Foo" = (props: props<_>) => make(props)

\"UncurriedProps$Foo"
}
}

module Bar = {
type props = {}

@react.component let make = (_: props) => React.jsx(Foo.make, {callback: (. _, _, _) => ()})
let make = {
let \"UncurriedProps$Bar" = props => make(props)

\"UncurriedProps$Bar"
}
}
26 changes: 26 additions & 0 deletions res_syntax/tests/ppx/react/uncurriedProps.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@@jsxConfig({ version: 4 })

@react.component
let make = (~a: (. unit) => unit=(. ) => ()) => React.null

let func = (~callback: (. string, bool, bool) => unit=(. _, _, _) => (), ()) => {
let _ = callback
}

func(~callback=(. str, a, b) => {
let _ = str
let _ = a
let _ = b
}, ())

module Foo = {
@react.component
let make = (~callback: (. string, bool, bool) => unit=(. _, _, _) => ()) => {
React.null
}
}

module Bar = {
@react.component
let make = () => <Foo callback={(. _, _, _) => ()} />
}