diff --git a/CHANGELOG.md b/CHANGELOG.md index eae5700ade..ed93f9f578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ # 12.0.0-alpha.14 (Unreleased) +#### :bug: Bug fix + +- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497 + # 12.0.0-alpha.13 #### :boom: Breaking Change diff --git a/tools/src/tools.ml b/tools/src/tools.ml index e0bdc28c37..2db1be7e6a 100644 --- a/tools/src/tools.ml +++ b/tools/src/tools.ml @@ -1,5 +1,7 @@ open Analysis +module StringSet = Set.Make (String) + type fieldDoc = { fieldName: string; docstrings: string list; @@ -458,6 +460,7 @@ let extractDocs ~entryPointFile ~debug = let env = QueryEnv.fromFile file in let rec extractDocsForModule ?(modulePath = [env.file.moduleName]) (structure : Module.structure) = + let valuesSeen = ref StringSet.empty in { id = modulePath |> List.rev |> ident; docstring = structure.docstring |> List.map String.trim; @@ -611,7 +614,18 @@ let extractDocs ~entryPointFile ~debug = (makeId ~identifier:(Path.name p) moduleTypeIdPath); }) - | _ -> None); + | _ -> None) + (* Filter out shadowed bindings by keeping only the last value associated with an id *) + |> List.rev + |> List.filter_map (fun (docItem : docItem) -> + match docItem with + | Value {id} -> + if StringSet.mem id !valuesSeen then None + else ( + valuesSeen := StringSet.add id !valuesSeen; + Some docItem) + | _ -> Some docItem) + |> List.rev; } in let docs = extractDocsForModule structure in