Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

JSX4: Fix missing attributes from props type #731

Merged
merged 4 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 @@ -57,6 +57,7 @@
- Fix dropping attributes from props in make function in JSX V4 https://github.com/rescript-lang/syntax/pull/723
- Fix an issue where error messages related to duplicate props were displayed without a loc and were unclear https://github.com/rescript-lang/syntax/pull/728
- Fix issue where error messages related to non-existent props were displayed without location information https://github.com/rescript-lang/syntax/pull/730
- 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/syntax/pull/731

#### :eyeglasses: Spec Compliance

Expand Down
13 changes: 2 additions & 11 deletions cli/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} )
Copy link
Member Author

Choose a reason for hiding this comment

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

Noted the unnecessary adding of the optional attribute for the core_type.

:: 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 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 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={(. _, _, _) => ()} />
}