Skip to content

Commit eaa5407

Browse files
committed
Fix jsx completion for abstract Jsx.component
1 parent 2f97455 commit eaa5407

4 files changed

Lines changed: 92 additions & 62 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#### :bug: Bug fix
2424

25+
- Fix JSX prop completions in editor analysis for components backed by abstract `Jsx.component` values. https://github.com/rescript-lang/rescript/pull/8390
2526
- Preserve multibyte characters when wrapping long source lines in compiler code frames. https://github.com/rescript-lang/rescript/pull/8520
2627

2728
#### :memo: Documentation

analysis/src/completion_jsx.ml

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -231,42 +231,40 @@ let get_jsx_labels ~component_path ~find_type_of_value ~package ~state =
231231
(name, t, env))
232232
| _ -> []
233233
in
234+
let is_component_path path =
235+
match Path.last path with
236+
| "component" -> true
237+
| _ -> false
238+
in
239+
let is_component_like_path path =
240+
match Path.last path with
241+
| "componentLike" -> true
242+
| _ -> false
243+
in
244+
let rec get_props_type (t : Types.type_expr) =
245+
match t.desc with
246+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> get_props_type t1
247+
| Tconstr (path, type_args, _) -> Some (path, type_args)
248+
| _ -> None
249+
in
234250
let rec get_labels (t : Types.type_expr) =
235251
match t.desc with
236252
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> get_labels t1
237-
| Tconstr (p, [props_type], _) when Path.name p = "React.component" -> (
238-
let rec get_props_type (t : Types.type_expr) =
239-
match t.desc with
240-
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> get_props_type t1
241-
| Tconstr (path, type_args, _) when Path.last path = "props" ->
242-
Some (path, type_args)
243-
| _ -> None
244-
in
253+
| Tconstr (p, [props_type], _) when is_component_path p -> (
245254
match props_type |> get_props_type with
246255
| Some (path, type_args) -> get_fields ~path ~type_args
247256
| None -> [])
248257
| Tarrow
249-
({lbl = Nolabel; typ = {desc = Tconstr (path, type_args, _)}}, _, _, _)
250-
when Path.last path = "props" ->
251-
get_fields ~path ~type_args
252-
| Tconstr (cl_path, [{desc = Tconstr (path, type_args, _)}; _], _)
253-
when Path.name cl_path = "React.componentLike"
254-
&& Path.last path = "props" ->
258+
({lbl = Nolabel; typ}, _, _, _) -> (
259+
match typ |> get_props_type with
260+
| Some (path, type_args) -> get_fields ~path ~type_args
261+
| None -> [])
262+
| Tconstr (cl_path, [props_type; _], _)
263+
when is_component_like_path cl_path -> (
255264
(* JSX V4 external or interface *)
256-
get_fields ~path ~type_args
257-
| Tarrow ({lbl = Nolabel; typ}, _, _, _) -> (
258-
(* Component without the JSX PPX, like a make fn taking a hand-written
259-
type props. *)
260-
let rec dig_to_constr typ =
261-
match typ.Types.desc with
262-
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> dig_to_constr t1
263-
| Tconstr (path, type_args, _) when Path.last path = "props" ->
264-
Some (path, type_args)
265-
| _ -> None
266-
in
267-
match dig_to_constr typ with
268-
| None -> []
269-
| Some (path, type_args) -> get_fields ~path ~type_args)
265+
match props_type |> get_props_type with
266+
| Some (path, type_args) -> get_fields ~path ~type_args
267+
| None -> [])
270268
| _ -> []
271269
in
272270
typ |> get_labels

tests/analysis_tests/tests/src/CompletionJsx.res

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,20 @@ module Info = {
8787
}
8888
}
8989

90+
module DomPropsComponent = {
91+
@react.componentWithProps
92+
let make = (props: JsxDOM.domProps) => {
93+
ignore(props)
94+
React.null
95+
}
96+
}
97+
9098
// <Info _type={#warning} >
9199
// ^com
92100

