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

Better error msg and loc for multiple props type declaration #655

Merged
merged 4 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions cli/JSXV4.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ type props<'x, 'y, 'z> = {x: 'x, y?: 'y, z?: 'z}
}
```

If there is any type with the same name of `props`, it needs to be renamed to avoid the error of the multiple definitions of the type name.

### Component Application

```rescript
Expand Down
6 changes: 3 additions & 3 deletions cli/reactjs_jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ let transformStructureItem ~config mapper item =
in
(* type props<'x, 'y> = { x: 'x, y?: 'y, ... } *)
let propsRecordType =
makePropsRecordType "props" Location.none namedTypeList
makePropsRecordType "props" pstr_loc namedTypeList
in
(* can't be an arrow because it will defensively uncurry *)
let newExternalType =
Expand Down Expand Up @@ -975,7 +975,7 @@ let transformStructureItem ~config mapper item =
let vbMatchList = List.map vbMatch namedArgWithDefaultValueList in
(* type props = { ... } *)
let propsRecordType =
makePropsRecordType "props" emptyLoc namedTypeList
makePropsRecordType "props" pstr_loc namedTypeList
in
let innerExpression =
Exp.apply
Expand Down Expand Up @@ -1197,7 +1197,7 @@ let transformSignatureItem ~config _mapper item =
(makePropsTypeParams namedTypeList)
in
let propsRecordType =
makePropsRecordTypeSig "props" Location.none
makePropsRecordTypeSig "props" psig_loc
((* If there is Nolabel arg, regard the type as ref in forwardRef *)
(if !hasForwardRef then [(true, "ref", [], refType Location.none)]
else [])
Expand Down
4 changes: 3 additions & 1 deletion tests/ppx/react/expected/commentAtTop.res.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
type props<'msg> = {msg: 'msg} // test React JSX file
type props<'msg> = { // test React JSX file
msg: 'msg,
}

