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

Commit

Permalink
Turn on type expansion. Add a bunch of examples.
Browse files Browse the repository at this point in the history
Now types used by other annotated types, if in the same file, are consider implicitly annotated.

This implements the major new features proposed in #70.

Addresses the repro example in #64.
  • Loading branch information
cristianoc committed Oct 23, 2018
1 parent efc25fc commit a1bf47a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
13 changes: 13 additions & 0 deletions examples/reason-react-example/src/TypeExpansion.bs.js

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

19 changes: 19 additions & 0 deletions examples/reason-react-example/src/TypeExpansion.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This type references module Tuple -- an import must be generated when this is expanded */
type lowerType = {person: Tuples.person};

type middleType = {. "lowerType": lowerType};

/* This is the only annotated type. Type expansion. */
[@genType]
type topType = (int, middleType);

[@genType]
let testConversion = (x: topType) => x;

/* MarcelCutt's example */
module A = {
type user = {name: string};
};

[@genType]
type b = A.user;
22 changes: 22 additions & 0 deletions examples/reason-react-example/src/TypeExpansion.re.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @flow strict
* @generated
* @nolint
*/

// $FlowExpectedError: Reason checked type sufficiently
const TypeExpansionBS = require('./TypeExpansion.bs');

import type {person as Tuples_person} from '../src/basics/Tuples.re';

export type lowerType = {|+person: Tuples_person|};

export type middleType = {|+lowerType: lowerType|};

export type topType = [number, middleType];

export type A_user = {|+name: string|};

export type b = A_user;

export const testConversion: (topType) => topType = function _(Arg1) { const result = TypeExpansionBS.testConversion([Arg1[0], {lowerType:[Arg1[1].lowerType.person]}]); return [result[0], {lowerType:{person:result[1].lowerType[0]}}] };
3 changes: 1 addition & 2 deletions src/EmitJs.re
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,13 @@ let emitImportTypes =

let inlineAnnotatedTypes =
(~config, ~typeDeclarations, typeMap: Translation.typeMap) => {
let visited = ref(StringSet.empty);
let markedAsGenType = ref(StringSet.empty);
let initialAnnotatedTypes =
typeMap
|> StringMap.bindings
|> List.filter(((_, (_, _, genTypeKind, _))) => genTypeKind == GenType);
let inlineTyp = ((_typeName, (_, typ, genTypeKind, _))) => {
let visited = ref(StringSet.empty);
let rec visit = typ =>
switch (typ) {
| Ident(typeName, _) =>
Expand All @@ -777,7 +777,6 @@ let inlineAnnotatedTypes =
| (_, _, GenType | GenTypeOpaque | Generated, _) => ()
| (_, typ1, NoGenType, _) =>
markedAsGenType := markedAsGenType^ |> StringSet.add(typeName);
logItem("Marking type %s as GenType\n", typeName);
typ1 |> visit;
| exception Not_found => ()
};
Expand Down
2 changes: 1 addition & 1 deletion src/GenTypeCommon.re
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let defaultConfig = {
bsBlockPath: "bs-platform/lib/js/block.js",
bsCurryPath: "bs-platform/lib/js/curry.js",
importPath: Relative,
inlineAnnotations: false,
inlineAnnotations: true,
language: Flow,
module_: ES6,
modulesMap: ModuleNameMap.empty,
Expand Down

0 comments on commit a1bf47a

Please sign in to comment.