Skip to content

Commit 3853ddf

Browse files
authored
Don't produce duplicate type definitions for recursive types (#7524)
* Don't produce duplicate type definitions for recursive types * Add CHANGELOG
1 parent 97a81e5 commit 3853ddf

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497
2525
- Treat `throw` like `raise` in analysis. https://github.com/rescript-lang/rescript/pull/7521
2626
- Fix `index out of bounds` exception thrown in rare cases by `rescript-editor-analysis.exe codeAction` command. https://github.com/rescript-lang/rescript/pull/7523
27+
- Don't produce duplicate type definitions for recursive types on hover. https://github.com/rescript-lang/rescript/pull/7524
2728

2829
#### :nail_care: Polish
2930

analysis/src/Hover.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
open SharedTypes
22

3+
module StringSet = Set.Make (String)
4+
35
let showModuleTopLevel ~docstring ~isType ~name (topLevel : Module.item list) =
46
let contents =
57
topLevel
@@ -115,7 +117,18 @@ let expandTypes ~file ~package ~supportsMarkdownLinks typ =
115117
],
116118
`InlineType )
117119
| all ->
120+
let typesSeen = ref StringSet.empty in
121+
let typeId ~(env : QueryEnv.t) ~name =
122+
env.file.moduleName :: List.rev (name :: env.pathRev) |> String.concat "."
123+
in
118124
( all
125+
(* Don't produce duplicate type definitions for recursive types *)
126+
|> List.filter (fun {env; name} ->
127+
let typeId = typeId ~env ~name in
128+
if StringSet.mem typeId !typesSeen then false
129+
else (
130+
typesSeen := StringSet.add typeId !typesSeen;
131+
true))
119132
|> List.map (fun {decl; env; loc; path} ->
120133
let linkToTypeDefinitionStr =
121134
if

tests/analysis_tests/tests/src/Hover.res

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ let payloadVariant = InlineRecord({field1: 1, field2: true})
262262
let payloadVariant2 = Args(1, true)
263263
// ^hov
264264

265+
module RecursiveVariants = {
266+
type rec t = Action1(int) | Action2(float) | Batch(array<t>)
267+
}
268+
269+
let recursiveVariant = RecursiveVariants.Action1(1)
270+
// ^hov
271+
265272
// Hover on unsaved
266273
// let fff = "hello"; fff
267274
// ^hov

tests/analysis_tests/tests/src/expected/Hover.res.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,13 @@ Hover src/Hover.res 258:22
309309
Hover src/Hover.res 261:23
310310
{"contents": {"kind": "markdown", "value": "```rescript\npayloadVariants\nArgs(int, bool)\n```\n\n---\n\n```\n \n```\n```rescript\ntype payloadVariants =\n | InlineRecord({field1: int, field2: bool})\n | Args(int, bool)\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C256%2C0%5D)\n"}}
311311

312-
Hover src/Hover.res 265:23
312+
Hover src/Hover.res 268:42
313+
{"contents": {"kind": "markdown", "value": "```rescript\nRecursiveVariants.t\nAction1(int)\n```\n\n---\n\n```\n \n```\n```rescript\ntype RecursiveVariants.t =\n | Action1(int)\n | Action2(float)\n | Batch(array<t>)\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C265%2C2%5D)\n"}}
314+
315+
Hover src/Hover.res 272:23
313316
Nothing at that position. Now trying to use completion.
314-
posCursor:[265:23] posNoWhite:[265:22] Found expr:[265:22->265:25]
315-
Pexp_ident fff:[265:22->265:25]
317+
posCursor:[272:23] posNoWhite:[272:22] Found expr:[272:22->272:25]
318+
Pexp_ident fff:[272:22->272:25]
316319
Completable: Cpath Value[fff]
317320
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
318321
Resolved opens 1 Stdlib
@@ -323,10 +326,10 @@ Resolved opens 1 Stdlib
323326
ContextPath string
324327
{"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}}
325328

326-
Hover src/Hover.res 268:33
329+
Hover src/Hover.res 275:33
327330
Nothing at that position. Now trying to use completion.
328-
posCursor:[268:33] posNoWhite:[268:32] Found expr:[268:31->268:40]
329-
Pexp_ident someField:[268:31->268:40]
331+
posCursor:[275:33] posNoWhite:[275:32] Found expr:[275:31->275:40]
332+
Pexp_ident someField:[275:31->275:40]
330333
Completable: Cpath Value[someField]
331334
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
332335
Resolved opens 1 Stdlib
@@ -339,9 +342,9 @@ ContextPath Value[x]
339342
Path x
340343
{"contents": {"kind": "markdown", "value": "```rescript\nbool\n```"}}
341344

342-
Hover src/Hover.res 271:8
345+
Hover src/Hover.res 278:8
343346
{"contents": {"kind": "markdown", "value": "\n [`Belt.Array`]()\n\n **mutable array**: Utilities functions\n\n```rescript\nmodule Array: {\n module Id\n module Array\n module SortArray\n module MutableQueue\n module MutableStack\n module List\n module Range\n module Set\n module Map\n module MutableSet\n module MutableMap\n module HashSet\n module HashMap\n module Option\n module Result\n module Int\n module Float\n}\n```"}}
344347

345-
Hover src/Hover.res 274:6
348+
Hover src/Hover.res 281:6
346349
{"contents": {"kind": "markdown", "value": "```rescript\ntype aliased = variant\n```\n\n---\n\n```\n \n```\n```rescript\ntype variant = CoolVariant | OtherCoolVariant\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C251%2C0%5D)\n"}}
347350

0 commit comments

Comments
 (0)