@react.component
let make = ({msg, _}: props<'msg>) => {
Expand Down
10 changes: 8 additions & 2 deletions tests/ppx/react/expected/externalWithCustomName.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ let t = React.createElement(Foo.component, Foo.componentProps(~a=1, ~b={"1"}, ()
@@jsxConfig({version: 4, mode: "classic"})

module Foo = {
type props<'a, 'b> = {a: 'a, b: 'b}
type props<'a, 'b> = {
a: 'a,
b: 'b,
}

@module("Foo")
external component: React.componentLike<props<int, string>, React.element> = "component"
Expand All @@ -24,7 +27,10 @@ let t = React.createElement(Foo.component, {a: 1, b: "1"})
@@jsxConfig({version: 4, mode: "automatic"})

module Foo = {
type props<'a, 'b> = {a: 'a, b: 'b}
type props<'a, 'b> = {
a: 'a,
b: 'b,
}

@module("Foo")
external component: React.componentLike<props<int, string>, React.element> = "component"
Expand Down
10 changes: 8 additions & 2 deletions tests/ppx/react/expected/externalWithRef.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'x, 'ref> = {x: 'x, ref?: 'ref}
type props<'x, 'ref> = {
x: 'x,
ref?: 'ref,
}

@module("componentForwardRef")
external make: React.componentLike<props<string, ReactDOM.Ref.currentDomRef>, React.element> =
Expand All @@ -28,7 +31,10 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4C = {
type props<'x, 'ref> = {x: 'x, ref?: 'ref}
type props<'x, 'ref> = {
x: 'x,
ref?: 'ref,
}

@module("componentForwardRef")
external make: React.componentLike<props<string, ReactDOM.Ref.currentDomRef>, React.element> =
Expand Down
10 changes: 8 additions & 2 deletions tests/ppx/react/expected/externalWithTypeVariables.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'x, 'children> = {x: 'x, children: 'children}
type props<'x, 'children> = {
x: 'x,
children: 'children,
}

@module("c")
external make: React.componentLike<props<t<'a>, React.element>, React.element> = "component"
Expand All @@ -25,7 +28,10 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4C = {
type props<'x, 'children> = {x: 'x, children: 'children}
type props<'x, 'children> = {
x: 'x,
children: 'children,
}

@module("c")
external make: React.componentLike<props<t<'a>, React.element>, React.element> = "component"
Expand Down
8 changes: 6 additions & 2 deletions tests/ppx/react/expected/fileLevelConfig.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'msg> = {msg: 'msg}
type props<'msg> = {
msg: 'msg,
}

@react.component
let make = ({msg, _}: props<'msg>) => {
Expand All @@ -33,7 +35,9 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4A = {
type props<'msg> = {msg: 'msg}
type props<'msg> = {
msg: 'msg,
}

@react.component
let make = ({msg, _}: props<'msg>) => {
Expand Down
4 changes: 3 additions & 1 deletion tests/ppx/react/expected/interface.res.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module A = {
type props<'x> = {x: 'x}
type props<'x> = {
x: 'x,
}
@react.component let make = ({x, _}: props<'x>) => React.string(x)
let make = {
let \"Interface$A" = (props: props<_>) => make(props)
Expand Down
4 changes: 3 additions & 1 deletion tests/ppx/react/expected/interface.resi.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module A: {
type props<'x> = {x: 'x}
type props<'x> = {
x: 'x,
}
let make: React.componentLike<props<string>, React.element>
}

Expand Down
5 changes: 4 additions & 1 deletion tests/ppx/react/expected/interfaceWithRef.res.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type props<'x, 'ref> = {x: 'x, ref?: 'ref}
type props<'x, 'ref> = {
x: 'x,
ref?: 'ref,
}
@react.component
let make = (
{x, _}: props<string, ReactDOM.Ref.currentDomRef>,
Expand Down
5 changes: 4 additions & 1 deletion tests/ppx/react/expected/interfaceWithRef.resi.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
type props<'x, 'ref> = {x: 'x, ref?: 'ref}
type props<'x, 'ref> = {
x: 'x,
ref?: 'ref,
}
let make: React.componentLike<props<string, ReactDOM.Ref.currentDomRef>, React.element>
12 changes: 10 additions & 2 deletions tests/ppx/react/expected/newtype.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
type props<'a, 'b, 'c> = {
a: 'a,
b: 'b,
c: 'c,
}

@react.component
let make = ({a, b, c, _}: props<'\"type-a", array<option<[#Foo('\"type-a")]>>, 'a>) =>
Expand All @@ -39,7 +43,11 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4A = {
type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
type props<'a, 'b, 'c> = {
a: 'a,
b: 'b,
c: 'c,
}

@react.component
let make = ({a, b, c, _}: props<'\"type-a", array<option<[#Foo('\"type-a")]>>, 'a>) =>
Expand Down
9 changes: 7 additions & 2 deletions tests/ppx/react/expected/removedKeyProp.res.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@@jsxConfig({version: 4, mode: "classic"})

module Foo = {
type props<'x, 'y> = {x: 'x, y: 'y}
type props<'x, 'y> = {
x: 'x,
y: 'y,
}

@react.component let make = ({x, y, _}: props<'x, 'y>) => React.string(x ++ y)
let make = {
Expand All @@ -12,7 +15,9 @@ module Foo = {
}

module HasChildren = {
type props<'children> = {children: 'children}
type props<'children> = {
children: 'children,
}

@react.component
let make = ({children, _}: props<'children>) => ReactDOM.createElement(React.fragment, [children])
Expand Down
10 changes: 8 additions & 2 deletions tests/ppx/react/expected/topLevel.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'a, 'b> = {a: 'a, b: 'b}
type props<'a, 'b> = {
a: 'a,
b: 'b,
}

@react.component
let make = ({a, b, _}: props<'a, 'b>) => {
Expand All @@ -36,7 +39,10 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4A = {
type props<'a, 'b> = {a: 'a, b: 'b}
type props<'a, 'b> = {
a: 'a,
b: 'b,
}

@react.component
let make = ({a, b, _}: props<'a, 'b>) => {
Expand Down
10 changes: 8 additions & 2 deletions tests/ppx/react/expected/typeConstraint.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ module V3 = {
@@jsxConfig({version: 4, mode: "classic"})

module V4C = {
type props<'a, 'b> = {a: 'a, b: 'b}
type props<'a, 'b> = {
a: 'a,
b: 'b,
}

@react.component
let make = ({a, b, _}: props<'\"type-a", '\"type-a">) =>
Expand All @@ -32,7 +35,10 @@ module V4C = {
@@jsxConfig({version: 4, mode: "automatic"})

module V4A = {
type props<'a, 'b> = {a: 'a, b: 'b}
type props<'a, 'b> = {
a: 'a,
b: 'b,
}

@react.component let make = ({a, b, _}: props<'\"type-a", '\"type-a">) => ReactDOM.jsx("div", {})
let make = {
Expand Down
11 changes: 8 additions & 3 deletions tests/ppx/react/expected/v4.res.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
type props<'x, 'y> = {x: 'x, y: 'y} // Component with type constraint
type props<'x, 'y> = { // Component with type constraint
x: 'x,
y: 'y,
}
@react.component let make = ({x, y, _}: props<string, string>) => React.string(x ++ y)
let make = {
let \"V4" = (props: props<_>) => make(props)
\"V4"
}

module AnotherName = {
type props<'x> = {x: 'x}
type // Component with another name than "make"
props<'x> = {
x: 'x,
}

// Component with another name than "make"
@react.component let anotherName = ({x, _}: props<'x>) => React.string(x)
let anotherName = {
let \"V4$AnotherName$anotherName" = (props: props<_>) => anotherName(props)
Expand Down