Skip to content

Commit 1d7dfb7

Browse files
authoredApr 12, 2024
Make JSX completion work for React.component (#966)
* make jsx completion work for React.component * changelog
1 parent c9520bf commit 1d7dfb7

6 files changed

+58
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :nail_care: Polish
1616

1717
- Make sure doc strings are always on top in hovers. https://github.com/rescript-lang/rescript-vscode/pull/956
18+
- Make JSX completion work for `make` functions of type `React.component<props>`, like what you get when using `React.lazy_`. https://github.com/rescript-lang/rescript-vscode/pull/966
1819

1920
#### :rocket: New Feature
2021

‎analysis/src/CompletionJsx.ml

+11
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,17 @@ let getJsxLabels ~componentPath ~findTypeOfValue ~package =
747747
_ ) ->
748748
(* JSX V3 *)
749749
getFieldsV3 tObj
750+
| Tconstr (p, [propsType], _) when Path.name p = "React.component" -> (
751+
let rec getPropsType (t : Types.type_expr) =
752+
match t.desc with
753+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> getPropsType t1
754+
| Tconstr (path, typeArgs, _) when Path.last path = "props" ->
755+
Some (path, typeArgs)
756+
| _ -> None
757+
in
758+
match propsType |> getPropsType with
759+
| Some (path, typeArgs) -> getFieldsV4 ~path ~typeArgs
760+
| None -> [])
750761
| Tarrow (Nolabel, {desc = Tconstr (path, typeArgs, _)}, _, _)
751762
when Path.last path = "props" ->
752763
(* JSX V4 *)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type status = On | Off
2+
3+
@@jsxConfig({version: 4, mode: "automatic"})
4+
5+
@react.component
6+
let make = (~status: status, ~name: string) => {
7+
ignore(status)
8+
ignore(name)
9+
React.null
10+
}

‎analysis/tests/src/CompletionJsxProps.res

+10
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ let tsomeVar = #two
3434

3535
// let _ = <CompletionSupport.TestComponent on={t}
3636
// ^com
37+
38+
@@jsxConfig({version: 4, mode: "automatic"})
39+
40+
module CompletableComponentLazy = {
41+
let loadComponent = () => Js.import(CompletableComponent.make)
42+
let make = React.lazy_(loadComponent)
43+
}
44+
45+
// let _ = <CompletableComponentLazy status=
46+
// ^com

‎analysis/tests/src/expected/CompletableComponent.res.txt

Whitespace-only changes.

‎analysis/tests/src/expected/CompletionJsxProps.res.txt

+26
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,29 @@ Path CompletionSupport.TestComponent.make
330330
"documentation": null
331331
}]
332332

333+
Complete src/CompletionJsxProps.res 44:44
334+
posCursor:[44:44] posNoWhite:[44:43] Found expr:[44:12->44:44]
335+
JSX <CompletableComponentLazy:[44:12->44:36] status[44:37->44:43]=...__ghost__[0:-1->0:-1]> _children:None
336+
Completable: Cexpression CJsxPropValue [CompletableComponentLazy] status
337+
Package opens Pervasives.JsxModules.place holder
338+
Resolved opens 1 pervasives
339+
ContextPath CJsxPropValue [CompletableComponentLazy] status
340+
Path CompletableComponentLazy.make
341+
[{
342+
"label": "On",
343+
"kind": 4,
344+
"tags": [],
345+
"detail": "On",
346+
"documentation": {"kind": "markdown", "value": "```rescript\nOn\n```\n\n```rescript\ntype status = On | Off\n```"},
347+
"insertText": "{On}",
348+
"insertTextFormat": 2
349+
}, {
350+
"label": "Off",
351+
"kind": 4,
352+
"tags": [],
353+
"detail": "Off",
354+
"documentation": {"kind": "markdown", "value": "```rescript\nOff\n```\n\n```rescript\ntype status = On | Off\n```"},
355+
"insertText": "{Off}",
356+
"insertTextFormat": 2
357+
}]
358+

0 commit comments

Comments
 (0)