Skip to content

Commit e48b26e

Browse files
committed
adapt the primitive completions inside of jsx to the generic JSX moe
1 parent 26f37f5 commit e48b26e

22 files changed

+383
-155
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,14 +1010,23 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10101010
| Some (builtinNameToComplete, typ)
10111011
when Utils.checkName builtinNameToComplete ~prefix:funNamePrefix
10121012
~exact:false ->
1013+
let name =
1014+
match package.genericJsxModule with
1015+
| None -> "React." ^ builtinNameToComplete
1016+
| Some g ->
1017+
g ^ "." ^ builtinNameToComplete
1018+
|> String.split_on_char '.'
1019+
|> TypeUtils.removeOpensFromCompletionPath ~rawOpens
1020+
~package:full.package
1021+
|> String.concat "."
1022+
in
10131023
[
1014-
Completion.createWithSnippet
1015-
~name:("React." ^ builtinNameToComplete)
1016-
~kind:(Value typ) ~env ~sortText:"A"
1024+
Completion.createWithSnippet ~name ~kind:(Value typ) ~env
1025+
~sortText:"A"
10171026
~docstring:
10181027
[
10191028
"Turns `" ^ builtinNameToComplete
1020-
^ "` into `React.element` so it can be used inside of JSX.";
1029+
^ "` into a JSX element so it can be used inside of JSX.";
10211030
]
10221031
();
10231032
]

analysis/src/CompletionDecorators.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,17 @@ Example `@raises(Exn)` or `@raises([E1, E2, E3])` for multiple exceptions.
164164

165165
You will need this decorator whenever you want to use a ReScript / React component in ReScript JSX expressions.
166166

167-
Note: The `@react.component` decorator requires the react-jsx config to be set in your `bsconfig.json` to enable the required React transformations.
167+
Note: The `@react.component` decorator requires the `jsx` config to be set in your `rescript.json`/`bsconfig.json` to enable the required React transformations.
168168

169169
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#react-component-decorator).|};
170170
] );
171+
( "jsx.component",
172+
None,
173+
[
174+
{|The `@jsx.component` decorator is used to annotate functions that are JSX components used with ReScript's [generic JSX transform](https://rescript-lang.org/docs/manual/latest/jsx#generic-jsx-transform-jsx-beyond-react-experimental).
175+
176+
You will need this decorator whenever you want to use a JSX component in ReScript JSX expressions.|};
177+
] );
171178
( "return",
172179
Some "return(${1:nullable})",
173180
[

analysis/src/CompletionFrontEnd.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
659659
let value_binding (iterator : Ast_iterator.iterator)
660660
(value_binding : Parsetree.value_binding) =
661661
let oldInJsxContext = !inJsxContext in
662-
if Utils.isReactComponent value_binding then inJsxContext := true;
662+
if Utils.isJsxComponent value_binding then inJsxContext := true;
663663
(match value_binding with
664664
| {pvb_pat = {ppat_desc = Ppat_constraint (_pat, coreType)}; pvb_expr}
665665
when locHasCursor pvb_expr.pexp_loc -> (

analysis/src/TypeUtils.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,4 +1083,4 @@ let removeOpensFromCompletionPath ~rawOpens ~package completionPath =
10831083
let pathToElementProps package =
10841084
match package.genericJsxModule with
10851085
| None -> ["ReactDOM"; "domProps"]
1086-
| Some g -> (g |> String.split_on_char '.') @ ["DOM"; "props"]
1086+
| Some g -> (g |> String.split_on_char '.') @ ["Elements"; "props"]

analysis/src/Utils.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ let rec unwrapIfOption (t : Types.type_expr) =
156156
| Tconstr (Path.Pident {name = "option"}, [unwrappedType], _) -> unwrappedType
157157
| _ -> t
158158

159-
let isReactComponent (vb : Parsetree.value_binding) =
159+
let isJsxComponent (vb : Parsetree.value_binding) =
160160
vb.pvb_attributes
161161
|> List.exists (function
162-
| {Location.txt = "react.component"}, _payload -> true
162+
| {Location.txt = "react.component" | "jsx.component"}, _payload -> true
163163
| _ -> false)
164164

165165
let checkName name ~prefix ~exact =

analysis/src/Xform.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ module AddTypeAnnotation = struct
215215
match si.pstr_desc with
216216
| Pstr_value (_recFlag, bindings) ->
217217
let processBinding (vb : Parsetree.value_binding) =
218-
(* Can't add a type annotation to a react component, or the compiler crashes *)
219-
let isReactComponent = Utils.isReactComponent vb in
220-
if not isReactComponent then processPattern vb.pvb_pat;
218+
(* Can't add a type annotation to a jsx component, or the compiler crashes *)
219+
let isJsxComponent = Utils.isJsxComponent vb in
220+
if not isJsxComponent then processPattern vb.pvb_pat;
221221
processFunction vb.pvb_expr
222222
in
223223
bindings |> List.iter (processBinding ~argNum:1);

analysis/tests-generic-jsx-transform/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/tests-generic-jsx-transform/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
},
66
"private": true,
77
"dependencies": {
8-
"rescript": "11.1.0-rc.1"
8+
"rescript": "11.1.0-rc.2"
99
}
1010
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ 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 DOM = {
36+
module Elements = {
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
4040
autocompletion etc for your specific type. */
4141
type props = {
42-
testing?: bool,
43-
test2?: string
42+
testing?: bool,
43+
test2?: string,
44+
children?: element
4445
}
4546

4647
@module("preact")
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
// <div
22
// ^com
33

4-
// <div testing=
5-
// ^com
4+
// <div testing={}
5+
// ^com
6+
7+
module SomeComponent = {
8+
@jsx.component
9+
let make = (~someProp) => {
10+
let someString = ""
11+
let someInt = 12
12+
let someArr = [GenericJsx.null]
13+
ignore(someInt)
14+
ignore(someArr)
15+
// someString->st
16+
// ^com
17+
open GenericJsx
18+
<div>
19+
{GenericJsx.string(someProp)}
20+
<div> {GenericJsx.null} </div>
21+
// {someString->st}
22+
// ^com
23+
</div>
24+
}
25+
}

0 commit comments

Comments
 (0)