Skip to content

Commit

Permalink
improve indentation of parentheses blocks in generated JS code (#1293)
Browse files Browse the repository at this point in the history
* improve indentation of most blocks in generated JS code

* snapshot runtime tests

* fix: snapshot tests
  • Loading branch information
anmonteiro authored Jan 13, 2025
1 parent f2ada94 commit 6d4280c
Show file tree
Hide file tree
Showing 303 changed files with 49,712 additions and 49,712 deletions.
26 changes: 13 additions & 13 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ and pp_function ~return_unit ~is_method cxt ~fn_state (l : Ident.t list)
param_body ()
| No_name { single_arg } ->
(* see # 1692, add a paren for annoymous function for safety *)
cond_paren_group cxt (not single_arg) 1 (fun () ->
cond_paren_group cxt (not single_arg) (fun () ->
string cxt L.function_;
space cxt;
param_body ())
Expand Down Expand Up @@ -552,7 +552,7 @@ and expression_desc cxt ~(level : int) x : cxt =
bool cxt b;
cxt
| Seq (e1, e2) ->
cond_paren_group cxt (level > 0) 1 (fun () ->
cond_paren_group cxt (level > 0) (fun () ->
let cxt = expression ~level:0 cxt e1 in
comma_sp cxt;
expression ~level:0 cxt e2)
Expand All @@ -570,7 +570,7 @@ and expression_desc cxt ~(level : int) x : cxt =
]}
*)
| Call { expr = e; args = el; info } ->
cond_paren_group cxt (level > 15) 1 (fun () ->
cond_paren_group cxt (level > 15) (fun () ->
group cxt 1 (fun () ->
match (info, el) with
| { arity = Full; _ }, _ | _, [] ->
Expand Down Expand Up @@ -697,15 +697,15 @@ and expression_desc cxt ~(level : int) x : cxt =
if need_paren then paren cxt action else action ();
cxt
| Is_null_or_undefined e ->
cond_paren_group cxt (level > 0) 1 (fun () ->
cond_paren_group cxt (level > 0) (fun () ->
let cxt = expression ~level:1 cxt e in
space cxt;
string cxt "==";
space cxt;
string cxt L.null;
cxt)
| Js_not e ->
cond_paren_group cxt (level > 13) 1 (fun () ->
cond_paren_group cxt (level > 13) (fun () ->
string cxt "!";
expression ~level:13 cxt e)
| Typeof e ->
Expand All @@ -730,7 +730,7 @@ and expression_desc cxt ~(level : int) x : cxt =
{[ 0.000 - x ]}
*)
->
cond_paren_group cxt (level > 13) 1 (fun () ->
cond_paren_group cxt (level > 13) (fun () ->
string cxt (match desc with Float _ -> "- " | _ -> "-");
expression ~level:13 cxt e)
| Bin { op; expr1 = e1; expr2 = e2 } ->
Expand All @@ -740,7 +740,7 @@ and expression_desc cxt ~(level : int) x : cxt =
in
(* We are more conservative here, to make the generated code more readable
to the user *)
cond_paren_group cxt need_paren 1 (fun () ->
cond_paren_group cxt need_paren (fun () ->
let cxt = expression ~level:lft cxt e1 in
space cxt;
string cxt (Js_op.op_str op);
Expand All @@ -752,7 +752,7 @@ and expression_desc cxt ~(level : int) x : cxt =
let need_paren =
level > out || match op with Lsl | Lsr | Asr -> true | _ -> false
in
cond_paren_group cxt need_paren 1 (fun () ->
cond_paren_group cxt need_paren (fun () ->
let cxt = expression ~level:lft cxt e1 in
space cxt;
string cxt "+";
Expand Down Expand Up @@ -857,12 +857,12 @@ and expression_desc cxt ~(level : int) x : cxt =
cxt)
| Array_index { expr = e; index = p } | String_index { expr = e; index = p }
->
cond_paren_group cxt (level > 15) 1 (fun () ->
cond_paren_group cxt (level > 15) (fun () ->
group cxt 1 (fun () ->
let cxt = expression ~level:15 cxt e in
bracket_group cxt 1 (fun () -> expression ~level:0 cxt p)))
| Static_index { expr = e; field = s; _ } ->
cond_paren_group cxt (level > 15) 1 (fun () ->
cond_paren_group cxt (level > 15) (fun () ->
let cxt = expression ~level:15 cxt e in
Js_dump_property.property_access cxt.pp s;
(* See [ .obj_of_exports]
Expand All @@ -872,13 +872,13 @@ and expression_desc cxt ~(level : int) x : cxt =
cxt)
| Length { expr = e; _ } ->
(* Todo: check parens *)
cond_paren_group cxt (level > 15) 1 (fun () ->
cond_paren_group cxt (level > 15) (fun () ->
let cxt = expression ~level:15 cxt e in
string cxt L.dot;
string cxt L.length;
cxt)
| New { expr = e; args = el } ->
cond_paren_group cxt (level > 15) 1 (fun () ->
cond_paren_group cxt (level > 15) (fun () ->
group cxt 1 (fun () ->
string cxt L.new_;
space cxt;
Expand Down Expand Up @@ -910,7 +910,7 @@ and expression_desc cxt ~(level : int) x : cxt =
var f = { x : 2 , y : 2}
]}
*)
cond_paren_group cxt (level > 1) 0 (fun () ->
cond_paren_group cxt (level > 1) (fun () ->
if lst = [] then (
string cxt "{}";
cxt)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/js_pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ let paren_vgroup st n action =

let paren_group st n action = group st n (fun _ -> paren st action)

let cond_paren_group st b n action =
if b then paren_group st n action else action ()
let cond_paren_group st b action =
if b then paren_group st 0 action else action ()

let brace_group st n action = group st n (fun _ -> brace st action)

Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_pp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ val vgroup : t -> int -> (unit -> 'a) -> 'a
val paren : t -> (unit -> 'a) -> 'a
val brace : t -> (unit -> 'a) -> 'a
val paren_group : t -> int -> (unit -> 'a) -> 'a
val cond_paren_group : t -> bool -> int -> (unit -> 'a) -> 'a
val cond_paren_group : t -> bool -> (unit -> 'a) -> 'a
val paren_vgroup : t -> int -> (unit -> 'a) -> 'a
val brace_group : t -> int -> (unit -> 'a) -> 'a
val brace_vgroup : t -> int -> (unit -> 'a) -> 'a
Expand Down
36 changes: 18 additions & 18 deletions jscomp/test/dist-es6/jscomp/test/es6_tests/es6_module_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ Mt.from_pair_suites("Es6_module_test", {
hd: [
"list_length",
(function (param) {
return {
TAG: /* Eq */0,
_0: Stdlib__List.length({
hd: 1,
tl: {
hd: 2,
tl: /* [] */0
}
}),
_1: 2
};
})
return {
TAG: /* Eq */0,
_0: Stdlib__List.length({
hd: 1,
tl: {
hd: 2,
tl: /* [] */0
}
}),
_1: 2
};
})
],
tl: {
hd: [
"length",
(function (param) {
return {
TAG: /* Eq */0,
_0: 3,
_1: 3
};
})
return {
TAG: /* Eq */0,
_0: 3,
_1: 3
};
})
],
tl: /* [] */0
}
Expand Down
Loading

0 comments on commit 6d4280c

Please sign in to comment.