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

Commit

Permalink
Fix issue where a shadowed declaration would be emitted instead of th…
Browse files Browse the repository at this point in the history
…e shadowing one.

Fixes #354.
  • Loading branch information
cristianoc committed Feb 5, 2020
1 parent 9edcb4c commit dc7e88d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/typescript-react-example/src/Shadow.bs.js

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

8 changes: 8 additions & 0 deletions examples/typescript-react-example/src/Shadow.gen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* TypeScript file generated from Shadow.re by genType. */
/* eslint-disable import/first */


// tslint:disable-next-line:no-var-requires
const ShadowBS = require('./Shadow.bs');

export const test: (_1:void) => string = ShadowBS.test;
12 changes: 12 additions & 0 deletions examples/typescript-react-example/src/Shadow.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[@genType]
let test = () => 3;

[@genType]
let test = () => "a";

module M = {
[@genType]
let test = () => 3;

let test = () => "a";
};
36 changes: 36 additions & 0 deletions src/TranslateStructure.re
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,41 @@ let addAnnotationsToFunctionType =
| _ => type_
};

let removeValueBindingDuplicates = structureItems => {
let rec processBindings = (bindings: list(Typedtree.value_binding), ~seen) =>
switch (bindings) {
| [{vb_pat: {pat_desc: Tpat_var(id, _)}} as binding, ...otherBindings] =>
let name = Ident.name(id);
if (seen^ |> StringSet.mem(name)) {
otherBindings |> processBindings(~seen);
} else {
seen := seen^ |> StringSet.add(name);
[binding, ...otherBindings |> processBindings(~seen)];
};
| [binding, ...otherBindings] => [
binding,
...otherBindings |> processBindings(~seen),
]
| [] => []
};
let rec processItems = (items: list(Typedtree.structure_item), ~acc, ~seen) =>
switch (items) {
| [
{Typedtree.str_desc: Tstr_value(loc, valueBindings)} as item,
...otherItems,
] =>
let bindings = valueBindings |> processBindings(~seen);
let item = {...item, str_desc: Tstr_value(loc, bindings)};
otherItems |> processItems(~acc=[item, ...acc], ~seen);
| [item, ...otherItems] =>
otherItems |> processItems(~acc=[item, ...acc], ~seen)
| [] => acc
};
structureItems
|> List.rev
|> processItems(~acc=[], ~seen=ref(StringSet.empty));
};

let translateValueBinding =
(
~config,
Expand Down Expand Up @@ -415,6 +450,7 @@ and translateStructure =
};
let moduleItemGen = Runtime.moduleItemGen();
structure.Typedtree.str_items
|> removeValueBindingDuplicates
|> List.map(structItem =>
structItem
|> translateStructureItem(
Expand Down

0 comments on commit dc7e88d

Please sign in to comment.