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

Commit 72fa187

Browse files
committed
Add support for comments in expor types.
1 parent 429a10b commit 72fa187

File tree

8 files changed

+56
-20
lines changed

8 files changed

+56
-20
lines changed

sample-project/src/Component2.re.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ const Component2BS = require("./Component2.bs");
44
const CreateBucklescriptBlock = require("bs-platform/lib/js/block.js");
55

66
// No need to import locally visible type block. Make sure it is also marked with @genFlow
7-
export opaque type VariantA = any // Reason type already checked. Making it opaque;
7+
export opaque type VariantA = any;
88
export const A: VariantA = 0;
9-
export opaque type VariantB = any // Reason type already checked. Making it opaque;
9+
export opaque type VariantB = any;
1010
export const B: (number, number) => VariantB = function _(Arg1, Arg2) { return CreateBucklescriptBlock.__(0, [Arg1, Arg2]) }
11-
export opaque type VariantC = any // Reason type already checked. Making it opaque;
11+
export opaque type VariantC = any;
1212
export const C: (?number) => VariantC = function _(Arg1) { return CreateBucklescriptBlock.__(1, [(Arg1 === null ? undefined : Arg1)]) }
1313
export type variant =
1414
| VariantA
1515
| VariantB
1616
| VariantC;
17-
export opaque type BlockBlock = any // Reason type already checked. Making it opaque;
17+
export opaque type BlockBlock = any;
1818
export const Block: BlockBlock = 0;
1919
export type block =
2020
| BlockBlock;