101+
// <DomPropsComponent onClick=>
102+
// ^com
103+
93104

94105
// let _ = <p>{"".s}</p>
95106
// ^com

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

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,20 +1035,40 @@ Path MultiPropComp.make
10351035
}
10361036
]
10371037

1038-
Complete src/CompletionJsx.res 89:26
1039-
posCursor:[89:26] posNoWhite:[89:24] Found expr:[89:3->89:27]
1040-
JSX <Info:[89:4->89:8] _type[89:9->89:14]=...[89:16->89:24]> _children:None
1038+
Complete src/CompletionJsx.res 97:26
1039+
posCursor:[97:26] posNoWhite:[97:24] Found expr:[97:3->97:27]
1040+
JSX <Info:[97:4->97:8] _type[97:9->97:14]=...[97:16->97:24]> _children:None
10411041
Completable: Cjsx([Info], "", [_type])
10421042
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
10431043
Resolved opens 1 Stdlib
10441044
Path Info.make
10451045
[ { "detail": "string", "kind": 4, "label": "key", "tags": [] } ]
10461046

1047-
Complete src/CompletionJsx.res 93:19
1048-
posCursor:[93:19] posNoWhite:[93:18] Found expr:[93:11->93:24]
1049-
JSX <p:[93:12->93:13] > _children:93:15
1050-
posCursor:[93:19] posNoWhite:[93:18] Found expr:[93:15->93:19]
1051-
Pexp_field [93:15->93:17] s:[93:18->93:19]
1047+
Complete src/CompletionJsx.res 100:30
1048+
posCursor:[100:30] posNoWhite:[100:29] Found expr:[100:3->100:31]
1049+
JSX <DomPropsComponent:[100:4->100:21] onClick[100:22->100:29]=...[100:22->100:29]> _children:None
1050+
Completable: Cexpression CJsxPropValue [DomPropsComponent] onClick
1051+
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
1052+
Resolved opens 1 Stdlib
1053+
ContextPath CJsxPropValue [DomPropsComponent] onClick
1054+
Path DomPropsComponent.make
1055+
[
1056+
{
1057+
"detail": "JsxEvent.Mouse.t => unit",
1058+
"insertText": "{${1:event} => ${0:event}}",
1059+
"insertTextFormat": 2,
1060+
"kind": 12,
1061+
"label": "event => event",
1062+
"sortText": "A",
1063+
"tags": []
1064+
}
1065+
]
1066+
1067+
Complete src/CompletionJsx.res 104:19
1068+
posCursor:[104:19] posNoWhite:[104:18] Found expr:[104:11->104:24]
1069+
JSX <p:[104:12->104:13] > _children:104:15
1070+
posCursor:[104:19] posNoWhite:[104:18] Found expr:[104:15->104:19]
1071+
Pexp_field [104:15->104:17] s:[104:18->104:19]
10521072
Completable: Cpath string.s
10531073
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
10541074
Resolved opens 1 Stdlib
@@ -1064,8 +1084,8 @@ Path s
10641084
{
10651085
"newText": "",
10661086
"range": {
1067-
"end": { "character": 18, "line": 93 },
1068-
"start": { "character": 17, "line": 93 }
1087+
"end": { "character": 18, "line": 104 },
1088+
"start": { "character": 17, "line": 104 }
10691089
}
10701090
}
10711091
],
@@ -1086,8 +1106,8 @@ Path s
10861106
{
10871107
"newText": "",
10881108
"range": {
1089-
"end": { "character": 18, "line": 93 },
1090-
"start": { "character": 17, "line": 93 }
1109+
"end": { "character": 18, "line": 104 },
1110+
"start": { "character": 17, "line": 104 }
10911111
}
10921112
}
10931113
],
@@ -1107,8 +1127,8 @@ Path s
11071127
{
11081128
"newText": "",
11091129
"range": {
1110-
"end": { "character": 18, "line": 93 },
1111-
"start": { "character": 17, "line": 93 }
1130+
"end": { "character": 18, "line": 104 },
1131+
"start": { "character": 17, "line": 104 }
11121132
}
11131133
}
11141134
],
@@ -1128,8 +1148,8 @@ Path s
11281148
{
11291149
"newText": "",
11301150
"range": {
1131-
"end": { "character": 18, "line": 93 },
1132-
"start": { "character": 17, "line": 93 }
1151+
"end": { "character": 18, "line": 104 },
1152+
"start": { "character": 17, "line": 104 }
11331153
}
11341154
}
11351155
],
@@ -1149,8 +1169,8 @@ Path s
11491169
{
11501170
"newText": "",
11511171
"range": {
1152-
"end": { "character": 18, "line": 93 },
1153-
"start": { "character": 17, "line": 93 }
1172+
"end": { "character": 18, "line": 104 },
1173+
"start": { "character": 17, "line": 104 }
11541174
}
11551175
}
11561176
],
@@ -1171,8 +1191,8 @@ Path s
11711191
{
11721192
"newText": "",
11731193
"range": {
1174-
"end": { "character": 18, "line": 93 },
1175-
"start": { "character": 17, "line": 93 }
1194+
"end": { "character": 18, "line": 104 },
1195+
"start": { "character": 17, "line": 104 }
11761196
}
11771197
}
11781198
],
@@ -1192,8 +1212,8 @@ Path s
11921212
{
11931213
"newText": "",
11941214
"range": {
1195-
"end": { "character": 18, "line": 93 },
1196-
"start": { "character": 17, "line": 93 }
1215+
"end": { "character": 18, "line": 104 },
1216+
"start": { "character": 17, "line": 104 }
11971217
}
11981218
}
11991219
],
@@ -1213,8 +1233,8 @@ Path s
12131233
{
12141234
"newText": "",
12151235
"range": {
1216-
"end": { "character": 18, "line": 93 },
1217-
"start": { "character": 17, "line": 93 }
1236+
"end": { "character": 18, "line": 104 },
1237+
"start": { "character": 17, "line": 104 }
12181238
}
12191239
}
12201240
],
@@ -1235,8 +1255,8 @@ Path s
12351255
{
12361256
"newText": "",
12371257
"range": {
1238-
"end": { "character": 18, "line": 93 },
1239-
"start": { "character": 17, "line": 93 }
1258+
"end": { "character": 18, "line": 104 },
1259+
"start": { "character": 17, "line": 104 }
12401260
}
12411261
}
12421262
],
@@ -1256,8 +1276,8 @@ Path s
12561276
{
12571277
"newText": "",
12581278
"range": {
1259-
"end": { "character": 18, "line": 93 },
1260-
"start": { "character": 17, "line": 93 }
1279+
"end": { "character": 18, "line": 104 },
1280+
"start": { "character": 17, "line": 104 }
12611281
}
12621282
}
12631283
],
@@ -1277,8 +1297,8 @@ Path s
12771297
{
12781298
"newText": "",
12791299
"range": {
1280-
"end": { "character": 18, "line": 93 },
1281-
"start": { "character": 17, "line": 93 }
1300+
"end": { "character": 18, "line": 104 },
1301+
"start": { "character": 17, "line": 104 }
12821302
}
12831303
}
12841304
],
@@ -1298,8 +1318,8 @@ Path s
12981318
{
12991319
"newText": "",
13001320
"range": {
1301-
"end": { "character": 18, "line": 93 },
1302-
"start": { "character": 17, "line": 93 }
1321+
"end": { "character": 18, "line": 104 },
1322+
"start": { "character": 17, "line": 104 }
13031323
}
13041324
}
13051325
],
@@ -1319,8 +1339,8 @@ Path s
13191339
{
13201340
"newText": "",
13211341
"range": {
1322-
"end": { "character": 18, "line": 93 },
1323-
"start": { "character": 17, "line": 93 }
1342+
"end": { "character": 18, "line": 104 },
1343+
"start": { "character": 17, "line": 104 }
13241344
}
13251345
}
13261346
],

0 commit comments

Comments
 (0)