Skip to content

Commit 6a9ffc1

Browse files
authored
Migrate typed tree to use AST with locs for fn arguments (#7873)
* migrate function arguments to use the AST node that includes locations in the typed tree as well * fix signature help after refactor * changelog
1 parent a58ff10 commit 6a9ffc1

28 files changed

+114
-145
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#### :house: Internal
2828

29+
- Use AST nodes with locations for fn arguments in the typed tree. https://github.com/rescript-lang/rescript/pull/7873
30+
2931
# 12.0.0-beta.14
3032

3133
#### :boom: Breaking Change

analysis/reanalyze/src/Arnold.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@ module ExtendFunctionTable = struct
582582
Texp_apply {funct = {exp_desc = Texp_ident (path, {loc}, _)}; args};
583583
}
584584
when kindOpt <> None ->
585-
let checkArg ((argLabel : Asttypes.Noloc.arg_label), _argOpt) =
585+
let checkArg ((argLabel : Asttypes.arg_label), _argOpt) =
586586
match (argLabel, kindOpt) with
587-
| (Labelled l | Optional l), Some kind ->
587+
| (Labelled {txt = l} | Optional {txt = l}), Some kind ->
588588
kind |> List.for_all (fun {Kind.label} -> label <> l)
589589
| _ -> true
590590
in
@@ -624,9 +624,9 @@ module ExtendFunctionTable = struct
624624
when callee |> FunctionTable.isInFunctionInTable ~functionTable ->
625625
let functionName = Path.name callee in
626626
args
627-
|> List.iter (fun ((argLabel : Asttypes.Noloc.arg_label), argOpt) ->
627+
|> List.iter (fun ((argLabel : Asttypes.arg_label), argOpt) ->
628628
match (argLabel, argOpt |> extractLabelledArgument) with
629-
| Labelled label, Some (path, loc)
629+
| Labelled {txt = label}, Some (path, loc)
630630
when path |> FunctionTable.isInFunctionInTable ~functionTable
631631
->
632632
functionTable
@@ -672,11 +672,11 @@ module CheckExpressionWellFormed = struct
672672
->
673673
let functionName = Path.name functionPath in
674674
args
675-
|> List.iter (fun ((argLabel : Asttypes.Noloc.arg_label), argOpt) ->
675+
|> List.iter (fun ((argLabel : Asttypes.arg_label), argOpt) ->
676676
match argOpt |> ExtendFunctionTable.extractLabelledArgument with
677677
| Some (path, loc) -> (
678678
match argLabel with
679-
| Labelled label -> (
679+
| Labelled {txt = label} -> (
680680
if
681681
functionTable
682682
|> FunctionTable.functionGetKindOfLabel ~functionName
@@ -761,7 +761,7 @@ module Compile = struct
761761
let argsFromKind =
762762
innerFunctionDefinition.kind
763763
|> List.map (fun (entry : Kind.entry) ->
764-
( Asttypes.Noloc.Labelled entry.label,
764+
( Asttypes.Labelled {txt = entry.label; loc = Location.none},
765765
Some
766766
{
767767
expr with
@@ -785,7 +785,7 @@ module Compile = struct
785785
args
786786
|> List.find_opt (fun arg ->
787787
match arg with
788-
| Asttypes.Noloc.Labelled s, Some _ -> s = label
788+
| Asttypes.Labelled {txt = s}, Some _ -> s = label
789789
| _ -> false)
790790
in
791791
let argOpt =

analysis/reanalyze/src/DeadOptionalArgs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let rec hasOptionalArgs (texpr : Types.type_expr) =
4040
let rec fromTypeExpr (texpr : Types.type_expr) =
4141
match texpr.desc with
4242
| _ when not (active ()) -> []
43-
| Tarrow ({lbl = Optional s}, tTo, _, _) -> s :: fromTypeExpr tTo
43+
| Tarrow ({lbl = Optional {txt = s}}, tTo, _, _) -> s :: fromTypeExpr tTo
4444
| Tarrow (_, tTo, _, _) -> fromTypeExpr tTo
4545
| Tlink t -> fromTypeExpr t
4646
| Tsubst t -> fromTypeExpr t

analysis/reanalyze/src/DeadValue.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ let processOptionalArgs ~expType ~(locFrom : Location.t) ~locTo ~path args =
104104
| None -> Some false
105105
in
106106
match lbl with
107-
| Asttypes.Noloc.Optional s when not locFrom.loc_ghost ->
107+
| Asttypes.Optional {txt = s} when not locFrom.loc_ghost ->
108108
if argIsSupplied <> Some false then supplied := s :: !supplied;
109109
if argIsSupplied = None then suppliedMaybe := s :: !suppliedMaybe
110110
| _ -> ());

analysis/src/CompletionBackEnd.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,13 +1075,12 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10751075
(* compute the application of the first label, then the next ones *)
10761076
let args = processApply args [label] in
10771077
processApply args nextLabels
1078-
| (Asttypes.Noloc.Nolabel, _) :: nextArgs, [Asttypes.Noloc.Nolabel] ->
1079-
nextArgs
1078+
| (Asttypes.Nolabel, _) :: nextArgs, [Asttypes.Nolabel] -> nextArgs
10801079
| ((Labelled _, _) as arg) :: nextArgs, [Nolabel] ->
10811080
arg :: processApply nextArgs labels
10821081
| (Optional _, _) :: nextArgs, [Nolabel] -> processApply nextArgs labels
1083-
| ( (((Labelled s1 | Optional s1), _) as arg) :: nextArgs,
1084-
[(Labelled s2 | Optional s2)] ) ->
1082+
| ( (((Labelled {txt = s1} | Optional {txt = s1}), _) as arg) :: nextArgs,
1083+
[(Labelled {txt = s2} | Optional {txt = s2})] ) ->
10851084
if s1 = s2 then nextArgs else arg :: processApply nextArgs labels
10861085
| ((Nolabel, _) as arg) :: nextArgs, [(Labelled _ | Optional _)] ->
10871086
arg :: processApply nextArgs labels
@@ -1132,9 +1131,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
11321131
synthetic = true;
11331132
contextPath =
11341133
(match cp with
1135-
| CPApply (c, args) -> CPApply (c, args @ [Asttypes.Noloc.Nolabel])
1134+
| CPApply (c, args) -> CPApply (c, args @ [Asttypes.Nolabel])
11361135
| CPId _ when TypeUtils.isFunctionType ~env ~package typ ->
1137-
CPApply (cp, [Asttypes.Noloc.Nolabel])
1136+
CPApply (cp, [Asttypes.Nolabel])
11381137
| _ -> cp);
11391138
id = fieldName;
11401139
inJsx;
@@ -1893,8 +1892,8 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
18931892
args
18941893
|> List.map (fun ((label, typ) : typedFnArg) ->
18951894
match label with
1896-
| Optional name -> "~" ^ name ^ "=?"
1897-
| Labelled name -> "~" ^ name
1895+
| Optional {txt = name} -> "~" ^ name ^ "=?"
1896+
| Labelled {txt = name} -> "~" ^ name
18981897
| Nolabel ->
18991898
if TypeUtils.typeIsUnit typ then "()"
19001899
else (

analysis/src/CompletionFrontEnd.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,7 @@ let rec exprToContextPathInner ~(inJsxContext : bool) (e : Parsetree.expression)
303303
| Pexp_apply {funct = e1; args} -> (
304304
match exprToContextPath ~inJsxContext e1 with
305305
| None -> None
306-
| Some contexPath ->
307-
Some
308-
(CPApply (contexPath, args |> List.map fst |> List.map Asttypes.to_noloc))
309-
)
306+
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
310307
| Pexp_tuple exprs ->
311308
let exprsAsContextPaths =
312309
exprs |> List.filter_map (exprToContextPath ~inJsxContext)

analysis/src/CreateInterface.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ let printSignature ~extractor ~signature =
173173
in
174174
let lblName = labelDecl.ld_id |> Ident.name in
175175
let lbl =
176-
if labelDecl.ld_optional then Asttypes.Noloc.Optional lblName
177-
else Labelled lblName
176+
if labelDecl.ld_optional then
177+
Asttypes.Optional {txt = lblName; loc = Location.none}
178+
else Asttypes.Labelled {txt = lblName; loc = Location.none}
178179
in
179180
{
180181
retType with

analysis/src/SharedTypes.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let ident l = l |> List.map str |> String.concat "."
44

55
type path = string list
66

7-
type typedFnArg = Asttypes.Noloc.arg_label * Types.type_expr
7+
type typedFnArg = Asttypes.arg_label * Types.type_expr
88

99
let pathToString (path : path) = path |> String.concat "."
1010

@@ -619,7 +619,7 @@ module Completable = struct
619619
| CPFloat
620620
| CPBool
621621
| CPOption of contextPath
622-
| CPApply of contextPath * Asttypes.Noloc.arg_label list
622+
| CPApply of contextPath * Asttypes.arg_label list
623623
| CPId of {
624624
path: string list;
625625
completionContext: completionContext;
@@ -708,9 +708,9 @@ module Completable = struct
708708
contextPathToString cp ^ "("
709709
^ (labels
710710
|> List.map (function
711-
| Asttypes.Noloc.Nolabel -> "Nolabel"
712-
| Labelled s -> "~" ^ s
713-
| Optional s -> "?" ^ s)
711+
| Asttypes.Nolabel -> "Nolabel"
712+
| Labelled {txt} -> "~" ^ txt
713+
| Optional {txt} -> "?" ^ txt)
714714
|> String.concat ", ")
715715
^ ")"
716716
| CPArray (Some ctxPath) -> "array<" ^ contextPathToString ctxPath ^ ">"

analysis/src/SignatureHelp.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ let findActiveParameter ~argAtCursor ~args =
152152
(* If a function only has one, unlabelled argument, we can safely assume that's active whenever we're in the signature help for that function,
153153
even if we technically didn't find anything at the cursor (which we don't for empty expressions). *)
154154
match args with
155-
| [(Asttypes.Noloc.Nolabel, _)] -> Some 0
155+
| [(Asttypes.Nolabel, _)] -> Some 0
156156
| _ -> None)
157157
| Some (Unlabelled unlabelledArgumentIndex) ->
158158
let index = ref 0 in
159159
args
160160
|> List.find_map (fun (label, _) ->
161161
match label with
162-
| Asttypes.Noloc.Nolabel when !index = unlabelledArgumentIndex ->
162+
| Asttypes.Nolabel when !index = unlabelledArgumentIndex ->
163163
Some !index
164164
| _ ->
165165
index := !index + 1;
@@ -169,7 +169,7 @@ let findActiveParameter ~argAtCursor ~args =
169169
args
170170
|> List.find_map (fun (label, _) ->
171171
match label with
172-
| (Asttypes.Noloc.Labelled labelName | Optional labelName)
172+
| (Asttypes.Labelled {txt = labelName} | Optional {txt = labelName})
173173
when labelName = name ->
174174
Some !index
175175
| _ ->
@@ -472,7 +472,6 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
472472
parameters =
473473
parameters
474474
|> List.map (fun (argLabel, start, end_) ->
475-
let argLabel = Asttypes.to_noloc argLabel in
476475
let paramArgCount = !paramUnlabelledArgCount in
477476
paramUnlabelledArgCount := paramArgCount + 1;
478477
let unlabelledArgCount = ref 0 in
@@ -485,11 +484,12 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
485484
let argCount = !unlabelledArgCount in
486485
unlabelledArgCount := argCount + 1;
487486
match (lbl, argLabel) with
488-
| ( Asttypes.Noloc.Optional l1,
489-
Asttypes.Noloc.Optional l2 )
487+
| ( Asttypes.Optional {txt = l1},
488+
Asttypes.Optional {txt = l2} )
490489
when l1 = l2 ->
491490
true
492-
| Labelled l1, Labelled l2
491+
| ( Labelled {txt = l1},
492+
Labelled {txt = l2} )
493493
when l1 = l2 ->
494494
true
495495
| Nolabel, Nolabel

analysis/src/TypeUtils.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,10 @@ let getArgs ~env (t : Types.type_expr) ~full =
902902
match t.desc with
903903
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) ->
904904
getArgsLoop ~full ~env ~currentArgumentPosition t1
905-
| Tarrow ({lbl = Labelled l; typ = tArg}, tRet, _, _) ->
905+
| Tarrow ({lbl = Labelled {txt = l}; typ = tArg}, tRet, _, _) ->
906906
(SharedTypes.Completable.Labelled l, tArg)
907907
:: getArgsLoop ~full ~env ~currentArgumentPosition tRet
908-
| Tarrow ({lbl = Optional l; typ = tArg}, tRet, _, _) ->
908+
| Tarrow ({lbl = Optional {txt = l}; typ = tArg}, tRet, _, _) ->
909909
(Optional l, tArg) :: getArgsLoop ~full ~env ~currentArgumentPosition tRet
910910
| Tarrow ({lbl = Nolabel; typ = tArg}, tRet, _, _) ->
911911
(Unlabelled {argumentPosition = currentArgumentPosition}, tArg)
@@ -1144,7 +1144,7 @@ let getFirstFnUnlabelledArgType ~env ~full t =
11441144
in
11451145
let rec findFirstUnlabelledArgType labels =
11461146
match labels with
1147-
| (Asttypes.Noloc.Nolabel, t) :: _ -> Some t
1147+
| (Asttypes.Nolabel, t) :: _ -> Some t
11481148
| _ :: rest -> findFirstUnlabelledArgType rest
11491149
| [] -> None
11501150
in

0 commit comments

Comments
 (0)