Skip to content

Commit 9c13e7a

Browse files
committed
enable completions for handwritten JSX components
1 parent f169731 commit 9c13e7a

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

analysis/src/CompletionJsx.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,19 @@ let getJsxLabels ~componentPath ~findTypeOfValue ~package =
559559
&& Path.last path = "props" ->
560560
(* JSX V4 external or interface *)
561561
getFieldsV4 ~path ~typeArgs
562+
| Tarrow (Nolabel, typ, _, _) -> (
563+
(* Component without the JSX PPX, like a make fn taking a hand-written
564+
type props. *)
565+
let rec digToConstr typ =
566+
match typ.Types.desc with
567+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> digToConstr t1
568+
| Tconstr (path, typeArgs, _) when Path.last path = "props" ->
569+
Some (path, typeArgs)
570+
| _ -> None
571+
in
572+
match digToConstr typ with
573+
| None -> []
574+
| Some (path, typeArgs) -> getFieldsV4 ~path ~typeArgs)
562575
| _ -> []
563576
in
564577
typ |> getLabels

analysis/tests/src/CompletionJsx.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ module SomeComponent = {
3131
</div>
3232
}
3333
}
34+
35+
module CompWithoutJsxPpx = {
36+
type props = {name: string}
37+
38+
let make = ({name}) => {
39+
ignore(name)
40+
React.null
41+
}
42+
}
43+
44+
// <CompWithoutJsxPpx n
45+
// ^com

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,15 @@ Completable: Cpath Value[someArr]->a <<jsx>>
358358
"documentation": {"kind": "markdown", "value": "Deprecated: `append` is not type-safe. Use `concat` instead.\n\n"}
359359
}]
360360

361+
Complete src/CompletionJsx.res 43:23
362+
posCursor:[43:23] posNoWhite:[43:22] Found expr:[43:4->43:23]
363+
JSX <CompWithoutJsxPpx:[43:4->43:21] n[43:22->43:23]=...[43:22->43:23]> _children:None
364+
Completable: Cjsx([CompWithoutJsxPpx], n, [n])
365+
[{
366+
"label": "name",
367+
"kind": 4,
368+
"tags": [],
369+
"detail": "string",
370+
"documentation": null
371+
}]
372+

0 commit comments

Comments
 (0)