Skip to content

Commit 195e155

Browse files
committed
small cleanup refactor
1 parent 4982266 commit 195e155

File tree

10 files changed

+44
-27
lines changed

10 files changed

+44
-27
lines changed

compiler/syntax/src/res_parens.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,7 @@ let ternary_operand expr =
301301
Nothing
302302
| {pexp_desc = Pexp_constraint _} -> Parenthesized
303303
| _ when Res_parsetree_viewer.is_fun_newtype expr -> (
304-
let _attrsOnArrow, _parameters, return_expr =
305-
ParsetreeViewer.fun_expr expr
306-
in
304+
let _parameters, return_expr = ParsetreeViewer.fun_expr expr in
307305
match return_expr.pexp_desc with
308306
| Pexp_constraint _ -> Parenthesized
309307
| _ -> Nothing)

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ let has_partial_attribute attrs =
8686
| _ -> false)
8787
attrs
8888

89+
type function_attributes_info = {async: bool; attributes: Parsetree.attributes}
90+
91+
let process_function_attributes attrs =
92+
let rec process async acc attrs =
93+
match attrs with
94+
| [] -> {async; attributes = List.rev acc}
95+
| ({Location.txt = "res.async"}, _) :: rest -> process true acc rest
96+
| attr :: rest -> process async (attr :: acc) rest
97+
in
98+
process false [] attrs
99+
89100
let has_await_attribute attrs =
90101
List.exists
91102
(function
@@ -174,7 +185,7 @@ let fun_expr expr =
174185
collect_new_types (string_loc :: acc) return_expr
175186
| return_expr -> (List.rev acc, return_expr)
176187
in
177-
let rec collect ~n_fun attrs_before acc expr =
188+
let rec collect ~n_fun ~params expr =
178189
match expr with
179190
| {
180191
pexp_desc =
@@ -186,12 +197,11 @@ let fun_expr expr =
186197
rhs = {pexp_desc = Pexp_apply _};
187198
};
188199
} ->
189-
(attrs_before, List.rev acc, rewrite_underscore_apply expr)
190-
| {pexp_desc = Pexp_newtype (string_loc, rest); pexp_attributes = attrs}
191-
when n_fun = 0 ->
200+
(List.rev params, rewrite_underscore_apply expr)
201+
| {pexp_desc = Pexp_newtype (string_loc, rest); pexp_attributes = attrs} ->
192202
let string_locs, return_expr = collect_new_types [string_loc] rest in
193203
let param = NewTypes {attrs; locs = string_locs} in
194-
collect ~n_fun attrs_before (param :: acc) return_expr
204+
collect ~n_fun ~params:(param :: params) return_expr
195205
| {
196206
pexp_desc =
197207
Pexp_fun
@@ -208,19 +218,17 @@ let fun_expr expr =
208218
let parameter =
209219
Parameter {attrs = []; lbl; default_expr; pat = pattern}
210220
in
211-
collect ~n_fun:(n_fun + 1) attrs_before (parameter :: acc) return_expr
221+
collect ~n_fun:(n_fun + 1) ~params:(parameter :: params) return_expr
212222
(* If a fun has an attribute, then it stops here and makes currying.
213223
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
214-
| {pexp_desc = Pexp_fun _} -> (attrs_before, List.rev acc, expr)
224+
| {pexp_desc = Pexp_fun _} -> (List.rev params, expr)
215225
| expr when n_fun = 0 && Ast_uncurried.expr_is_uncurried_fun expr ->
216226
let expr = Ast_uncurried.expr_extract_uncurried_fun expr in
217-
collect ~n_fun attrs_before acc expr
218-
| expr -> (attrs_before, List.rev acc, expr)
227+
collect ~n_fun ~params expr
228+
| expr -> (List.rev params, expr)
219229
in
220230
match expr with
221-
| {pexp_desc = Pexp_fun _ | Pexp_newtype _} ->
222-
collect ~n_fun:0 expr.pexp_attributes [] {expr with pexp_attributes = []}
223-
| _ -> collect ~n_fun:0 [] [] expr
231+
| _ -> collect ~n_fun:0 ~params:[] {expr with pexp_attributes = []}
224232

225233
let process_braces_attr expr =
226234
match expr.pexp_attributes with

compiler/syntax/src/res_parsetree_viewer.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ type fun_param_kind =
5353
| NewTypes of {attrs: Parsetree.attributes; locs: string Asttypes.loc list}
5454

5555
val fun_expr :
56-
Parsetree.expression ->
57-
Parsetree.attributes * fun_param_kind list * Parsetree.expression
56+
Parsetree.expression -> fun_param_kind list * Parsetree.expression
5857

5958
(* example:
6059
* `makeCoordinate({

compiler/syntax/src/res_printer.ml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
19801980
};
19811981
pvb_expr = {pexp_desc = Pexp_newtype _} as expr;
19821982
} -> (
1983-
let _attrs, parameters, return_expr = ParsetreeViewer.fun_expr expr in
1983+
let parameters, return_expr = ParsetreeViewer.fun_expr expr in
19841984
let abstract_type =
19851985
match parameters with
19861986
| [NewTypes {locs = vars}] ->
@@ -2695,8 +2695,11 @@ and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl =
26952695

26962696
and print_expression ~state (e : Parsetree.expression) cmt_tbl =
26972697
let print_arrow e =
2698-
let attrs_on_arrow, parameters, return_expr = ParsetreeViewer.fun_expr e in
2699-
let async, attrs = Ast_async.extract_async_attribute attrs_on_arrow in
2698+
let parameters, return_expr = ParsetreeViewer.fun_expr e in
2699+
let attrs_on_arrow = e.pexp_attributes in
2700+
let ParsetreeViewer.{async; attributes = attrs} =
2701+
ParsetreeViewer.process_function_attributes attrs_on_arrow
2702+
in
27002703
let return_expr, typ_constraint =
27012704
match return_expr.pexp_desc with
27022705
| Pexp_constraint (expr, typ) ->
@@ -3436,8 +3439,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34363439
| _ -> expr_with_await
34373440

34383441
and print_pexp_fun ~state ~in_callback e cmt_tbl =
3439-
let attrs_on_arrow, parameters, return_expr = ParsetreeViewer.fun_expr e in
3440-
let async, attrs = Ast_async.extract_async_attribute attrs_on_arrow in
3442+
let parameters, return_expr = ParsetreeViewer.fun_expr e in
3443+
let attrs_on_arrow = e.pexp_attributes in
3444+
let ParsetreeViewer.{async; attributes = attrs} =
3445+
ParsetreeViewer.process_function_attributes attrs_on_arrow
3446+
in
34413447
let return_expr, typ_constraint =
34423448
match return_expr.pexp_desc with
34433449
| Pexp_constraint (expr, typ) ->

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

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

tests/analysis_tests/tests-incremental-typechecking/package-lock.json

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

tests/analysis_tests/tests-reanalyze/deadcode/package-lock.json

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

tests/analysis_tests/tests-reanalyze/termination/package-lock.json

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

tests/analysis_tests/tests/package-lock.json

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

tests/tools_tests/package-lock.json

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

0 commit comments

Comments
 (0)