Skip to content

Commit 19a71b9

Browse files
authored
Improve formatting of the generated JS code (rescript-lang#6932)
* Fix js dump * Commit compiled test files * Update change log * Fix space in front comma for for in loop * Commit test output * Return spaces around () in for loop * Commit tests output * Add space around brackets in while loop * Commit test outputs * Reduce tabulation of switch case items * Commit tests output * Remove empty default from generated Js code * Commit test outputs * Remove tabulation from throw new Error generated code * Commit test output * Update changelog * Revert formatting changes in changelog file * Commit output of genType tests
1 parent 0b1443f commit 19a71b9

File tree

389 files changed

+24798
-25195
lines changed

Some content is hidden

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

389 files changed

+24798
-25195
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212
1313
# 12.0.0-alpha.2 (Unreleased)
1414

15+
#### :nail_care: Polish
16+
17+
- Improve formatting in the generated js code. https://github.com/rescript-lang/rescript-compiler/pull/6932
18+
- `}\ncatch{` -> `} catch {`
19+
- `for(let i = 0 ,i_finish = r.length; i < i_finish; ++i){` -> `for (let i = 0, i_finish = r.length; i < i_finish; ++i) {`
20+
- `while(true) {` -> `while (true) {`
21+
- Fixed tabulation for `switch case` bodies
22+
- Fixed tabulation for `throw new Error` bodies
23+
- Removed empty line at the end of `switch` statement
24+
- Removed empty `default` case from `switch` statement in the generated code
25+
1526
# 12.0.0-alpha.1
1627

1728
#### :rocket: New Feature

jscomp/core/js_dump.ml

+17-12
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ let debugger_nl f =
248248

249249
let break_nl f =
250250
P.string f L.break;
251-
P.space f;
252251
semi f;
253252
P.newline f
254253

@@ -430,17 +429,18 @@ and pp_one_case_clause :
430429
'a. _ -> P.t -> (P.t -> 'a -> unit) -> 'a * J.case_clause -> _ =
431430
fun cxt f pp_cond
432431
(switch_case, ({ switch_body; should_break; comment } : J.case_clause)) ->
432+
P.newline f;
433433
let cxt =
434434
P.group f 1 (fun _ ->
435-
P.group f 1 (fun _ ->
435+
P.group f 0 (fun _ ->
436436
P.string f L.case;
437437
P.space f;
438438
pp_comment_option f comment;
439439
pp_cond f switch_case;
440440
(* could be integer or string *)
441441
P.space f;
442442
P.string f L.colon);
443-
P.group f 1 (fun _ ->
443+
P.group f 0 (fun _ ->
444444
let cxt =
445445
match switch_body with
446446
| [] -> cxt
@@ -454,7 +454,6 @@ and pp_one_case_clause :
454454
semi f);
455455
cxt))
456456
in
457-
P.newline f;
458457
cxt
459458

460459
and loop_case_clauses :
@@ -858,11 +857,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
858857
cxt)
859858
| New (e, el) ->
860859
P.cond_paren_group f (level > 15) (fun _ ->
861-
P.group f 1 (fun _ ->
860+
P.group f 0 (fun _ ->
862861
P.string f L.new_;
863862
P.space f;
864863
let cxt = expression ~level:16 cxt f e in
865-
P.paren_group f 1 (fun _ ->
864+
P.paren_group f 0 (fun _ ->
866865
match el with Some el -> arguments cxt f el | None -> cxt)))
867866
| Cond (e, e1, e2) ->
868867
let action () =
@@ -1045,13 +1044,15 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10451044
match e.expression_desc with
10461045
| Number (Int { i = 1l }) ->
10471046
P.string f L.while_;
1047+
P.space f;
10481048
P.string f L.lparen;
10491049
P.string f L.true_;
10501050
P.string f L.rparen;
10511051
P.space f;
10521052
cxt
10531053
| _ ->
10541054
P.string f L.while_;
1055+
P.space f;
10551056
let cxt =
10561057
P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e)
10571058
in
@@ -1068,7 +1069,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10681069
P.group f 0 (fun _ ->
10691070
(* The only place that [semi] may have semantics here *)
10701071
P.string f L.for_;
1071-
P.paren_group f 1 (fun _ ->
1072+
P.space f;
1073+
let ctx = P.paren_group f 1 (fun _ ->
10721074
let cxt, new_id =
10731075
match
10741076
(for_ident_expression, finish.expression_desc)
@@ -1081,8 +1083,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10811083
let cxt =
10821084
expression ~level:1 cxt f ident_expression
10831085
in
1084-
P.space f;
10851086
comma f;
1087+
P.space f;
10861088
let id =
10871089
Ext_ident.create (Ident.name id ^ "_finish")
10881090
in
@@ -1128,7 +1130,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11281130
semi f;
11291131
P.space f;
11301132
pp_direction f direction;
1131-
Ext_pp_scope.ident cxt f id))
1133+
Ext_pp_scope.ident cxt f id) in
1134+
P.space f;
1135+
ctx)
11321136
in
11331137
brace_block cxt f s)
11341138
in
@@ -1177,6 +1181,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11771181
match def with
11781182
| None -> cxt
11791183
| Some def ->
1184+
P.newline f;
11801185
P.group f 1 (fun _ ->
11811186
P.string f L.default;
11821187
P.string f L.colon;
@@ -1195,6 +1200,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11951200
match def with
11961201
| None -> cxt
11971202
| Some def ->
1203+
P.newline f;
11981204
P.group f 1 (fun _ ->
11991205
P.string f L.default;
12001206
P.string f L.colon;
@@ -1224,10 +1230,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
12241230
match ctch with
12251231
| None -> cxt
12261232
| Some (i, b) ->
1227-
P.newline f;
1228-
P.string f "catch (";
1233+
P.string f " catch (";
12291234
let cxt = Ext_pp_scope.ident cxt f i in
1230-
P.string f ")";
1235+
P.string f ") ";
12311236
brace_block cxt f b
12321237
in
12331238
match fin with

jscomp/core/lam_compile.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,10 @@ and compile_general_cases :
584584
| Complete -> None
585585
| NonComplete -> None
586586
| Default lam ->
587-
Some (Js_output.output_as_block (compile_lambda cxt lam))
587+
let statements = Js_output.output_as_block (compile_lambda cxt lam) in
588+
match statements with
589+
| [] -> None
590+
| _ -> Some statements
588591
in
589592
let body =
590593
group_apply ~merge_cases cases (fun last (switch_case, lam) ->

jscomp/gentype_tests/typescript-react-example/src/NestedVariants.res.js

+15-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/gentype_tests/typescript-react-example/src/VariantsWithPayload.res.js

+9-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/runtime/release.ninja

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ o runtime/caml_exceptions.cmj : cc_cmi runtime/caml_exceptions.res | runtime/cam
2525
o runtime/caml_exceptions.cmi : cc runtime/caml_exceptions.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2626
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj
2727
o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
28-
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.res | runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
28+
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.res | runtime/caml.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
2929
o runtime/caml_format.cmi : cc runtime/caml_format.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
3030
o runtime/caml_hash.cmj : cc_cmi runtime/caml_hash.res | runtime/caml_hash.cmi runtime/caml_hash_primitive.cmj runtime/caml_nativeint_extern.cmj
3131
o runtime/caml_hash.cmi : cc runtime/caml_hash.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
@@ -41,7 +41,7 @@ o runtime/caml_md5.cmj : cc_cmi runtime/caml_md5.res | runtime/caml_array_extern
4141
o runtime/caml_md5.cmi : cc runtime/caml_md5.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
4242
o runtime/caml_module.cmj : cc_cmi runtime/caml_module.res | runtime/caml_array_extern.cmj runtime/caml_module.cmi runtime/caml_obj.cmj
4343
o runtime/caml_module.cmi : cc runtime/caml_module.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
44-
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj
44+
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml.cmj runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj
4545
o runtime/caml_obj.cmi : cc runtime/caml_obj.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
4646
o runtime/caml_option.cmj : cc_cmi runtime/caml_option.res | runtime/caml_option.cmi runtime/caml_undefined_extern.cmj
4747
o runtime/caml_option.cmi : cc runtime/caml_option.resi | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj
@@ -57,7 +57,7 @@ o runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj : cc runtime/caml_
5757
o runtime/caml_bigint_extern.cmi runtime/caml_bigint_extern.cmj : cc runtime/caml_bigint_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5858
o runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5959
o runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj : cc runtime/caml_int64_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
60-
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/js.cmi runtime/js.cmj
60+
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/caml_option.cmj runtime/js.cmi runtime/js.cmj
6161
o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runtime/caml_nativeint_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6262
o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6363
o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj

jscomp/test/406_primitive_test.js

+9-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/UncurriedExternals.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)