Skip to content

Commit e8c88e6

Browse files
committed
Towards handling V4 components import.
1 parent 2d48f25 commit e8c88e6

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

jscomp/gentype/EmitJs.ml

+38
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ let rec emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
162162
retType;
163163
} as function_)
164164
when retType |> EmitType.isTypeFunctionComponent ~fields ->
165+
(* JSX V3 *)
165166
let componentName =
166167
match importFile with "." | ".." -> None | _ -> Some importFile
167168
in
@@ -182,6 +183,43 @@ let rec emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
182183
}
183184
in
184185
Function { function_ with componentName }
186+
| Function
187+
({
188+
argTypes = [ { aType = Ident { name } as propsType; aName } ];
189+
retType;
190+
} as function_)
191+
when Filename.check_suffix name "props"
192+
&& retType |> EmitType.isTypeFunctionComponent ~fields:[] -> (
193+
let _ = function_ in
194+
let _ = propsType in
195+
match typeGetInlined propsType with
196+
| Object (closedFlags, fields) ->
197+
(* JSX V3 *)
198+
let componentName =
199+
match importFile with
200+
| "." | ".." -> None
201+
| _ -> Some importFile
202+
in
203+
let fields =
204+
Ext_list.filter_map fields (fun (field : field) ->
205+
match field.nameJS with
206+
| "children"
207+
when field.type_ |> EmitType.isTypeReactElement ->
208+
Some { field with type_ = EmitType.typeReactChild }
209+
| "key" ->
210+
(* Filter out key, which is added to the props type definition in V4 *)
211+
None
212+
| _ -> Some field)
213+
in
214+
let function_ =
215+
{
216+
function_ with
217+
argTypes =
218+
[ { aType = Object (closedFlags, fields); aName } ];
219+
}
220+
in
221+
Function { function_ with componentName }
222+
| _ -> type_)
185223
| _ -> type_
186224
in
187225
let converter = type_ |> typeGetConverter in

jscomp/gentype_tests/typescript-react-example/src/JSXV4.gen.tsx

+24-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,32 @@ import {make as makeNotChecked} from './hookExample';
77
import * as React from 'react';
88

99
// In case of type error, check the type of 'make' in 'JSXV4.re' and './hookExample'.
10-
export const makeTypeChecked: <a>(_1:props<JSX.Element,person,JSX.Element,renderMe<a>>) => JSX.Element = makeNotChecked;
10+
export const makeTypeChecked: React.ComponentType<{
11+
readonly actions?: JSX.Element;
12+
readonly person: {
13+
readonly name: string;
14+
readonly age: number
15+
};
16+
readonly children: React.ReactNode;
17+
readonly renderMe: React.ComponentType<{
18+
readonly randomString: string;
19+
readonly poly: any
20+
}>
21+
}> = makeNotChecked;
1122

1223
// Export 'make' early to allow circular import from the '.bs.js' file.
13-
export const make: unknown = makeTypeChecked as <a>(_1:props<JSX.Element,person,JSX.Element,renderMe<a>>) => JSX.Element;
24+
export const make: unknown = makeTypeChecked as React.ComponentType<{
25+
readonly actions?: JSX.Element;
26+
readonly person: {
27+
readonly name: string;
28+
readonly age: number
29+
};
30+
readonly children: React.ReactNode;
31+
readonly renderMe: React.ComponentType<{
32+
readonly randomString: string;
33+
readonly poly: any
34+
}>
35+
}>;
1436

1537
// tslint:disable-next-line:no-var-requires
1638
const JSXV4BS = require('./JSXV4.bs');

lib/4.06.1/whole_compiler.ml

+38
Original file line numberDiff line numberDiff line change
@@ -240372,6 +240372,7 @@ let rec emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
240372240372
retType;
240373240373
} as function_)
240374240374
when retType |> EmitType.isTypeFunctionComponent ~fields ->
240375+
(* JSX V3 *)
240375240376
let componentName =
240376240377
match importFile with "." | ".." -> None | _ -> Some importFile
240377240378
in
@@ -240392,6 +240393,43 @@ let rec emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
240392240393
}
240393240394
in
240394240395
Function { function_ with componentName }
240396+
| Function
240397+
({
240398+
argTypes = [ { aType = Ident { name } as propsType; aName } ];
240399+
retType;
240400+
} as function_)
240401+
when Filename.check_suffix name "props"
240402+
&& retType |> EmitType.isTypeFunctionComponent ~fields:[] -> (
240403+
let _ = function_ in
240404+
let _ = propsType in
240405+
match typeGetInlined propsType with
240406+
| Object (closedFlags, fields) ->
240407+
(* JSX V3 *)
240408+
let componentName =
240409+
match importFile with
240410+
| "." | ".." -> None
240411+
| _ -> Some importFile
240412+
in
240413+
let fields =
240414+
Ext_list.filter_map fields (fun (field : field) ->
240415+
match field.nameJS with
240416+
| "children"
240417+
when field.type_ |> EmitType.isTypeReactElement ->
240418+
Some { field with type_ = EmitType.typeReactChild }
240419+
| "key" ->
240420+
(* Filter out key, which is added to the props type definition in V4 *)
240421+
None
240422+
| _ -> Some field)
240423+
in
240424+
let function_ =
240425+
{
240426+
function_ with
240427+
argTypes =
240428+
[ { aType = Object (closedFlags, fields); aName } ];
240429+
}
240430+
in
240431+
Function { function_ with componentName }
240432+
| _ -> type_)
240395240433
| _ -> type_
240396240434
in
240397240435
let converter = type_ |> typeGetConverter in

0 commit comments

Comments
 (0)