Skip to content

Commit fdc3ec6

Browse files
committed
adapt completion to look up the correct types when using a generic JSX transform
1 parent 144d056 commit fdc3ec6

File tree

7 files changed

+53
-2872
lines changed

7 files changed

+53
-2872
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10781078
| Some f -> Some (f.fname.txt, f.typ, env))
10791079
| _ -> None
10801080
in
1081-
["ReactDOM"; "domProps"] |> digToTypeForCompletion
1081+
TypeUtils.pathToElementProps package |> digToTypeForCompletion
10821082
else
10831083
CompletionJsx.getJsxLabels ~componentPath:pathToComponent
10841084
~findTypeOfValue ~package
@@ -1692,9 +1692,14 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
16921692
(* We always try to look up completion from the actual domProps type first.
16931693
This works in JSXv4. For JSXv3, we have a backup hardcoded list of dom
16941694
labels we can use for completion. *)
1695-
let fromDomProps =
1695+
let pathToElementProps = TypeUtils.pathToElementProps package in
1696+
if Debug.verbose () then
1697+
Printf.printf
1698+
"[completing-lowercase-jsx] Attempting to complete from type at %s\n"
1699+
(pathToElementProps |> String.concat ".");
1700+
let fromElementProps =
16961701
match
1697-
["ReactDOM"; "domProps"]
1702+
pathToElementProps
16981703
|> digToRecordFieldsForCompletion ~debug ~package ~opens ~full ~pos ~env
16991704
~scope
17001705
with
@@ -1713,11 +1718,13 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
17131718
else None)
17141719
|> List.map mkLabel)
17151720
in
1716-
match fromDomProps with
1717-
| Some domProps -> domProps
1721+
match fromElementProps with
1722+
| Some elementProps -> elementProps
17181723
| None ->
17191724
if debug then
1720-
Printf.printf "Could not find ReactDOM.domProps to complete from.\n";
1725+
Printf.printf
1726+
"[completing-lowercase-jsx] could not find element props to complete \
1727+
from.\n";
17211728
(CompletionJsx.domLabels
17221729
|> List.filter (fun (name, _t) ->
17231730
Utils.startsWith name prefix

analysis/src/Packages.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ let newBsPackage ~rootPath =
5959
| (major, _), None when major >= 11 -> Some true
6060
| _, ns -> Option.bind ns Json.bool
6161
in
62+
let genericJsxModule =
63+
let jsxConfig = config |> Json.get "jsx" in
64+
match jsxConfig with
65+
| Some jsxConfig -> (
66+
match jsxConfig |> Json.get "module" with
67+
| Some (String m) when String.lowercase_ascii m <> "react" ->
68+
Some m
69+
| _ -> None)
70+
| None -> None
71+
in
6272
let uncurried = uncurried = Some true in
6373
let sourceDirectories =
6474
FindFiles.getSourceDirectories ~includeDev:true ~baseDir:rootPath
@@ -121,6 +131,7 @@ let newBsPackage ~rootPath =
121131
("Opens from ReScript config file: "
122132
^ (opens |> List.map pathToString |> String.concat " "));
123133
{
134+
genericJsxModule;
124135
suffix;
125136
rescriptVersion;
126137
rootPath;

analysis/src/SharedTypes.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ type builtInCompletionModules = {
497497
}
498498

499499
type package = {
500+
genericJsxModule: string option;
500501
suffix: string;
501502
rootPath: filePath;
502503
projectFiles: FileSet.t;
@@ -718,7 +719,8 @@ module Completable = struct
718719
| Cpath cp -> "Cpath " ^ contextPathToString cp
719720
| Cdecorator s -> "Cdecorator(" ^ str s ^ ")"
720721
| CdecoratorPayload (Module s) -> "CdecoratorPayload(module=" ^ s ^ ")"
721-
| CdecoratorPayload (ModuleWithImportAttributes _) -> "CdecoratorPayload(moduleWithImportAttributes)"
722+
| CdecoratorPayload (ModuleWithImportAttributes _) ->
723+
"CdecoratorPayload(moduleWithImportAttributes)"
722724
| CdecoratorPayload (JsxConfig _) -> "JsxConfig"
723725
| CnamedArg (cp, s, sl2) ->
724726
"CnamedArg("

analysis/src/TypeUtils.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,8 @@ let removeOpensFromCompletionPath ~rawOpens ~package completionPath =
10791079
|> removeRawOpens rawOpens
10801080
in
10811081
completionPathMinusOpens
1082+
1083+
let pathToElementProps package =
1084+
match package.genericJsxModule with
1085+
| None -> ["ReactDOM"; "domProps"]
1086+
| Some g -> (g |> String.split_on_char '.') @ ["DOM"; "props"]

analysis/tests-generic-jsx-transform/src/GenericJsx.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type fragmentProps = {children?: element}
3333
@module("preact") external jsxFragment: component<fragmentProps> = "Fragment"
3434

3535
/* The Elements module is the equivalent to the ReactDOM module in React. This holds things relevant to _lowercase_ JSX elements. */
36-
module Elements = {
36+
module DOM = {
3737
/* Here you can control what props lowercase JSX elements should have.
3838
A base that the React JSX transform uses is provided via JsxDOM.domProps,
3939
but you can make this anything. The editor tooling will support
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
// <div
2-
// ^com
2+
// ^com
3+
4+
// <div testing=
5+
// ^com

0 commit comments

Comments
 (0)