sample-project/src/Types.re

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
[@genFlow]
22
type typeWithVars('x, 'y, 'z) =
33
| A('x, 'y)
4-
| B('z);
4+
| B('z);
5+
6+
[@genFlow]
7+
type coord = {
8+
x: int,
9+
y: int,
10+
};

sample-project/src/Types.re.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
const CreateBucklescriptBlock = require("bs-platform/lib/js/block.js");
44

5-
export opaque type TypeWithVarsA<x,y> = any // Reason type already checked. Making it opaque;
5+
export opaque type TypeWithVarsA<x,y> = any;
66
export const A: <x,y>(x, y) => TypeWithVarsA<x,y> = function _(Arg1, Arg2) { return CreateBucklescriptBlock.__(0, [Arg1, Arg2]) }
7-
export opaque type TypeWithVarsB<z> = any // Reason type already checked. Making it opaque;
7+
export opaque type TypeWithVarsB<z> = any;
88
export const B: <z>(z) => TypeWithVarsB<z> = function _(Arg1) { return CreateBucklescriptBlock.__(1, [Arg1]) }
99
export type typeWithVars<x,y,z> =
1010
| TypeWithVarsA<x,y>
1111
| TypeWithVarsB<z>;
12+
export opaque type coord = any; /* Record type not supported */
1213

sample-project/src/nested/Nested.re.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const CreateBucklescriptBlock = require("bs-platform/lib/js/block.js");
44
const NestedBS = require("./Nested.bs");
55

66
import type {variant as Component2variant} from '../../src/Component2.re';
7-
export opaque type VariantA = any // Reason type already checked. Making it opaque;
7+
export opaque type VariantA = any;
88
export const A: VariantA = 0;
9-
export opaque type VariantB = any // Reason type already checked. Making it opaque;
9+
export opaque type VariantB = any;
1010
export const B: (number, number) => VariantB = function _(Arg1, Arg2) { return CreateBucklescriptBlock.__(0, [Arg1, Arg2]) }
11-
export opaque type VariantC = any // Reason type already checked. Making it opaque;
11+
export opaque type VariantC = any;
1212
export const C: (?number) => VariantC = function _(Arg1) { return CreateBucklescriptBlock.__(1, [(Arg1 === null ? undefined : Arg1)]) }
1313
export type variant =
1414
| VariantA

src/CodeItem.re

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type exportType = {
88
opaque: bool,
99
typeParams: list(typ),
1010
typeName: string,
11+
comment: option(string),
1112
typ,
1213
};
1314

@@ -179,15 +180,16 @@ let createFunctionType = (generics, argConvertableTypes, resultType) =>
179180
Arrow(generics, args, resultType);
180181
};
181182

182-
let exportType = (~opaque, typeParams, ~typeName, typ) => {
183+
let exportType = (~opaque, typeParams, ~typeName, ~comment=?, typ) => {
183184
opaque,
184185
typeParams,
185186
typeName,
187+
comment,
186188
typ,
187189
};
188190

189-
let codeItemForExportType = (~opaque, typeParams, ~typeName, typ) =>
190-
ExportType({opaque, typeParams, typeName, typ});
191+
let codeItemForExportType = (~opaque, typeParams, ~typeName, ~comment=?, typ) =>
192+
ExportType({opaque, typeParams, typeName, comment, typ});
191193

192194
let variantLeafTypeName = (typeName, leafName) =>
193195
String.capitalize(typeName) ++ String.capitalize(leafName);
@@ -399,7 +401,18 @@ let fromTypeDecl = (~language, dec: Typedtree.type_declaration) =>
399401
let freeTypeVarNames = TypeVars.extract(typeParams);
400402
let typeVars = TypeVars.toFlow(freeTypeVarNames);
401403
let typeName = Ident.name(dec.typ_id);
402-
([], [codeItemForExportType(~opaque=true, typeVars, ~typeName, any)]);
404+
(
405+
[],
406+
[
407+
codeItemForExportType(
408+
~opaque=true,
409+
typeVars,
410+
~typeName,
411+
~comment="Record type not supported",
412+
any,
413+
),
414+
],
415+
);
403416
/*
404417
* This case includes aliasings such as:
405418
*

src/EmitJs.re

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ let requireModule =
2020
);
2121

2222
let emitExportType =
23-
(~language, {CodeItem.opaque, typeParams, typeName, typ}) =>
24-
typ |> EmitTyp.emitExportType(~language, ~opaque, ~typeName, ~typeParams);
23+
(~language, {CodeItem.opaque, typeParams, typeName, comment, typ}) =>
24+
typ
25+
|> EmitTyp.emitExportType(
26+
~language,
27+
~opaque,
28+
~typeName,
29+
~typeParams,
30+
~comment,
31+
);
2532

2633
let emitImportType = (~language, importType) =>
2734
switch (importType) {

src/EmitTyp.re

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ let commentBeforeRequire = (~language) =>
5555
language == Typescript ?
5656
"// tslint:disable-next-line:no-var-requires\n" : "";
5757

58-
let emitExportType = (~language, ~opaque, ~typeName, ~typeParams, typ) => {
58+
let emitExportType =
59+
(~language, ~opaque, ~typeName, ~typeParams, ~comment, typ) => {
5960
let typeParamsString =
6061
genericsString(List.map(typToString(~language), typeParams));
62+
let commentString =
63+
switch (comment) {
64+
| None => ""
65+
| Some(s) => " /* " ++ s ++ " */"
66+
};
6167

6268
switch (language) {
6369
| Flow =>
@@ -68,8 +74,8 @@ let emitExportType = (~language, ~opaque, ~typeName, ~typeParams, typ) => {
6874
++ typeParamsString
6975
++ " = "
7076
++ (typ |> typToString(~language))
71-
++ (opaque ? " // Reason type already checked. Making it opaque" : "")
7277
++ ";"
78+
++ commentString
7379

7480
| Typescript =>
7581
if (opaque) {
@@ -88,15 +94,17 @@ let emitExportType = (~language, ~opaque, ~typeName, ~typeParams, typ) => {
8894
++ typeParamsString
8995
++ " { protected opaque: "
9096
++ typeOfOpaqueField
91-
++ " }; /* simulate opaque types */";
97+
++ " }; /* simulate opaque types */"
98+
++ commentString;
9299
} else {
93100
"// tslint:disable-next-line:interface-over-type-literal\n"
94101
++ "export type "
95102
++ typeName
96103
++ typeParamsString
97104
++ " = "
98105
++ (typ |> typToString(~language))
99-
++ ";";
106+
++ ";"
107+
++ commentString;
100108
}
101109
| Untyped => ""
102110
};

src/EmitTyp.rei

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ let emitExportType:
2222
~opaque: bool,
2323
~typeName: string,
2424
~typeParams: list(typ),
25+
~comment: option(string),
2526
typ
2627
) =>
2728
string;

0 commit comments

Comments
 (0)