Skip to content

Commit 6f4dcdb

Browse files
committed
fix ident-to-module action
1 parent 8843b12 commit 6f4dcdb

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

compiler/ml/cmt_utils.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type action_type =
99
| ReplaceWithPolymorphicVariantConstructor of {constructor_name: string}
1010
| RewriteObjectToRecord
1111
| RewriteArrayToTuple
12+
| RewriteIdentToModule of {module_name: string}
1213
| RewriteIdent of {new_ident: Longident.t}
1314
| PrefixVariableWithUnderscore
1415
| RemoveUnusedVariable
@@ -28,6 +29,8 @@ let action_to_string = function
2829
| AddAwait -> "AddAwait"
2930
| RewriteObjectToRecord -> "RewriteObjectToRecord"
3031
| RewriteArrayToTuple -> "RewriteArrayToTuple"
32+
| RewriteIdentToModule {module_name} ->
33+
Printf.sprintf "RewriteIdentToModule(%s)" module_name
3134
| PrefixVariableWithUnderscore -> "PrefixVariableWithUnderscore"
3235
| RemoveUnusedVariable -> "RemoveUnusedVariable"
3336
| ReplaceWithVariantConstructor {constructor_name} ->

compiler/ml/typetexp.ml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -873,35 +873,34 @@ let report_error env ppf = function
873873
a lowercase identifier in JS but a module in ReScript.
874874
875875
'Console' is a typical example, where JS is `console.log` and ReScript is `Console.log`. *)
876-
let as_module =
876+
let as_module_name =
877877
match lid with
878-
| Lident name -> (
878+
| Lident name -> Some (String.capitalize_ascii name)
879+
| _ -> None
880+
in
881+
let as_module =
882+
match as_module_name with
883+
| Some name -> (
879884
try
880885
Some
881886
(env
882887
|> Env.lookup_module ~load:false
883888
(Lident (String.capitalize_ascii name)))
884889
with _ -> None)
885-
| _ -> None
890+
| None -> None
886891
in
887-
match as_module with
888-
| None -> ()
889-
| Some module_path ->
890-
let new_ident =
891-
module_path |> Printtyp.string_of_path |> Longident.parse
892-
in
892+
match (as_module, as_module_name) with
893+
| Some module_path, Some as_module_name ->
893894
Cmt_utils.add_possible_action
894895
{
895896
loc;
896-
action = Cmt_utils.RewriteIdent {new_ident};
897-
description =
898-
"Change to `"
899-
^ (new_ident |> Longident.flatten |> String.concat ".")
900-
^ "`";
897+
action = Cmt_utils.RewriteIdentToModule {module_name = as_module_name};
898+
description = "Change to `" ^ as_module_name ^ "`";
901899
};
902900
Format.fprintf ppf "@,@[<v 2>@,@[%s to use the module @{<info>%a@}?@]@]"
903901
(if did_spellcheck then "Or did you mean" else "Maybe you meant")
904-
Printtyp.path module_path)
902+
Printtyp.path module_path
903+
| _ -> ())
905904
| Unbound_module lid ->
906905
(* modified *)
907906
(match lid with
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
\"Console".log(123)
1+
Console.log(123)
22

33
/* === AVAILABLE ACTIONS:
4-
- RewriteIdent(Console) - Change to `Console`
4+
- RewriteIdentToModule(Console) - Change to `Console`
55
*/

tools/src/tools.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,22 @@ module Actions = struct
14861486
else
14871487
(* Other cases when the loc is on something else in the expr *)
14881488
match (expr.pexp_desc, action.action) with
1489+
| ( Pexp_field
1490+
( {pexp_desc = Pexp_ident e},
1491+
{txt = Lident inner; loc} ),
1492+
RewriteIdentToModule {module_name} )
1493+
when e.loc = action.loc ->
1494+
Some
1495+
{
1496+
expr with
1497+
pexp_desc =
1498+
Pexp_ident
1499+
{
1500+
loc;
1501+
txt =
1502+
Longident.Ldot (Lident module_name, inner);
1503+
};
1504+
}
14891505
| Pexp_await inner, RemoveAwait
14901506
when inner.pexp_loc = action.loc ->
14911507
Some (Ast_mapper.default_mapper.expr mapper inner)
@@ -1578,6 +1594,8 @@ module Actions = struct
15781594
List.mem "RewriteObjectToRecord" filter
15791595
| RewriteArrayToTuple -> List.mem "RewriteArrayToTuple" filter
15801596
| RewriteIdent _ -> List.mem "RewriteIdent" filter
1597+
| RewriteIdentToModule _ ->
1598+
List.mem "RewriteIdentToModule" filter
15811599
| PrefixVariableWithUnderscore ->
15821600
List.mem "PrefixVariableWithUnderscore" filter
15831601
| RemoveUnusedVariable ->

0 commit comments

Comments
 (0)