Skip to content

Commit 1795684

Browse files
authored
Error message: Suggest module in place of value where it makes sense (#7384)
* suggest existing module name in scope when it makes sense for missing values * add another test * changelog
1 parent e950b11 commit 1795684

7 files changed

+56
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
- Better representation of JSX in AST. https://github.com/rescript-lang/rescript/pull/7286
2323

24+
#### :nail_care: Polish
25+
26+
- Improve error message for missing value when the identifier is also the name of a module in scope. https://github.com/rescript-lang/rescript/pull/7384
27+
2428
# 12.0.0-alpha.11
2529

2630
#### :bug: Bug fix

compiler/ml/typetexp.ml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ let report_error env ppf = function
841841
Printtyp.reset_and_mark_loops_list [ty; ty'];
842842
fprintf ppf "@[<hov>Method '%s' has type %a,@ which should be %a@]" l
843843
Printtyp.type_expr ty Printtyp.type_expr ty')
844-
| Unbound_value lid ->
844+
| Unbound_value lid -> (
845845
(* modified *)
846846
(match lid with
847847
| Ldot (outer, inner) ->
@@ -850,7 +850,29 @@ let report_error env ppf = function
850850
| other_ident ->
851851
Format.fprintf ppf "The value %a can't be found" Printtyp.longident
852852
other_ident);
853-
super_spellcheck ppf Env.fold_values env lid |> ignore
853+
let did_spellcheck = super_spellcheck ppf Env.fold_values env lid in
854+
(* For cases such as when the user refers to something that's a value with
855+
a lowercase identifier in JS but a module in ReScript.
856+
857+
'Console' is a typical example, where JS is `console.log` and ReScript is `Console.log`. *)
858+
(* TODO(codemods) Add codemod for refering to the module instead. *)
859+
let as_module =
860+
match lid with
861+
| Lident name -> (
862+
try
863+
Some
864+
(env
865+
|> Env.lookup_module ~load:false
866+
(Lident (String.capitalize_ascii name)))
867+
with _ -> None)
868+
| _ -> None
869+
in
870+
match as_module with
871+
| None -> ()
872+
| Some module_path ->
873+
Format.fprintf ppf "@,@[<v 2>@,@[%s to use the module @{<info>%a@}?@]@]"
874+
(if did_spellcheck then "Or did you mean" else "Maybe you meant")
875+
Printtyp.path module_path)
854876
| Unbound_module lid ->
855877
(* modified *)
856878
(match lid with
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/suggest_module_for_missing_identifier.res:1:1-7
4+
5+
1 │ console.log("Hello")
6+
2 │
7+
8+
The value console can't be found
9+
10+
Maybe you meant to use the module Console?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/suggest_module_for_missing_identifier_with_spellcheck.res:2:1-7
4+
5+
1 │ let consol = 1
6+
2 │ console.log("Hello")
7+
3 │
8+
9+
The value console can't be found
10+
11+
Hint: Did you mean consol?
12+
13+
Or did you mean to use the module Console?
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let consol = 1
2+
console.log("Hello")

tests/build_tests/super_errors/input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const { bsc } = setup(import.meta.dirname);
1010

1111
const expectedDir = path.join(import.meta.dirname, "expected");
1212

13-
const fixtures = readdirSync("fixtures").filter(
14-
fileName => path.extname(fileName) === ".res",
13+
const fixtures = readdirSync(path.join(import.meta.dirname, "fixtures")).filter(
14+
(fileName) => path.extname(fileName) === ".res"
1515
);
1616

1717
const prefix = ["-w", "+A", "-bs-jsx", "4"];

0 commit comments

Comments
 (0)