Skip to content

complete for functions creating React.element when in a JSX context #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
complete for functions creating React.element when in a JSX context
  • Loading branch information
zth committed Jan 11, 2023
commit d7df33b97e483f62a21f0b5a965c762db70bb04e
56 changes: 46 additions & 10 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
else None)
| None -> [])
| None -> [])
| CPPipe {contextPath = cp; id = funNamePrefix; lhsLoc} -> (
| CPPipe {contextPath = cp; id = funNamePrefix; lhsLoc; inJsx} -> (
match
cp
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
Expand Down Expand Up @@ -1584,7 +1584,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
| None -> None
in
match completionPath with
| Some completionPath ->
| Some completionPath -> (
let completionPathMinusOpens =
completionPath
|> removeRawOpens package.opens
Expand All @@ -1599,14 +1599,50 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
|> getCompletionsForPath ~completionContext:Value ~exact:false
~package ~opens ~allFiles ~pos ~env ~scope
in
completions
|> List.map (fun (completion : Completion.t) ->
{
completion with
name = completionName completion.name;
env
(* Restore original env for the completion after x->foo()... *);
})
let completions =
completions
|> List.map (fun (completion : Completion.t) ->
{
completion with
name = completionName completion.name;
env
(* Restore original env for the completion after x->foo()... *);
})
in
(* We add React element functions to the completion if we're in a JSX context *)
let forJsxCompletion =
match (inJsx, getTypePath typ) with
| true, Some (Path.Pident id) when Ident.name id = "int" -> Some "int"
| true, Some (Path.Pident id) when Ident.name id = "float" ->
Some "float"
| true, Some (Path.Pident id) when Ident.name id = "string" ->
Some "string"
| true, Some (Path.Pident id) when Ident.name id = "array" ->
(* Make sure the array contains React.element *)
let isReactElementArray =
match typ |> extractType ~env ~package with
| Some (Tarray (_env, payload)) -> (
match
payload |> getTypePath |> Option.map pathIdentToString
with
| Some "React.element" -> true
| _ -> false)
| _ -> false
in
if isReactElementArray then Some "array" else None
| _ -> None
in
match forJsxCompletion with
| Some builtinNameToComplete
when checkName builtinNameToComplete ~prefix:funNamePrefix
~exact:false ->
[
Completion.create
~name:("React." ^ builtinNameToComplete)
~kind:(Value typ) ~env;
]
@ completions
| _ -> completions)
| None -> [])
| None -> [])
| CTuple ctxPaths ->
Expand Down
24 changes: 22 additions & 2 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
!scope |> Scope.addModule ~name:md.pmd_name.txt ~loc:md.pmd_name.loc
in
let setLookingForPat ctxPath = lookingForPat := Some ctxPath in
let inJsxContext = ref false in

let unsetLookingForPat () = lookingForPat := None in
(* Identifies expressions where we can do typed pattern or expr completion. *)
Expand Down Expand Up @@ -1035,6 +1036,14 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
if not !processed then
Ast_iterator.default_iterator.structure_item iterator item
in
let value_binding (iterator : Ast_iterator.iterator)
(value_binding : Parsetree.value_binding) =
if
locHasCursor value_binding.pvb_expr.pexp_loc
&& Utils.isReactComponent value_binding
then inJsxContext := true;
Ast_iterator.default_iterator.value_binding iterator value_binding
in
let signature (iterator : Ast_iterator.iterator)
(signature : Parsetree.signature) =
let oldScope = !scope in
Expand Down Expand Up @@ -1125,11 +1134,20 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
match exprToContextPath lhs with
| Some pipe ->
setResult
(Cpath (CPPipe {contextPath = pipe; id; lhsLoc = lhs.pexp_loc}));
(Cpath
(CPPipe
{
contextPath = pipe;
id;
lhsLoc = lhs.pexp_loc;
inJsx = !inJsxContext;
}));
true
| None -> false)
| Some (pipe, lhsLoc) ->
setResult (Cpath (CPPipe {contextPath = pipe; id; lhsLoc}));
setResult
(Cpath
(CPPipe {contextPath = pipe; id; lhsLoc; inJsx = !inJsxContext}));
true
in
typedCompletionExpr expr;
Expand Down Expand Up @@ -1201,6 +1219,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
| None -> ())
| Pexp_apply ({pexp_desc = Pexp_ident compName}, args)
when Res_parsetree_viewer.isJsxExpression expr ->
inJsxContext := true;
let jsxProps = extractJsxProps ~compName ~args in
let compNamePath = flattenLidCheckDot ~jsx:true compName in
if debug then
Expand Down Expand Up @@ -1468,6 +1487,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
structure_item;
typ;
type_kind;
value_binding;
}
in

Expand Down
6 changes: 5 additions & 1 deletion analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ module Completable = struct
| CPPipe of {
contextPath: contextPath;
id: string;
inJsx: bool; (** Whether this pipe was found in a JSX context. *)
lhsLoc: Location.t;
(** The loc item for the left hand side of the pipe. *)
}
Expand Down Expand Up @@ -648,7 +649,10 @@ module Completable = struct
completionContextToString completionContext ^ list sl
| CPField (cp, s) -> contextPathToString cp ^ "." ^ str s
| CPObj (cp, s) -> contextPathToString cp ^ "[\"" ^ s ^ "\"]"
| CPPipe {contextPath; id} -> contextPathToString contextPath ^ "->" ^ id
| CPPipe {contextPath; id; inJsx} ->
contextPathToString contextPath
^ "->" ^ id
^ if inJsx then " <<jsx>>" else ""
| CTuple ctxPaths ->
"CTuple("
^ (ctxPaths |> List.map contextPathToString |> String.concat ", ")
Expand Down
5 changes: 5 additions & 0 deletions analysis/src/Utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,8 @@ let rec unwrapIfOption (t : Types.type_expr) =
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> unwrapIfOption t1
| Tconstr (Path.Pident {name = "option"}, [unwrappedType], _) -> unwrappedType
| _ -> t
let isReactComponent (vb : Parsetree.value_binding) =
vb.pvb_attributes
|> List.exists (function
| {Location.txt = "react.component"}, _payload -> true
| _ -> false)
9 changes: 2 additions & 7 deletions analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,8 @@ module AddTypeAnnotation = struct
match si.pstr_desc with
| Pstr_value (_recFlag, bindings) ->
let processBinding (vb : Parsetree.value_binding) =
let isReactComponent =
(* Can't add a type annotation to a react component, or the compiler crashes *)
vb.pvb_attributes
|> List.exists (function
| {Location.txt = "react.component"}, _payload -> true
| _ -> false)
in
(* Can't add a type annotation to a react component, or the compiler crashes *)
let isReactComponent = Utils.isReactComponent vb in
if not isReactComponent then processPattern vb.pvb_pat;
processFunction vb.pvb_expr
in
Expand Down
37 changes: 37 additions & 0 deletions analysis/tests/src/CompletionJsx.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
let someString = "hello"
ignore(someString)

// someString->st
// ^com

module SomeComponent = {
@react.component
let make = (~someProp) => {
let someInt = 12
let someArr = [React.null]
let someInvalidArr = [12]
ignore(someInt)
ignore(someArr)
ignore(someInvalidArr)
// someString->st
// ^com
<div>
{React.string(someProp)}
<div> {React.null} </div>
// {someString->st}
// ^com
// {"Some string"->st}
// ^com
// {"Some string"->Js.String2.trim->st}
// ^com
// {someInt->}
// ^com
// {12->}
// ^com
// {someArr->a}
// ^com
// {someInvalidArr->a}
// ^com
</div>
}
}
Loading