Skip to content

Improve formatting of the generated JS code #6932

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

Merged
merged 18 commits into from
Aug 4, 2024
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@

# 12.0.0-alpha.2 (Unreleased)

#### :nail_care: Polish

- Improve formatting in the generated js code. https://github.com/rescript-lang/rescript-compiler/pull/6932
- `}\ncatch{` -> `} catch {`
- `for(let i = 0 ,i_finish = r.length; i < i_finish; ++i){` -> `for (let i = 0, i_finish = r.length; i < i_finish; ++i) {`
- `while(true) {` -> `while (true) {`
- Fixed tabulation for `switch case` bodies
- Fixed tabulation for `throw new Error` bodies
- Removed empty line at the end of `switch` statement
- Removed empty `default` case from `switch` statement in the generated code

# 12.0.0-alpha.1

#### :rocket: New Feature
Expand Down
29 changes: 17 additions & 12 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ let debugger_nl f =

let break_nl f =
P.string f L.break;
P.space f;
semi f;
P.newline f

Expand Down Expand Up @@ -430,17 +429,18 @@ and pp_one_case_clause :
'a. _ -> P.t -> (P.t -> 'a -> unit) -> 'a * J.case_clause -> _ =
fun cxt f pp_cond
(switch_case, ({ switch_body; should_break; comment } : J.case_clause)) ->
P.newline f;
let cxt =
P.group f 1 (fun _ ->
P.group f 1 (fun _ ->
P.group f 0 (fun _ ->
P.string f L.case;
P.space f;
pp_comment_option f comment;
pp_cond f switch_case;
(* could be integer or string *)
P.space f;
P.string f L.colon);
P.group f 1 (fun _ ->
P.group f 0 (fun _ ->
let cxt =
match switch_body with
| [] -> cxt
Expand All @@ -454,7 +454,6 @@ and pp_one_case_clause :
semi f);
cxt))
in
P.newline f;
cxt

and loop_case_clauses :
Expand Down Expand Up @@ -858,11 +857,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
cxt)
| New (e, el) ->
P.cond_paren_group f (level > 15) (fun _ ->
P.group f 1 (fun _ ->
P.group f 0 (fun _ ->
P.string f L.new_;
P.space f;
let cxt = expression ~level:16 cxt f e in
P.paren_group f 1 (fun _ ->
P.paren_group f 0 (fun _ ->
match el with Some el -> arguments cxt f el | None -> cxt)))
| Cond (e, e1, e2) ->
let action () =
Expand Down Expand Up @@ -1045,13 +1044,15 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
match e.expression_desc with
| Number (Int { i = 1l }) ->
P.string f L.while_;
P.space f;
P.string f L.lparen;
P.string f L.true_;
P.string f L.rparen;
P.space f;
cxt
| _ ->
P.string f L.while_;
P.space f;
let cxt =
P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e)
in
Expand All @@ -1068,7 +1069,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
P.group f 0 (fun _ ->
(* The only place that [semi] may have semantics here *)
P.string f L.for_;
P.paren_group f 1 (fun _ ->
P.space f;
let ctx = P.paren_group f 1 (fun _ ->
let cxt, new_id =
match
(for_ident_expression, finish.expression_desc)
Expand All @@ -1081,8 +1083,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
let cxt =
expression ~level:1 cxt f ident_expression
in
P.space f;
comma f;
P.space f;
let id =
Ext_ident.create (Ident.name id ^ "_finish")
in
Expand Down Expand Up @@ -1128,7 +1130,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
semi f;
P.space f;
pp_direction f direction;
Ext_pp_scope.ident cxt f id))
Ext_pp_scope.ident cxt f id) in
P.space f;
ctx)
in
brace_block cxt f s)
in
Expand Down Expand Up @@ -1177,6 +1181,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
match def with
| None -> cxt
| Some def ->
P.newline f;
P.group f 1 (fun _ ->
P.string f L.default;
P.string f L.colon;
Expand All @@ -1195,6 +1200,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
match def with
| None -> cxt
| Some def ->
P.newline f;
P.group f 1 (fun _ ->
P.string f L.default;
P.string f L.colon;
Expand Down Expand Up @@ -1224,10 +1230,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
match ctch with
| None -> cxt
| Some (i, b) ->
P.newline f;
P.string f "catch (";
P.string f " catch (";
let cxt = Ext_pp_scope.ident cxt f i in
P.string f ")";
P.string f ") ";
brace_block cxt f b
in
match fin with
Expand Down
5 changes: 4 additions & 1 deletion jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,10 @@ and compile_general_cases :
| Complete -> None
| NonComplete -> None
| Default lam ->
Some (Js_output.output_as_block (compile_lambda cxt lam))
let statements = Js_output.output_as_block (compile_lambda cxt lam) in
match statements with
| [] -> None
| _ -> Some statements
in
let body =
group_apply ~merge_cases cases (fun last (switch_case, lam) ->
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions jscomp/runtime/release.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ o runtime/caml_exceptions.cmj : cc_cmi runtime/caml_exceptions.res | runtime/cam
o runtime/caml_exceptions.cmi : cc runtime/caml_exceptions.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj
o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
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
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
o runtime/caml_format.cmi : cc runtime/caml_format.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
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
o runtime/caml_hash.cmi : cc runtime/caml_hash.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
Expand All @@ -41,7 +41,7 @@ o runtime/caml_md5.cmj : cc_cmi runtime/caml_md5.res | runtime/caml_array_extern
o runtime/caml_md5.cmi : cc runtime/caml_md5.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_module.cmj : cc_cmi runtime/caml_module.res | runtime/caml_array_extern.cmj runtime/caml_module.cmi runtime/caml_obj.cmj
o runtime/caml_module.cmi : cc runtime/caml_module.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj
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
o runtime/caml_obj.cmi : cc runtime/caml_obj.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_option.cmj : cc_cmi runtime/caml_option.res | runtime/caml_option.cmi runtime/caml_undefined_extern.cmj
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
Expand All @@ -57,7 +57,7 @@ o runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj : cc runtime/caml_
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
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
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
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
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
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
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
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
Expand Down
19 changes: 9 additions & 10 deletions jscomp/test/406_primitive_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions jscomp/test/UncurriedExternals.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading