Skip to content

Store the label loc in the application ast node. #7252

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 24 additions & 13 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,17 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
pexp_attributes;
};
args =
[(_, lhs); (_, {pexp_desc = Pexp_apply {funct = d; args; partial}})];
[
(_, _, lhs);
(_, _, {pexp_desc = Pexp_apply {funct = d; args; partial}});
];
} ->
(* Transform away pipe with apply call *)
exprToContextPath
{
pexp_desc =
Pexp_apply {funct = d; args = (Nolabel, lhs) :: args; partial};
Pexp_apply
{funct = d; args = (Nolabel, Location.none, lhs) :: args; partial};
pexp_loc;
pexp_attributes;
}
Expand All @@ -278,7 +282,8 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
args =
[
(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes});
(_, _, lhs);
(_, _, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes});
];
partial;
} ->
Expand All @@ -289,7 +294,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
Pexp_apply
{
funct = {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes};
args = [(Nolabel, lhs)];
args = [(Nolabel, Location.none, lhs)];
partial;
};
pexp_loc;
Expand All @@ -298,7 +303,8 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
| Pexp_apply {funct = e1; args} -> (
match exprToContextPath e1 with
| None -> None
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
| Some contexPath ->
Some (CPApply (contexPath, args |> List.map (fun (l, _, _) -> l))))
| Pexp_tuple exprs ->
let exprsAsContextPaths = exprs |> List.filter_map exprToContextPath in
if List.length exprs = List.length exprsAsContextPaths then
Expand Down Expand Up @@ -329,15 +335,15 @@ let completePipeChain (exp : Parsetree.expression) =
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
args = [_; (_, {pexp_desc = Pexp_apply {funct = d}})];
args = [_; (_, _, {pexp_desc = Pexp_apply {funct = d}})];
} ->
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, d.pexp_loc))
(* When the left side of the pipe we're completing is an identifier application.
Example: someArray->filterAllTheGoodStuff-> *)
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
args = [_; (_, {pexp_desc = Pexp_ident _; pexp_loc})];
args = [_; (_, _, {pexp_desc = Pexp_ident _; pexp_loc})];
} ->
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, pexp_loc))
| _ -> None
Expand Down Expand Up @@ -1116,8 +1122,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
funct = {pexp_desc = Pexp_ident {txt = Lident "->"; loc = opLoc}};
args =
[
(_, lhs);
(_, {pexp_desc = Pexp_extension _; pexp_loc = {loc_ghost = true}});
(_, _, lhs);
( _,
_,
{pexp_desc = Pexp_extension _; pexp_loc = {loc_ghost = true}} );
];
}
when opLoc |> Loc.hasPos ~pos:posBeforeCursor ->
Expand Down Expand Up @@ -1294,8 +1302,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
args =
[
(_, lhs);
(_, {pexp_desc = Pexp_ident {txt = Longident.Lident id; loc}});
(_, _, lhs);
( _,
_,
{pexp_desc = Pexp_ident {txt = Longident.Lident id; loc}} );
];
}
when loc |> Loc.hasPos ~pos:posBeforeCursor ->
Expand All @@ -1304,15 +1314,16 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Lident "->"; loc = opLoc}};
args = [(_, lhs); _];
args = [(_, _, lhs); _];
}
when Loc.end_ opLoc = posCursor ->
if Debug.verbose () then print_endline "[expr_iter] Case foo->";
setPipeResult ~lhs ~id:"" |> ignore
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Lident "->"}};
args = [_; (_, {pexp_desc = Pexp_apply {funct = funExpr; args}})];
args =
[_; (_, _, {pexp_desc = Pexp_apply {funct = funExpr; args}})];
}
when (* Normally named arg completion fires when the cursor is right after the expression.
E.g in foo(~<---there
Expand Down
6 changes: 4 additions & 2 deletions analysis/src/CompletionJsx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -465,20 +465,22 @@ let extractJsxProps ~(compName : Longident.t Location.loc) ~args =
in
let rec processProps ~acc args =
match args with
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
| (Asttypes.Labelled "children", _, {Parsetree.pexp_loc}) :: _ ->
{
compName;
props = List.rev acc;
childrenStart =
(if pexp_loc.loc_ghost then None else Some (Loc.start pexp_loc));
}
| ((Labelled s | Optional s), (eProp : Parsetree.expression)) :: rest -> (
| ((Labelled s | Optional s), lbl_loc, (eProp : Parsetree.expression))
:: rest -> (
let namedArgLoc =
eProp.pexp_attributes
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "res.namedArgLoc")
in
match namedArgLoc with
| Some ({loc}, _) ->
assert (loc = lbl_loc);
processProps
~acc:
({
Expand Down
4 changes: 2 additions & 2 deletions analysis/src/SemanticTokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ let command ~debug ~emitter ~path =

let posOfGreatherthanAfterProps =
let rec loop = function
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
| (Asttypes.Labelled "children", _, {Parsetree.pexp_loc}) :: _ ->
Loc.start pexp_loc
| _ :: args -> loop args
| [] -> (* should not happen *) (-1, -1)
Expand Down Expand Up @@ -297,7 +297,7 @@ let command ~debug ~emitter ~path =
emitter (* <foo> ... </foo> <-- *)
|> emitJsxTag ~debug ~name:">" ~pos:posOfFinalGreatherthan));

args |> List.iter (fun (_lbl, arg) -> iterator.expr iterator arg)
args |> List.iter (fun (_lbl, _, arg) -> iterator.expr iterator arg)
| Pexp_apply
{
funct =
Expand Down
6 changes: 4 additions & 2 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,9 @@ type arg = {label: label; exp: Parsetree.expression}
let extractExpApplyArgs ~args =
let rec processArgs ~acc args =
match args with
| (((Asttypes.Labelled s | Optional s) as label), (e : Parsetree.expression))
| ( ((Asttypes.Labelled s | Optional s) as label),
_,
(e : Parsetree.expression) )
:: rest -> (
let namedArgLoc =
e.pexp_attributes
Expand All @@ -919,7 +921,7 @@ let extractExpApplyArgs ~args =
in
processArgs ~acc:({label = Some labelled; exp = e} :: acc) rest
| None -> processArgs ~acc rest)
| (Asttypes.Nolabel, (e : Parsetree.expression)) :: rest ->
| (Asttypes.Nolabel, _, (e : Parsetree.expression)) :: rest ->
if e.pexp_loc.loc_ghost then processArgs ~acc rest
else processArgs ~acc:({label = None; exp = e} :: acc) rest
| [] -> List.rev acc
Expand Down
1 change: 1 addition & 0 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
[
_;
( _,
_,
{
pexp_desc =
Pexp_apply
Expand Down
6 changes: 5 additions & 1 deletion analysis/src/TypeUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,11 @@ module Codegen = struct
let mkFailWithExp () =
Ast_helper.Exp.apply
(Ast_helper.Exp.ident {txt = Lident "failwith"; loc = Location.none})
[(Nolabel, Ast_helper.Exp.constant (Pconst_string ("TODO", None)))]
[
( Nolabel,
Location.none,
Ast_helper.Exp.constant (Pconst_string ("TODO", None)) );
]

let mkConstructPat ?payload name =
Ast_helper.Pat.construct
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module IfThenElse = struct
Pexp_ident
{txt = Longident.Lident (("==" | "!=") as op)};
};
args = [(Nolabel, arg1); (Nolabel, arg2)];
args = [(Nolabel, _, arg1); (Nolabel, _, arg2)];
};
},
e1,
Expand Down
26 changes: 20 additions & 6 deletions compiler/frontend/ast_compatible.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ let apply_simple ?(loc = default_loc) ?(attrs = []) (fn : expression)
Pexp_apply
{
funct = fn;
args = Ext_list.map args (fun x -> (Asttypes.Nolabel, x));
args =
Ext_list.map args (fun x -> (Asttypes.Nolabel, Location.none, x));
partial = false;
};
}
Expand All @@ -52,7 +53,8 @@ let app1 ?(loc = default_loc) ?(attrs = []) fn arg1 : expression =
pexp_loc = loc;
pexp_attributes = attrs;
pexp_desc =
Pexp_apply {funct = fn; args = [(Nolabel, arg1)]; partial = false};
Pexp_apply
{funct = fn; args = [(Nolabel, Location.none, arg1)]; partial = false};
}

let app2 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 : expression =
Expand All @@ -61,7 +63,12 @@ let app2 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 : expression =
pexp_attributes = attrs;
pexp_desc =
Pexp_apply
{funct = fn; args = [(Nolabel, arg1); (Nolabel, arg2)]; partial = false};
{
funct = fn;
args =
[(Nolabel, Location.none, arg1); (Nolabel, Location.none, arg2)];
partial = false;
};
}

let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
Expand All @@ -72,7 +79,12 @@ let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
Pexp_apply
{
funct = fn;
args = [(Nolabel, arg1); (Nolabel, arg2); (Nolabel, arg3)];
args =
[
(Nolabel, Location.none, arg1);
(Nolabel, Location.none, arg2);
(Nolabel, Location.none, arg3);
];
partial = false;
};
}
Expand Down Expand Up @@ -118,7 +130,9 @@ let apply_labels ?(loc = default_loc) ?(attrs = []) fn
Pexp_apply
{
funct = fn;
args = Ext_list.map args (fun (l, a) -> (Asttypes.Labelled l, a));
args =
Ext_list.map args (fun (l, a) ->
(Asttypes.Labelled l, Location.none, a));
partial = false;
};
}
Expand Down Expand Up @@ -167,4 +181,4 @@ type object_field = Parsetree.object_field

let object_field l attrs ty = Parsetree.Otag (l, attrs, ty)

type args = (Asttypes.arg_label * Parsetree.expression) list
type args = (Asttypes.arg_label * Location.t * Parsetree.expression) list
2 changes: 1 addition & 1 deletion compiler/frontend/ast_compatible.mli
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ type object_field = Parsetree.object_field
val object_field :
Asttypes.label Asttypes.loc -> attributes -> core_type -> object_field

type args = (Asttypes.arg_label * Parsetree.expression) list
type args = (Asttypes.arg_label * Location.t * Parsetree.expression) list
11 changes: 7 additions & 4 deletions compiler/frontend/ast_exp_apply.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let bound (e : exp) (cb : exp -> _) =
let default_expr_mapper = Bs_ast_mapper.default_mapper.expr

let check_and_discard (args : Ast_compatible.args) =
Ext_list.map args (fun (label, x) ->
Ext_list.map args (fun (label, _, x) ->
Bs_syntaxerr.err_if_label x.pexp_loc label;
x)

Expand Down Expand Up @@ -92,7 +92,8 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
Bs_ast_invariant.warn_discarded_unused_attributes fn1.pexp_attributes;
{
pexp_desc =
Pexp_apply {funct = fn1; args = (Nolabel, a) :: args; partial};
Pexp_apply
{funct = fn1; args = (Nolabel, Location.none, a) :: args; partial};
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes @ f.pexp_attributes;
}
Expand All @@ -116,7 +117,9 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
Pexp_apply
{
funct = fn;
args = (Nolabel, bounded_obj_arg) :: args;
args =
(Nolabel, Location.none, bounded_obj_arg)
:: args;
partial = false;
};
pexp_attributes = [];
Expand Down Expand Up @@ -170,7 +173,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
let arg = self.expr self arg in
let fn = Exp.send ~loc obj {txt = name ^ Literals.setter_suffix; loc} in
Exp.constraint_ ~loc
(Exp.apply ~loc fn [(Nolabel, arg)])
(Exp.apply ~loc fn [(Nolabel, Location.none, arg)])
(Ast_literal.type_unit ~loc ())
in
match obj.pexp_desc with
Expand Down
1 change: 1 addition & 0 deletions compiler/frontend/ast_exp_extension.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
(Exp.ident ~loc {txt = Longident.parse "Js.Exn.raiseError"; loc})
[
( Nolabel,
Location.none,
Exp.constant ~loc
(Pconst_string
( (pretext
Expand Down
1 change: 1 addition & 0 deletions compiler/frontend/ast_uncurry_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
args =
[
( Nolabel,
Location.none,
Exp.constraint_ ~loc
(Exp.record ~loc
[
Expand Down
4 changes: 2 additions & 2 deletions compiler/frontend/bs_ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type mapper = {

let id x = x
let map_fst f (x, y) = (f x, y)
let map_snd f (x, y) = (x, f y)
let map_3rd f (x, y, z) = (x, y, f z)
let map_tuple f1 f2 (x, y) = (f1 x, f2 y)
let map_tuple3 f1 f2 f3 (x, y, z) = (f1 x, f2 y, f3 z)
let map_opt f = function
Expand Down Expand Up @@ -327,7 +327,7 @@ module E = struct
(sub.pat sub p) (sub.expr sub e)
| Pexp_apply {funct = e; args = l; partial} ->
apply ~loc ~attrs ~partial (sub.expr sub e)
(List.map (map_snd (sub.expr sub)) l)
(List.map (map_3rd (sub.expr sub)) l)
| Pexp_match (e, pel) ->
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/ast_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let add_promise_type ?(loc = Location.none) ~async
Ast_helper.Exp.ident ~loc
{txt = Ldot (Lident Primitive_modules.promise, "unsafe_async"); loc}
in
Ast_helper.Exp.apply ~loc unsafe_async [(Nolabel, result)]
Ast_helper.Exp.apply ~loc unsafe_async [(Nolabel, Location.none, result)]
else result

let rec add_promise_to_result ~loc (e : Parsetree.expression) =
Expand Down
3 changes: 2 additions & 1 deletion compiler/ml/ast_await.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let create_await_expression (e : Parsetree.expression) =
Ast_helper.Exp.ident ~loc
{txt = Ldot (Lident Primitive_modules.promise, "unsafe_await"); loc}
in
Ast_helper.Exp.apply ~loc unsafe_await [(Nolabel, e)]
Ast_helper.Exp.apply ~loc unsafe_await [(Nolabel, Location.none, e)]

(* Transform `@res.await M` to unpack(@res.await Js.import(module(M: __M0__))) *)
let create_await_module_expression ~module_type_lid (e : Parsetree.module_expr)
Expand All @@ -30,6 +30,7 @@ let create_await_module_expression ~module_type_lid (e : Parsetree.module_expr)
})
[
( Nolabel,
Location.none,
Exp.constraint_ ~loc:e.pmod_loc
(Exp.pack ~loc:e.pmod_loc
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/ast_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module Exp : sig
?attrs:attrs ->
?partial:bool ->
expression ->
(arg_label * expression) list ->
(arg_label * Location.t * expression) list ->
expression
val match_ : ?loc:loc -> ?attrs:attrs -> expression -> case list -> expression
val try_ : ?loc:loc -> ?attrs:attrs -> expression -> case list -> expression
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/ast_iterator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type iterator = {
tree. *)

let iter_fst f (x, _) = f x
let iter_snd f (_, y) = f y
let iter_snd f (_, _, y) = f y
let iter_tuple f1 f2 (x, y) =
f1 x;
f2 y
Expand Down
Loading