Skip to content

Commit 4c21a61

Browse files
zthfhammerschmidt
authored andcommitted
Hide Stdlib in output (#7305)
* hide Stdlib. and Stdlib_ in errors and editor type expr printing * also change decl printing to get rid of Stdlib prefix * remove unecessary fn call * Revert "remove unecessary fn call" This reverts commit 7bf0b50. * commit more tests * change approach to roughly the same as Pervasives use * make diff clearer * update output * changelog * add fixture error demonstrating the stdlib parts being removed * format
1 parent 082eb1c commit 4c21a61

File tree

9 files changed

+50
-22
lines changed

9 files changed

+50
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#### :nail_care: Polish
3232

3333
- Deprecate JSON.Classify.classify. https://github.com/rescript-lang/rescript/pull/7315
34+
- Hide stdlib modules in output. https://github.com/rescript-lang/rescript/pull/7305
3435

3536
#### :bug: Bug fix
3637

compiler/ml/printtyp.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,32 @@ let ident ppf id = pp_print_string ppf (ident_name id)
5252
(* Print a path *)
5353

5454
let ident_pervasives = Ident.create_persistent "Pervasives"
55+
let ident_stdlib = Ident.create_persistent "Stdlib"
5556
let printing_env = ref Env.empty
56-
let non_shadowed_pervasive = function
57+
let non_shadowed_pervasive_or_stdlib = function
5758
| Pdot (Pident id, s, _pos) as path -> (
58-
Ident.same id ident_pervasives
59+
(Ident.same id ident_pervasives || Ident.same id ident_stdlib)
5960
&&
6061
try Path.same path (Env.lookup_type (Lident s) !printing_env)
6162
with Not_found -> true)
6263
| _ -> false
6364

6465
let rec tree_of_path = function
6566
| Pident id -> Oide_ident (ident_name id)
66-
| Pdot (_, s, _pos) as path when non_shadowed_pervasive path -> Oide_ident s
67+
| Pdot (_, s, _pos) as path when non_shadowed_pervasive_or_stdlib path ->
68+
Oide_ident s
69+
| Pdot (p, s, _pos) when String.starts_with (Path.name p) ~prefix:"Stdlib_" ->
70+
let path_name = Path.name p in
71+
let ident_without_stdlib_prefix =
72+
String.sub path_name 7 (String.length path_name - 7)
73+
in
74+
Oide_dot (Oide_ident ident_without_stdlib_prefix, s)
6775
| Pdot (p, s, _pos) -> Oide_dot (tree_of_path p, s)
6876
| Papply (p1, p2) -> Oide_apply (tree_of_path p1, tree_of_path p2)
6977

7078
let rec path ppf = function
7179
| Pident id -> ident ppf id
72-
| Pdot (_, s, _pos) as path when non_shadowed_pervasive path ->
80+
| Pdot (_, s, _pos) as path when non_shadowed_pervasive_or_stdlib path ->
7381
pp_print_string ppf s
7482
| Pdot (p, s, _pos) ->
7583
path ppf p;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ Path Array.
116116
"label": "getSymbol",
117117
"kind": 12,
118118
"tags": [],
119-
"detail": "(array<'a>, Stdlib_Symbol.t) => option<'b>",
119+
"detail": "(array<'a>, Symbol.t) => option<'b>",
120120
"documentation": null
121121
}, {
122122
"label": "getSymbolUnsafe",
123123
"kind": 12,
124124
"tags": [],
125-
"detail": "(array<'a>, Stdlib_Symbol.t) => 'b",
125+
"detail": "(array<'a>, Symbol.t) => 'b",
126126
"documentation": null
127127
}, {
128128
"label": "findIndexOpt",
@@ -218,7 +218,7 @@ Path Array.
218218
"label": "sort",
219219
"kind": 12,
220220
"tags": [],
221-
"detail": "(array<'a>, ('a, 'a) => Stdlib_Ordering.t) => unit",
221+
"detail": "(array<'a>, ('a, 'a) => Ordering.t) => unit",
222222
"documentation": {"kind": "markdown", "value": "\n`sort(array, comparator)` sorts `array` in-place using the `comparator` function.\n\nBeware this will *mutate* the array.\n\nSee [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) on MDN.\n\n## Examples\n\n```rescript\nlet array = [3, 2, 1]\narray->Array.sort((a, b) => float(a - b))\narray->assertEqual([1, 2, 3])\n```\n"}
223223
}, {
224224
"label": "length",
@@ -326,7 +326,7 @@ Path Array.
326326
"label": "compare",
327327
"kind": 12,
328328
"tags": [],
329-
"detail": "(\n array<'a>,\n array<'a>,\n ('a, 'a) => Stdlib_Ordering.t,\n) => Stdlib_Ordering.t",
329+
"detail": "(array<'a>, array<'a>, ('a, 'a) => Ordering.t) => Ordering.t",
330330
"documentation": null
331331
}, {
332332
"label": "join",
@@ -404,7 +404,7 @@ Path Array.
404404
"label": "setSymbol",
405405
"kind": 12,
406406
"tags": [],
407-
"detail": "(array<'a>, Stdlib_Symbol.t, 'b) => unit",
407+
"detail": "(array<'a>, Symbol.t, 'b) => unit",
408408
"documentation": null
409409
}, {
410410
"label": "equal",
@@ -458,7 +458,7 @@ Path Array.
458458
"label": "toSorted",
459459
"kind": 12,
460460
"tags": [],
461-
"detail": "(array<'a>, ('a, 'a) => Stdlib_Ordering.t) => array<'a>",
461+
"detail": "(array<'a>, ('a, 'a) => Ordering.t) => array<'a>",
462462
"documentation": {"kind": "markdown", "value": "\n`toSorted(array, comparator)` returns a new, sorted array from `array`, using the `comparator` function.\n\nSee [`Array.toSorted`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted) on MDN.\n\n## Examples\n\n```rescript\nlet someArray = [3, 2, 1]\n\nsomeArray\n->Array.toSorted(Int.compare)\n->assertEqual([1, 2, 3])\n\nsomeArray->assertEqual([3, 2, 1]) // Original unchanged\n```\n"}
463463
}, {
464464
"label": "reduceWithIndex",
@@ -518,7 +518,7 @@ Path Array.
518518
"label": "fromIterator",
519519
"kind": 12,
520520
"tags": [],
521-
"detail": "Stdlib_Iterator.t<'a> => array<'a>",
521+
"detail": "Iterator.t<'a> => array<'a>",
522522
"documentation": {"kind": "markdown", "value": "\n`fromIterator(iterator)`\n\nCreates an array from the provided `iterator`\n\n## Examples\n\n```rescript\nMap.fromArray([(\"foo\", 1), (\"bar\", 2)])\n->Map.values\n->Array.fromIterator\n->assertEqual([1, 2])\n```\n"}
523523
}, {
524524
"label": "forEach",

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ Path Stdlib.Int.
260260
"label": "Int.compare",
261261
"kind": 12,
262262
"tags": [],
263-
"detail": "(int, int) => Stdlib_Ordering.t",
263+
"detail": "(int, int) => Ordering.t",
264264
"documentation": null
265265
}, {
266266
"label": "Int.toPrecision",
@@ -387,7 +387,7 @@ Path Stdlib.Int.
387387
"label": "Int.compare",
388388
"kind": 12,
389389
"tags": [],
390-
"detail": "(int, int) => Stdlib_Ordering.t",
390+
"detail": "(int, int) => Ordering.t",
391391
"documentation": null
392392
}, {
393393
"label": "Int.toPrecision",
@@ -752,7 +752,7 @@ Path Stdlib.String.s
752752
"label": "->String.searchOpt",
753753
"kind": 12,
754754
"tags": [],
755-
"detail": "(string, Stdlib_RegExp.t) => option<int>",
755+
"detail": "(string, RegExp.t) => option<int>",
756756
"documentation": {"kind": "markdown", "value": "\n`searchOpt(str, regexp)`. Like `search`, but return an `option<int>`.\n\n## Examples\n\n```rescript\nString.searchOpt(\"testing 1 2 3\", %re(\"/\\d+/\")) == Some(8)\nString.searchOpt(\"no numbers\", %re(\"/\\d+/\")) == None\n```\n"},
757757
"sortText": "searchOpt",
758758
"insertText": "->String.searchOpt",
@@ -764,7 +764,7 @@ Path Stdlib.String.s
764764
"label": "->String.splitByRegExpAtMost",
765765
"kind": 12,
766766
"tags": [],
767-
"detail": "(\n string,\n Stdlib_RegExp.t,\n ~limit: int,\n) => array<option<string>>",
767+
"detail": "(string, RegExp.t, ~limit: int) => array<option<string>>",
768768
"documentation": {"kind": "markdown", "value": "\n`splitByRegExpAtMost(str, regexp, ~limit)` splits the given `str` at every\noccurrence of `regexp` and returns an array of the first `limit` resulting\nsubstrings. If `limit` is negative or greater than the number of substrings, the\narray will contain all the substrings.\nSee [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN.\n\n## Examples\n\n```rescript\nString.splitByRegExpAtMost(\"Hello World. How are you doing?\", %re(\"/ /\"), ~limit=3) == [\n Some(\"Hello\"),\n Some(\"World.\"),\n Some(\"How\"),\n]\n```\n"},
769769
"sortText": "splitByRegExpAtMost",
770770
"insertText": "->String.splitByRegExpAtMost",
@@ -800,7 +800,7 @@ Path Stdlib.String.s
800800
"label": "->String.setSymbol",
801801
"kind": 12,
802802
"tags": [],
803-
"detail": "(string, Stdlib_Symbol.t, 'a) => unit",
803+
"detail": "(string, Symbol.t, 'a) => unit",
804804
"documentation": null,
805805
"sortText": "setSymbol",
806806
"insertText": "->String.setSymbol",
@@ -812,7 +812,7 @@ Path Stdlib.String.s
812812
"label": "->String.splitByRegExp",
813813
"kind": 12,
814814
"tags": [],
815-
"detail": "(string, Stdlib_RegExp.t) => array<option<string>>",
815+
"detail": "(string, RegExp.t) => array<option<string>>",
816816
"documentation": {"kind": "markdown", "value": "\n`splitByRegExp(str, regexp)` splits the given `str` at every occurrence of\n`regexp` and returns an array of the resulting substrings.\nSee [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN.\n\n## Examples\n\n```rescript\nString.splitByRegExp(\"Jan,Feb,Mar\", %re(\"/,/\")) == [Some(\"Jan\"), Some(\"Feb\"), Some(\"Mar\")]\n```\n"},
817817
"sortText": "splitByRegExp",
818818
"insertText": "->String.splitByRegExp",
@@ -860,7 +860,7 @@ Path Stdlib.String.s
860860
"label": "->String.search",
861861
"kind": 12,
862862
"tags": [],
863-
"detail": "(string, Stdlib_RegExp.t) => int",
863+
"detail": "(string, RegExp.t) => int",
864864
"documentation": {"kind": "markdown", "value": "\n`search(str, regexp)` returns the starting position of the first match of\n`regexp` in the given `str`, or -1 if there is no match.\nSee [`String.search`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) on MDN.\n\n## Examples\n\n```rescript\nString.search(\"testing 1 2 3\", %re(\"/\\d+/\")) == 8\nString.search(\"no numbers\", %re(\"/\\d+/\")) == -1\n```\n"},
865865
"sortText": "search",
866866
"insertText": "->String.search",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Path date
99
"label": "None",
1010
"kind": 12,
1111
"tags": [],
12-
"detail": "Stdlib.Date.t",
12+
"detail": "Date.t",
1313
"documentation": {"kind": "markdown", "value": "\nA type representing a JavaScript date.\n"}
1414
}, {
1515
"label": "Some(_)",
1616
"kind": 12,
1717
"tags": [],
18-
"detail": "Stdlib.Date.t",
18+
"detail": "Date.t",
1919
"documentation": {"kind": "markdown", "value": "\nA type representing a JavaScript date.\n"},
2020
"insertText": "Some(${1:_})",
2121
"insertTextFormat": 2

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module type FT = {
6666
let make: (~name: string) => React.element
6767
}
6868
}
69-
module NormaList = Stdlib.List
69+
module NormaList = List
7070
module BeltList = Belt.List
7171
module type MT2 = ModTyp
7272
module rec RM: ModTyp

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Path Firebase.Firestore.
2929
"label": "->Firestore.getDoc",
3030
"kind": 12,
3131
"tags": [],
32-
"detail": "documentReference<\n 'documentdata,\n> => Stdlib.Promise.t<documentSnapshot<'documentdata>>",
32+
"detail": "documentReference<\n 'documentdata,\n> => Promise.t<documentSnapshot<'documentdata>>",
3333
"documentation": null,
3434
"sortText": "getDoc",
3535
"insertText": "->Firestore.getDoc",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/stdlib_removed_in_error.res:3:12-42
4+
5+
1 │ type x = Stdlib.Promise.t<int>
6+
2 │
7+
3 │ let x: x = Stdlib.Promise.resolve("hello")
8+
4 │
9+
10+
This has type: Promise.t<string> (defined as promise<string>)
11+
But it's expected to have type: x (defined as promise<int>)
12+
13+
The incompatible parts:
14+
string vs int
15+
16+
You can convert string to int with Belt.Int.fromString.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type x = Stdlib.Promise.t<int>
2+
3+
let x: x = Stdlib.Promise.resolve("hello")

0 commit comments

Comments
 (0)