Skip to content

Commit afa723f

Browse files
authored
Remove res.namedArgLoc attribute and store the location information directly into the label. (#7247)
* Experiment with storing the location of function named arguments in the AST. * Store label location in type argument instead of attribute. * Store the label loc directly in the label, for application for now. * restore some punning behaviour Preserve `isMobile={isMobile}` when it's like that in the source, instead of reformatting to `isMobile`. Do we want that? * Store location directly in the label on Ptyp_arrow instead of a separate field. * Store location directly in the label on `Pexp_fun`. * Put type without locs inside module Noloc. * Restore original variant names. * Update CHANGELOG.md
1 parent 7105216 commit afa723f

File tree

73 files changed

+677
-807
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+677
-807
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
- AST cleanup: represent concatenation (`++`) and (dis)equality operators (`==`, `===`, `!=`, `!==`) just like in the syntax. https://github.com/rescript-lang/rescript/pull/7248
3434
- AST cleanup: use inline record for `Ptyp_arrow`. https://github.com/rescript-lang/rescript/pull/7250
3535
- Playground: Bundle stdlib runtime so that the playground can execute functions from Core/Belt/Js. (#7255)
36+
- AST cleanup: Remove `res.namedArgLoc` attribute and store the location information directly into the label. https://github.com/rescript-lang/rescript/pull/7247
3637

3738
#### :nail_care: Polish
38-
3939
- Rewatch 1.0.10. https://github.com/rescript-lang/rescript/pull/7259
4040

4141
# 12.0.0-alpha.7

analysis/reanalyze/src/Arnold.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ module ExtendFunctionTable = struct
582582
Texp_apply {funct = {exp_desc = Texp_ident (path, {loc}, _)}; args};
583583
}
584584
when kindOpt <> None ->
585-
let checkArg ((argLabel : Asttypes.arg_label), _argOpt) =
585+
let checkArg ((argLabel : Asttypes.Noloc.arg_label), _argOpt) =
586586
match (argLabel, kindOpt) with
587587
| (Labelled l | Optional l), Some kind ->
588588
kind |> List.for_all (fun {Kind.label} -> label <> l)
@@ -624,7 +624,7 @@ module ExtendFunctionTable = struct
624624
when callee |> FunctionTable.isInFunctionInTable ~functionTable ->
625625
let functionName = Path.name callee in
626626
args
627-
|> List.iter (fun ((argLabel : Asttypes.arg_label), argOpt) ->
627+
|> List.iter (fun ((argLabel : Asttypes.Noloc.arg_label), argOpt) ->
628628
match (argLabel, argOpt |> extractLabelledArgument) with
629629
| Labelled label, Some (path, loc)
630630
when path |> FunctionTable.isInFunctionInTable ~functionTable
@@ -672,7 +672,7 @@ module CheckExpressionWellFormed = struct
672672
->
673673
let functionName = Path.name functionPath in
674674
args
675-
|> List.iter (fun ((argLabel : Asttypes.arg_label), argOpt) ->
675+
|> List.iter (fun ((argLabel : Asttypes.Noloc.arg_label), argOpt) ->
676676
match argOpt |> ExtendFunctionTable.extractLabelledArgument with
677677
| Some (path, loc) -> (
678678
match argLabel with
@@ -761,7 +761,7 @@ module Compile = struct
761761
let argsFromKind =
762762
innerFunctionDefinition.kind
763763
|> List.map (fun (entry : Kind.entry) ->
764-
( Asttypes.Labelled entry.label,
764+
( Asttypes.Noloc.Labelled entry.label,
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.Labelled s, Some _ -> s = label
788+
| Asttypes.Noloc.Labelled s, Some _ -> s = label
789789
| _ -> false)
790790
in
791791
let argOpt =

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.Optional s when not locFrom.loc_ghost ->
107+
| Asttypes.Noloc.Optional 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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
952952
(* compute the application of the first label, then the next ones *)
953953
let args = processApply args [label] in
954954
processApply args nextLabels
955-
| (Asttypes.Nolabel, _) :: nextArgs, [Asttypes.Nolabel] -> nextArgs
955+
| (Asttypes.Noloc.Nolabel, _) :: nextArgs, [Asttypes.Noloc.Nolabel] ->
956+
nextArgs
956957
| ((Labelled _, _) as arg) :: nextArgs, [Nolabel] ->
957958
arg :: processApply nextArgs labels
958959
| (Optional _, _) :: nextArgs, [Nolabel] -> processApply nextArgs labels
@@ -1007,9 +1008,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10071008
synthetic = true;
10081009
contextPath =
10091010
(match cp with
1010-
| CPApply (c, args) -> CPApply (c, args @ [Asttypes.Nolabel])
1011+
| CPApply (c, args) -> CPApply (c, args @ [Asttypes.Noloc.Nolabel])
10111012
| CPId _ when TypeUtils.isFunctionType ~env ~package typ ->
1012-
CPApply (cp, [Asttypes.Nolabel])
1013+
CPApply (cp, [Asttypes.Noloc.Nolabel])
10131014
| _ -> cp);
10141015
id = fieldName;
10151016
inJsx = false;

analysis/src/CompletionFrontEnd.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,10 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
298298
| Pexp_apply {funct = e1; args} -> (
299299
match exprToContextPath e1 with
300300
| None -> None
301-
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
301+
| Some contexPath ->
302+
Some
303+
(CPApply (contexPath, args |> List.map fst |> List.map Asttypes.to_noloc))
304+
)
302305
| Pexp_tuple exprs ->
303306
let exprsAsContextPaths = exprs |> List.filter_map exprToContextPath in
304307
if List.length exprs = List.length exprsAsContextPaths then
@@ -1446,8 +1449,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
14461449
(match lbl with
14471450
| Nolabel ->
14481451
Unlabelled {argumentPosition = currentUnlabelledCount}
1449-
| Optional name -> Optional name
1450-
| Labelled name -> Labelled name);
1452+
| Optional {txt = name} -> Optional name
1453+
| Labelled {txt = name} -> Labelled name);
14511454
})
14521455
in
14531456
(match defaultExpOpt with

analysis/src/CompletionJsx.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,20 +465,19 @@ let extractJsxProps ~(compName : Longident.t Location.loc) ~args =
465465
in
466466
let rec processProps ~acc args =
467467
match args with
468-
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
468+
| (Asttypes.Labelled {txt = "children"}, {Parsetree.pexp_loc}) :: _ ->
469469
{
470470
compName;
471471
props = List.rev acc;
472472
childrenStart =
473473
(if pexp_loc.loc_ghost then None else Some (Loc.start pexp_loc));
474474
}
475-
| ((Labelled s | Optional s), (eProp : Parsetree.expression)) :: rest -> (
476-
let namedArgLoc =
477-
eProp.pexp_attributes
478-
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "res.namedArgLoc")
479-
in
475+
| ( (Labelled {txt = s; loc} | Optional {txt = s; loc}),
476+
(eProp : Parsetree.expression) )
477+
:: rest -> (
478+
let namedArgLoc = if loc = Location.none then None else Some loc in
480479
match namedArgLoc with
481-
| Some ({loc}, _) ->
480+
| Some loc ->
482481
processProps
483482
~acc:
484483
({

analysis/src/CreateInterface.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ let printSignature ~extractor ~signature =
170170
in
171171
let lblName = labelDecl.ld_id |> Ident.name in
172172
let lbl =
173-
if labelDecl.ld_optional then Asttypes.Optional lblName
173+
if labelDecl.ld_optional then Asttypes.Noloc.Optional lblName
174174
else Labelled lblName
175175
in
176176
{

analysis/src/DumpAst.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ and printExprItem expr ~pos ~indentation =
219219
^ "arg: "
220220
^ (match arg with
221221
| Nolabel -> "Nolabel"
222-
| Labelled name -> "Labelled(" ^ name ^ ")"
223-
| Optional name -> "Optional(" ^ name ^ ")")
222+
| Labelled {txt = name} -> "Labelled(" ^ name ^ ")"
223+
| Optional {txt = name} -> "Optional(" ^ name ^ ")")
224224
^ ",\n"
225225
^ addIndentation (indentation + 2)
226226
^ "pattern: "

analysis/src/SemanticTokens.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ let command ~debug ~emitter ~path =
266266

267267
let posOfGreatherthanAfterProps =
268268
let rec loop = function
269-
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
269+
| (Asttypes.Labelled {txt = "children"}, {Parsetree.pexp_loc}) :: _ ->
270270
Loc.start pexp_loc
271271
| _ :: args -> loop args
272272
| [] -> (* should not happen *) (-1, -1)

analysis/src/SharedTypes.ml

Lines changed: 8 additions & 10 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.arg_label * Types.type_expr
7+
type typedFnArg = Asttypes.Noloc.arg_label * Types.type_expr
88

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

@@ -605,7 +605,7 @@ module Completable = struct
605605
| CPFloat
606606
| CPBool
607607
| CPOption of contextPath
608-
| CPApply of contextPath * Asttypes.arg_label list
608+
| CPApply of contextPath * Asttypes.Noloc.arg_label list
609609
| CPId of {
610610
path: string list;
611611
completionContext: completionContext;
@@ -692,7 +692,7 @@ module Completable = struct
692692
contextPathToString cp ^ "("
693693
^ (labels
694694
|> List.map (function
695-
| Asttypes.Nolabel -> "Nolabel"
695+
| Asttypes.Noloc.Nolabel -> "Nolabel"
696696
| Labelled s -> "~" ^ s
697697
| Optional s -> "?" ^ s)
698698
|> String.concat ", ")
@@ -898,14 +898,12 @@ type arg = {label: label; exp: Parsetree.expression}
898898
let extractExpApplyArgs ~args =
899899
let rec processArgs ~acc args =
900900
match args with
901-
| (((Asttypes.Labelled s | Optional s) as label), (e : Parsetree.expression))
901+
| ( ((Asttypes.Labelled {txt = s; loc} | Optional {txt = s; loc}) as label),
902+
(e : Parsetree.expression) )
902903
:: rest -> (
903-
let namedArgLoc =
904-
e.pexp_attributes
905-
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "res.namedArgLoc")
906-
in
904+
let namedArgLoc = if loc = Location.none then None else Some loc in
907905
match namedArgLoc with
908-
| Some ({loc}, _) ->
906+
| Some loc ->
909907
let labelled =
910908
{
911909
name = s;
@@ -919,7 +917,7 @@ let extractExpApplyArgs ~args =
919917
in
920918
processArgs ~acc:({label = Some labelled; exp = e} :: acc) rest
921919
| None -> processArgs ~acc rest)
922-
| (Asttypes.Nolabel, (e : Parsetree.expression)) :: rest ->
920+
| (Nolabel, (e : Parsetree.expression)) :: rest ->
923921
if e.pexp_loc.loc_ghost then processArgs ~acc rest
924922
else processArgs ~acc:({label = None; exp = e} :: acc) rest
925923
| [] -> List.rev acc

0 commit comments

Comments
 (0)