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

Commit

Permalink
Implement principle 1: an imported type is also exported to other mod…
Browse files Browse the repository at this point in the history
…ules.

Principle 1 is from: #70.

This simplifies a bunch of examples.

In particular, those discusse in #63.
  • Loading branch information
cristianoc committed Oct 21, 2018
1 parent 31dbddd commit 095cd80
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 69 deletions.
5 changes: 5 additions & 0 deletions examples/reason-react-example/src/basics/Records.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/reason-react-example/src/basics/Records.re
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,7 @@ let computeNestedNestedHalfNullable = (_: bigType): int => 0;
type testMutable = {
mutable mutableField: int,
immutableField: int,
};
};

[@genType]
let useTypeImportedInOtherModule = (x: Types.weekday) => x;
4 changes: 4 additions & 0 deletions examples/reason-react-example/src/basics/Records.re.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// $FlowExpectedError: Reason checked type sufficiently
const RecordsBS = require('./Records.bs');

import type {weekday as Types_weekday} from './Types.re';

export type coord = {|
+x: number,
+y: number,
Expand Down Expand Up @@ -94,3 +96,5 @@ export type bigType = {|
export const computeNestedNestedHalfNullable: (bigType) => number = RecordsBS.computeNestedNestedHalfNullable;

export type testMutable = {|mutableField: number, +immutableField: number|};

export const useTypeImportedInOtherModule: (Types_weekday) => Types_weekday = RecordsBS.useTypeImportedInOtherModule;
9 changes: 3 additions & 6 deletions examples/reason-react-example/src/basics/Types.re
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ let identity = (x: anInterestingFlowType) => x;
[@genType.import "./SomeFlowTypes"]
type weekday;

[@genType]
type t = weekday;

[@bs.module "./SomeFlowTypes"] external saturday: t = "SATURDAY";
[@bs.module "./SomeFlowTypes"] external sunday: t = "SUNDAY";
[@bs.module "./SomeFlowTypes"] external monday: t = "MONDAY";
[@bs.module "./SomeFlowTypes"] external saturday: weekday = "SATURDAY";
[@bs.module "./SomeFlowTypes"] external sunday: weekday = "SUNDAY";
[@bs.module "./SomeFlowTypes"] external monday: weekday = "MONDAY";

[@genType]
let isWeekend = day => day === saturday || day === sunday;
Expand Down
6 changes: 4 additions & 2 deletions examples/reason-react-example/src/basics/Types.re.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ export type peopleArray = Array<{|+name: string, +nickname: ?string|}>;

export opaque type myObj = Obj_t;

export type { anInterestingFlowType };

export const identity: (anInterestingFlowType) => anInterestingFlowType = TypesBS.identity;

export opaque type t = weekday;
export type { weekday };

export const isWeekend: (t) => boolean = TypesBS.isWeekend;
export const isWeekend: (weekday) => boolean = TypesBS.isWeekend;

export const testFunctionOnOptionsAsArgument: <T1,a>(?a, ((?a) => T1)) => T1 = function _(Arg1, Arg2) { const result = TypesBS.testFunctionOnOptionsAsArgument((Arg1 == null ? undefined : Arg1), Arg2); return result };

Expand Down
3 changes: 3 additions & 0 deletions examples/typescript-react-example/src/MyMath.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/* @flow strict */

export const round: ((_: number) => number) = Math.round;
Expand All @@ -16,3 +17,5 @@ export class AbsoluteValue {
return this.prop < 0 ? -this.prop : this.prop;
}
}

export type stringFunction = (_: string) => string;
5 changes: 5 additions & 0 deletions examples/typescript-react-example/src/UseWrapJsValue.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions examples/typescript-react-example/src/UseWrapJsValue.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[@genType]
let useGetProp = (x: WrapJsValue.AbsoluteValue.imported) =>
x->WrapJsValue.AbsoluteValue.getProp + 1;
let useGetProp = (x: WrapJsValue.AbsoluteValue.t) =>
x->WrapJsValue.AbsoluteValue.getProp + 1;

[@genType]
let useTypeImportedInOtherModule = (x: WrapJsValue.stringFunction) => x;
8 changes: 6 additions & 2 deletions examples/typescript-react-example/src/UseWrapJsValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// tslint:disable-next-line:no-var-requires
const UseWrapJsValueBS = require('./UseWrapJsValue.bs');

import {AbsoluteValue_imported as WrapJsValue_AbsoluteValue_imported} from './WrapJsValue';
import {AbsoluteValue_t as WrapJsValue_AbsoluteValue_t} from './WrapJsValue';

export const useGetProp: (_1:WrapJsValue_AbsoluteValue_imported) => number = UseWrapJsValueBS.useGetProp;
import {stringFunction as WrapJsValue_stringFunction} from './WrapJsValue';

export const useGetProp: (_1:WrapJsValue_AbsoluteValue_t) => number = UseWrapJsValueBS.useGetProp;

export const useTypeImportedInOtherModule: (_1:WrapJsValue_stringFunction) => WrapJsValue_stringFunction = UseWrapJsValueBS.useTypeImportedInOtherModule;
8 changes: 4 additions & 4 deletions examples/typescript-react-example/src/WrapJsValue.re
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ module AbsoluteValue = {
[@genType.as "AbsoluteValue"]
type t = {. "getAbs": (. unit) => int};

[@genType]
type imported = t;

/* This is untyped */
[@bs.send] external getProp: t => int = "getProp";

Expand All @@ -45,4 +42,7 @@ module AbsoluteValue = {
let useGetProp = (x: AbsoluteValue.t) => x->AbsoluteValue.getProp + 1;

[@genType]
let useGetAbs = (x: AbsoluteValue.t) => x->AbsoluteValue.getAbs + 1;
let useGetAbs = (x: AbsoluteValue.t) => x->AbsoluteValue.getAbs + 1;

[@genType.import "./MyMath"]
type stringFunction;
9 changes: 7 additions & 2 deletions examples/typescript-react-example/src/WrapJsValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ const WrapJsValueBS = require('./WrapJsValue.bs');

import {AbsoluteValue as AbsoluteValue_t} from './MyMath';

import {stringFunction} from './MyMath';

// tslint:disable-next-line:interface-over-type-literal
export type point = {readonly x: number, readonly y?: number};

export const roundedNumber: number = WrapJsValueBS.roundedNumber;

export const areaValue: number = WrapJsValueBS.areaValue;

// tslint:disable-next-line:max-classes-per-file
export abstract class AbsoluteValue_imported { protected opaque!: any }; /* simulate opaque types */
// tslint:disable-next-line:interface-over-type-literal
export type AbsoluteValue_t = AbsoluteValue_t;

export const useGetProp: (_1:AbsoluteValue_t) => number = WrapJsValueBS.useGetProp;

export const useGetAbs: (_1:AbsoluteValue_t) => number = WrapJsValueBS.useGetAbs;

// tslint:disable-next-line:interface-over-type-literal
export type stringFunction = stringFunction;
2 changes: 1 addition & 1 deletion src/CodeItem.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type exportType = {
opaque: bool,
typeVars: list(string),
resolvedTypeName: string,
typ,
optTyp: option(typ),
};

type exportVariantType = {
Expand Down
21 changes: 14 additions & 7 deletions src/EmitJs.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ let requireModule = (~early, ~env, ~importPath, ~strict=false, moduleName) => {
let createExportTypeMap = (~language, codeItems): typeMap => {
let updateExportTypeMap = (exportTypeMap: typeMap, codeItem): typeMap => {
let addExportType =
({resolvedTypeName, typeVars, typ, _}: CodeItem.exportType) => {
({resolvedTypeName, typeVars, optTyp, _}: CodeItem.exportType) => {
if (Debug.codeItems) {
logItem(
"Export Type: %s%s = %s\n",
"Export Type: %s%s%s\n",
resolvedTypeName,
typeVars == [] ?
"" : "(" ++ (typeVars |> String.concat(",")) ++ ")",
typ |> EmitTyp.typToString(~language),
switch (optTyp) {
| Some(typ) => " = %s" ++ (typ |> EmitTyp.typToString(~language))
| None => ""
},
);
};
exportTypeMap |> StringMap.add(resolvedTypeName, (typeVars, typ));
switch (optTyp) {
| Some(typ) =>
exportTypeMap |> StringMap.add(resolvedTypeName, (typeVars, typ))
| None => exportTypeMap
};
};
switch (codeItem) {
| CodeItem.ExportType(exportType) => exportType |> addExportType
Expand Down Expand Up @@ -113,15 +120,15 @@ let emitExportType =
~early=false,
~emitters,
~language,
{CodeItem.opaque, typeVars, resolvedTypeName, typ},
{CodeItem.opaque, typeVars, resolvedTypeName, optTyp},
) =>
typ
resolvedTypeName
|> EmitTyp.emitExportType(
~early,
~emitters,
~language,
~opaque,
~resolvedTypeName,
~optTyp,
~typeVars,
);

Expand Down
46 changes: 34 additions & 12 deletions src/EmitTyp.re
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,39 @@ let emitExportDefault = (~emitters, ~config, name) =>
};

let emitExportType =
(~early, ~emitters, ~language, ~opaque, ~resolvedTypeName, ~typeVars, typ) => {
(
~early,
~emitters,
~language,
~opaque,
~optTyp,
~typeVars,
resolvedTypeName,
) => {
let export = early ? Emitters.exportEarly : Emitters.export;
let typeParamsString = genericsString(~typeVars);

switch (language) {
| Flow =>
"export"
++ (opaque ? " opaque " : " ")
++ "type "
++ resolvedTypeName
++ typeParamsString
++ " = "
++ (typ |> typToString(~language))
++ ";"
|> export(~emitters)

switch (optTyp) {
| Some(typ) =>
"export"
++ (opaque ? " opaque " : " ")
++ "type "
++ resolvedTypeName
++ typeParamsString
++ " = "
++ (typ |> typToString(~language))
++ ";"
|> export(~emitters)
| None =>
"export"
++ (opaque ? " opaque " : " ")
++ "type "
++ (resolvedTypeName |> EmitText.brackets)
++ ";"
|> export(~emitters)
}
| Typescript =>
if (opaque) {
/* Represent an opaque type as an absract class with a field called 'opaque'.
Expand All @@ -250,7 +267,12 @@ let emitExportType =
++ resolvedTypeName
++ typeParamsString
++ " = "
++ (typ |> typToString(~language))
++ (
switch (optTyp) {
| Some(typ) => typ |> typToString(~language)
| None => resolvedTypeName
}
)
++ ";"
|> export(~emitters);
}
Expand Down
7 changes: 4 additions & 3 deletions src/EmitTyp.rei
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ open GenTypeCommon;
let blockTagValue: (~language: language, int) => string;

let componentExportName:
(~language: language, ~fileName: ModuleName.t, ~moduleName: ModuleName.t) => string;
(~language: language, ~fileName: ModuleName.t, ~moduleName: ModuleName.t) =>
string;

let emitExportConst:
(
Expand Down Expand Up @@ -57,9 +58,9 @@ let emitExportType:
~emitters: Emitters.t,
~language: language,
~opaque: bool,
~resolvedTypeName: string,
~optTyp: option(typ),
~typeVars: list(string),
typ
string
) =>
Emitters.t;

Expand Down
Loading

0 comments on commit 095cd80

Please sign in to comment.