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

Commit

Permalink
Add support for comments in expor types.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoc committed Aug 8, 2018
1 parent 429a10b commit 72fa187
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 20 deletions.
8 changes: 4 additions & 4 deletions sample-project/src/Component2.re.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ const Component2BS = require("./Component2.bs");
const CreateBucklescriptBlock = require("bs-platform/lib/js/block.js");

// No need to import locally visible type block. Make sure it is also marked with @genFlow
export opaque type VariantA = any // Reason type already checked. Making it opaque;
export opaque type VariantA = any;
export const A: VariantA = 0;
export opaque type VariantB = any // Reason type already checked. Making it opaque;
export opaque type VariantB = any;
export const B: (number, number) => VariantB = function _(Arg1, Arg2) { return CreateBucklescriptBlock.__(0, [Arg1, Arg2]) }
export opaque type VariantC = any // Reason type already checked. Making it opaque;
export opaque type VariantC = any;
export const C: (?number) => VariantC = function _(Arg1) { return CreateBucklescriptBlock.__(1, [(Arg1 === null ? undefined : Arg1)]) }
export type variant =
| VariantA
| VariantB
| VariantC;
export opaque type BlockBlock = any // Reason type already checked. Making it opaque;
export opaque type BlockBlock = any;
export const Block: BlockBlock = 0;
export type block =
| BlockBlock;
Expand Down
8 changes: 7 additions & 1 deletion sample-project/src/Types.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[@genFlow]
type typeWithVars('x, 'y, 'z) =
| A('x, 'y)
| B('z);
| B('z);

[@genFlow]
type coord = {
x: int,
y: int,
};
5 changes: 3 additions & 2 deletions sample-project/src/Types.re.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

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

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

6 changes: 3 additions & 3 deletions sample-project/src/nested/Nested.re.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const CreateBucklescriptBlock = require("bs-platform/lib/js/block.js");
const NestedBS = require("./Nested.bs");

import type {variant as Component2variant} from '../../src/Component2.re';
export opaque type VariantA = any // Reason type already checked. Making it opaque;
export opaque type VariantA = any;
export const A: VariantA = 0;
export opaque type VariantB = any // Reason type already checked. Making it opaque;
export opaque type VariantB = any;
export const B: (number, number) => VariantB = function _(Arg1, Arg2) { return CreateBucklescriptBlock.__(0, [Arg1, Arg2]) }
export opaque type VariantC = any // Reason type already checked. Making it opaque;
export opaque type VariantC = any;
export const C: (?number) => VariantC = function _(Arg1) { return CreateBucklescriptBlock.__(1, [(Arg1 === null ? undefined : Arg1)]) }
export type variant =
| VariantA
Expand Down
21 changes: 17 additions & 4 deletions src/CodeItem.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type exportType = {
opaque: bool,
typeParams: list(typ),
typeName: string,
comment: option(string),
typ,
};

Expand Down Expand Up @@ -179,15 +180,16 @@ let createFunctionType = (generics, argConvertableTypes, resultType) =>
Arrow(generics, args, resultType);
};

let exportType = (~opaque, typeParams, ~typeName, typ) => {
let exportType = (~opaque, typeParams, ~typeName, ~comment=?, typ) => {
opaque,
typeParams,
typeName,
comment,
typ,
};

let codeItemForExportType = (~opaque, typeParams, ~typeName, typ) =>
ExportType({opaque, typeParams, typeName, typ});
let codeItemForExportType = (~opaque, typeParams, ~typeName, ~comment=?, typ) =>
ExportType({opaque, typeParams, typeName, comment, typ});

let variantLeafTypeName = (typeName, leafName) =>
String.capitalize(typeName) ++ String.capitalize(leafName);
Expand Down Expand Up @@ -399,7 +401,18 @@ let fromTypeDecl = (~language, dec: Typedtree.type_declaration) =>
let freeTypeVarNames = TypeVars.extract(typeParams);
let typeVars = TypeVars.toFlow(freeTypeVarNames);
let typeName = Ident.name(dec.typ_id);
([], [codeItemForExportType(~opaque=true, typeVars, ~typeName, any)]);
(
[],
[
codeItemForExportType(
~opaque=true,
typeVars,
~typeName,
~comment="Record type not supported",
any,
),
],
);
/*
* This case includes aliasings such as:
*
Expand Down
11 changes: 9 additions & 2 deletions src/EmitJs.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ let requireModule =
);

let emitExportType =
(~language, {CodeItem.opaque, typeParams, typeName, typ}) =>
typ |> EmitTyp.emitExportType(~language, ~opaque, ~typeName, ~typeParams);
(~language, {CodeItem.opaque, typeParams, typeName, comment, typ}) =>
typ
|> EmitTyp.emitExportType(
~language,
~opaque,
~typeName,
~typeParams,
~comment,
);

let emitImportType = (~language, importType) =>
switch (importType) {
Expand Down
16 changes: 12 additions & 4 deletions src/EmitTyp.re
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ let commentBeforeRequire = (~language) =>
language == Typescript ?
"// tslint:disable-next-line:no-var-requires\n" : "";

let emitExportType = (~language, ~opaque, ~typeName, ~typeParams, typ) => {
let emitExportType =
(~language, ~opaque, ~typeName, ~typeParams, ~comment, typ) => {
let typeParamsString =
genericsString(List.map(typToString(~language), typeParams));
let commentString =
switch (comment) {
| None => ""
| Some(s) => " /* " ++ s ++ " */"
};

switch (language) {
| Flow =>
Expand All @@ -68,8 +74,8 @@ let emitExportType = (~language, ~opaque, ~typeName, ~typeParams, typ) => {
++ typeParamsString
++ " = "
++ (typ |> typToString(~language))
++ (opaque ? " // Reason type already checked. Making it opaque" : "")
++ ";"
++ commentString

| Typescript =>
if (opaque) {
Expand All @@ -88,15 +94,17 @@ let emitExportType = (~language, ~opaque, ~typeName, ~typeParams, typ) => {
++ typeParamsString
++ " { protected opaque: "
++ typeOfOpaqueField
++ " }; /* simulate opaque types */";
++ " }; /* simulate opaque types */"
++ commentString;
} else {
"// tslint:disable-next-line:interface-over-type-literal\n"
++ "export type "
++ typeName
++ typeParamsString
++ " = "
++ (typ |> typToString(~language))
++ ";";
++ ";"
++ commentString;
}
| Untyped => ""
};
Expand Down
1 change: 1 addition & 0 deletions src/EmitTyp.rei
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let emitExportType:
~opaque: bool,
~typeName: string,
~typeParams: list(typ),
~comment: option(string),
typ
) =>
string;
Expand Down

0 comments on commit 72fa187

Please sign in to comment.