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

Commit 467175f

Browse files
committed
Add support for paths in @genType.as when importing components from JS. Also, simply support renaming.
Normally the assumption is that JS components use default exports. When instead JS components use named export, they can now be imported by specifying the name in `genType.as`. In addition, paths are also supported, so one can import a component whose name is specified by a path. Examples are included for TS and Flow. Fixes #113.
1 parent f4b374c commit 467175f

File tree

12 files changed

+151
-14
lines changed

12 files changed

+151
-14
lines changed

Changes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# master
2+
- Add support for paths in `@genType.as` when importing components from JS. Also, simply support renaming.
3+
14
# 1.6.1
25
- Fix missing import React when importing component in untyped back-end.
36

examples/flow-react-example/src/TestImport.bs.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/flow-react-example/src/TestImport.gen.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ import {ValueStartingWithUpperCaseLetter as valueStartingWithUpperCaseLetterNotC
1616
// flowlint-next-line nonstrict-import:off
1717
import {default as defaultValueNotChecked} from './exportNestedValues';
1818

19+
// flowlint-next-line nonstrict-import:off
20+
import {TopLevelClass as TopLevelClass} from './interop/MyBanner.component';
21+
22+
// $FlowExpectedError: Reason checked type sufficiently
23+
import * as React from 'react';
24+
25+
// $FlowExpectedError: Reason checked type sufficiently
26+
import * as ReasonReact from 'reason-react/src/ReasonReact.js';
27+
1928
// In case of type error, check the type of 'innerStuffContents' in 'TestImport.re' and './exportNestedValues'.
2029
export const innerStuffContentsTypeChecked: {|+x: number|} = innerStuffContentsNotChecked.MiddleLevelElements.stuff.InnerStuff.innerStuffContents;
2130

@@ -39,3 +48,15 @@ export const defaultValueTypeChecked: number = defaultValueNotChecked;
3948

4049
// Export 'defaultValue' early to allow circular import from the '.bs.js' file.
4150
export const defaultValue: mixed = defaultValueTypeChecked;
51+
52+
export type Props = {|+show: boolean, +Message: ?string|};
53+
54+
// In case of type error, check the type of 'make' in 'TestImport.re' and the props of './interop/MyBanner.component'.
55+
export function MyBannerInternalTypeChecked(props: Props) {
56+
return <TopLevelClass.MiddleLevelElements.MyBannerInternal {...props}/>;
57+
}
58+
59+
// Export 'make' early to allow circular import from the '.bs.js' file.
60+
export const make: mixed = function _(show, Message, children) { return ReasonReact.wrapJsForReason(TopLevelClass.MiddleLevelElements.MyBannerInternal, {show: show, Message: Message}, children); };
61+
62+
export type message = {|+text: string|};

examples/flow-react-example/src/TestImport.re

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,24 @@ external valueStartingWithUpperCaseLetter: string = "";
2222
[@genType.import "./exportNestedValues"]
2323
[@genType.as "default"]
2424
[@bs.module "./TestImport.gen"]
25-
external defaultValue: int = "";
25+
external defaultValue: int = "";
26+
27+
[@genType]
28+
type message = {text: string};
29+
30+
[@genType.import "./interop/MyBanner.component"]
31+
[@genType.as "TopLevelClass.MiddleLevelElements.MyBannerInternal"]
32+
[@bs.module "./ImportMyBanner.gen"]
33+
external make:
34+
(~show: bool) =>
35+
[@genType.as "Message"] (
36+
(~message: Js.Nullable.t(string), 'a) =>
37+
ReasonReact.component(
38+
ReasonReact.stateless,
39+
ReasonReact.noRetainedProps,
40+
ReasonReact.actionless,
41+
)
42+
) =
43+
"";
44+
45+
let make = make;

examples/flow-react-example/src/interop/MyBanner.component.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ class App extends React.Component<Props> {
2323
}
2424
}
2525

26-
module.exports = App;
26+
export default App;
27+
28+
export class TopLevelClass {
29+
static MiddleLevelElements = {
30+
MyBannerInternal: App
31+
};
32+
}

examples/typescript-react-example/src/MyBanner.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ class App extends React.PureComponent<Props> {
2222
}
2323
}
2424

25+
export class TopLevelClass {
26+
static MiddleLevelElements = {
27+
MyBannerInternal: App
28+
};
29+
}
30+
2531
export default App;

examples/typescript-react-example/src/TestImport.bs.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/typescript-react-example/src/TestImport.gen.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import {ValueStartingWithUpperCaseLetter as valueStartingWithUpperCaseLetterNotC
88

99
import {default as defaultValueNotChecked} from './exportNestedValues';
1010

11+
import {TopLevelClass as TopLevelClass} from './MyBanner';
12+
13+
import * as React from 'react';
14+
15+
// tslint:disable-next-line:no-var-requires
16+
const ReasonReact = require('reason-react/src/ReasonReact.js');
17+
1118
// In case of type error, check the type of 'innerStuffContents' in 'TestImport.re' and './exportNestedValues'.
1219
export const innerStuffContentsTypeChecked: {readonly x: number} = innerStuffContentsNotChecked.MiddleLevelElements.stuff.InnerStuff.innerStuffContents;
1320

@@ -31,3 +38,17 @@ export const defaultValueTypeChecked: number = defaultValueNotChecked;
3138

3239
// Export 'defaultValue' early to allow circular import from the '.bs.js' file.
3340
export const defaultValue: unknown = defaultValueTypeChecked as number;
41+
42+
// tslint:disable-next-line:interface-over-type-literal
43+
export type Props = {readonly show: boolean, readonly message?: message};
44+
45+
// In case of type error, check the type of 'make' in 'TestImport.re' and the props of './MyBanner'.
46+
export function MyBannerInternalTypeChecked(props: Props) {
47+
return <TopLevelClass.MiddleLevelElements.MyBannerInternal {...props}/>;
48+
}
49+
50+
// Export 'make' early to allow circular import from the '.bs.js' file.
51+
export const make: unknown = function _(show: any, message: any, children: any) { return ReasonReact.wrapJsForReason(TopLevelClass.MiddleLevelElements.MyBannerInternal, {show: show, message: (message == null ? message : {text:message[0]})}, children); };
52+
53+
// tslint:disable-next-line:interface-over-type-literal
54+
export type message = {readonly text: string};

examples/typescript-react-example/src/TestImport.re

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,21 @@ external valueStartingWithUpperCaseLetter: string = "";
2222
[@genType.import "./exportNestedValues"]
2323
[@genType.as "default"]
2424
[@bs.module "./TestImport.gen"]
25-
external defaultValue: int = "";
25+
external defaultValue: int = "";
26+
27+
[@genType]
28+
type message = {text: string};
29+
30+
[@genType.import "./MyBanner"]
31+
[@genType.as "TopLevelClass.MiddleLevelElements.MyBannerInternal"]
32+
[@bs.module "./ImportMyBanner.gen"]
33+
external make:
34+
(~show: bool, ~message: option(message)=?, 'a) =>
35+
ReasonReact.component(
36+
ReasonReact.stateless,
37+
ReasonReact.noRetainedProps,
38+
ReasonReact.actionless,
39+
) =
40+
"";
41+
42+
let make = make;

src/CodeItem.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type exportVariantType = {
3030
};
3131

3232
type importComponent = {
33+
asPath: string,
3334
childrenTyp: typ,
3435
exportType,
3536
fileName: ModuleName.t,

0 commit comments

Comments
 (0)