Skip to content
Open
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
133 changes: 127 additions & 6 deletions compiler/backend/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ let pp_var_declare cxt f id =
P.string f L.let_;
P.space f;
let acxt = Ext_pp_scope.ident cxt f id in
(if Js_ts_annot.enabled () then
match Js_ts_annot.local_type id with
| Some ty ->
P.string f ":";
P.space f;
P.string f ty
| None -> ());
semi f;
acxt

Expand Down Expand Up @@ -328,6 +335,21 @@ and record_rest_pattern cxt f fields rest =

and formal_parameter_list cxt f l = iter_lst cxt f l Ext_pp_scope.ident comma_sp

(* [-bs-emit-ts]: each parameter followed by its type, where the typed tree
gave one to this identifier. *)
and annotated_parameter_list cxt f l =
iter_lst cxt f l
(fun cxt f id ->
let cxt = Ext_pp_scope.ident cxt f id in
(match Js_ts_annot.param_type id with
| Some {ty; optional} ->
P.string f (if optional then "?:" else ":");
P.space f;
P.string f ty
| None -> ());
cxt)
comma_sp

(* IdentMap *)
(*
f/122 -->
Expand Down Expand Up @@ -455,12 +477,31 @@ let rec pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t)
in
function_body ~return_unit cxt f b))
else
let ts_top =
match fn_state with
| Name_top x when Js_ts_annot.enabled () ->
Js_ts_annot.report_function x l;
Some x
| _ -> None
in
let cxt =
match l with
| [single] when arrow -> Ext_pp_scope.ident inner_cxt f single
| l ->
match (l, ts_top) with
| l, Some x ->
P.string f (Js_ts_annot.type_params x);
P.paren_group f 1 (fun _ -> annotated_parameter_list inner_cxt f l)
| [single], None when arrow -> Ext_pp_scope.ident inner_cxt f single
| l, None ->
P.paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f l)
in
(match ts_top with
| Some x -> (
match Js_ts_annot.result_type x with
| Some ty ->
P.string f ":";
P.space f;
P.string f ty
| None -> ())
| None -> ());
P.space f;
if arrow then (
P.string f L.arrow;
Expand Down Expand Up @@ -494,6 +535,43 @@ let rec pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t)
param_body ();
semi f
| Name_top x ->
let printed, _ = Ext_pp_scope.str_of_ident inner_cxt x in
let export = Js_ts_annot.export_inline x ~printed in
(* [-bs-emit-ts]: the function's overloads, each a declaration
without a body before the function, which carries its own
signature. An overload takes no [async]. *)
if Js_ts_annot.enabled () then
List.iter
(fun (typed, result) ->
if export then (
P.string f L.export;
P.space f);
P.string f (L.function_ ~async:false ~arrow:false);
ignore (Ext_pp_scope.ident inner_cxt f x : cxt);
P.string f (Js_ts_annot.type_params x);
ignore
(P.paren_group f 1 (fun _ ->
iter_lst inner_cxt f l
(fun cxt f id ->
let cxt = Ext_pp_scope.ident cxt f id in
(match Js_ts_annot.overload_param_type typed id with
| Some ty ->
P.string f ":";
P.space f;
P.string f ty
| None -> ());
cxt)
comma_sp)
: cxt);
P.string f ":";
P.space f;
P.string f result;
semi f;
P.newline f)
(Js_ts_annot.overloads x);
if export then (
P.string f L.export;
P.space f);
P.string f (L.function_ ~async ~arrow);
ignore (Ext_pp_scope.ident inner_cxt f x : cxt);
param_body ()
Expand Down Expand Up @@ -1178,7 +1256,7 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
match variable with
| {ident = i; value = None; ident_info; _} ->
if ident_info.used_stats = Dead_pure then cxt else pp_var_declare cxt f i
| {ident = name; value = Some e; ident_info = {used_stats; _}} -> (
| {ident = name; value = Some e; property; ident_info = {used_stats; _}} -> (
match used_stats with
| Dead_pure -> cxt
| Dead_non_pure ->
Expand All @@ -1203,7 +1281,44 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
semi f;
cxt
| _ ->
let cxt = pp_var_assign cxt f name in
let cxt =
if top && Js_ts_annot.enabled () then (
let is_const = property <> Variable in
let printed, _ = Ext_pp_scope.str_of_ident cxt name in
if is_const && Js_ts_annot.export_inline name ~printed then (
P.string f L.export;
P.space f);
P.string f (if is_const then L.const_ else L.let_);
P.space f;
let cxt = Ext_pp_scope.ident cxt f name in
(match Js_ts_annot.value_type name with
| Some ty ->
P.string f ":";
P.space f;
P.string f ty
| None -> ());
P.space f;
P.string f L.eq;
P.space f;
cxt)
else if Js_ts_annot.enabled () then
match
(property, e.expression_desc, Js_ts_annot.local_type name)
with
| Variable, _, Some ty | _, Undefined _, Some ty ->
P.string f L.let_;
P.space f;
let cxt = Ext_pp_scope.ident cxt f name in
P.string f ":";
P.space f;
P.string f ty;
P.space f;
P.string f L.eq;
P.space f;
cxt
| _ -> pp_var_assign cxt f name
else pp_var_assign cxt f name
in
let cxt = expression ~level:1 cxt f e in
semi f;
cxt))
Expand Down Expand Up @@ -1604,7 +1719,13 @@ and function_body ?directive (cxt : cxt) f ~return_unit (b : J.block) : unit =
ignore
(statement false cxt f {s with statement_desc = If (bool, then_, [])}
: cxt)
| Return {expression_desc = Undefined _} -> ()
(* A function whose body is only [return;] prints an empty body, except
under [-bs-emit-ts]: TypeScript requires a [return] in a function
whose declared result type is other than [undefined], [void] or
[any]. *)
| Return {expression_desc = Undefined _} when not (Js_ts_annot.enabled ())
->
()
| Return exp when return_unit ->
ignore (statement false cxt f (S.exp exp) : cxt)
| _ -> ignore (statement false cxt f s : cxt))
Expand Down
4 changes: 4 additions & 0 deletions compiler/backend/js_dump_import_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ let exports cxt f (idents : Ident.t list) =
(** Print module in ESModule format, it is ESModule, trailing comma is valid ES code *)
let esmodule_export cxt f (idents : Ident.t list) =
P.at_least_two_lines f;
(* [-bs-emit-ts]: functions printed as [export function] leave the list *)
let idents =
List.filter (fun id -> not (Js_ts_annot.exported_inline id)) idents
in
match idents with
| [] -> cxt
| _ ->
Expand Down
2 changes: 2 additions & 0 deletions compiler/backend/js_dump_lit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ let arrow = "=>"

let let_ = "let"

let const_ = "const"

let return = "return"

(* let eq = "=" *)
Expand Down
8 changes: 8 additions & 0 deletions compiler/backend/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ let esmodule_program ~output_dir fmt f (x : J.deps_program) =
x.modules)
in
let () = P.at_least_two_lines f in
Js_ts_annot.set_exports x.program.exports;
(if Js_ts_annot.enabled () then
match Js_ts_annot.header () with
| "" -> ()
| header ->
P.string f header;
P.newline f;
P.at_least_two_lines f);
let cxt = Js_dump.statements true cxt f x.program.block in
Js_dump_import_export.esmodule_export cxt f x.program.exports

Expand Down
2 changes: 1 addition & 1 deletion compiler/backend/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let declare_variable ~kind (ident : Ident.t) : t =

let define_variable ~kind (v : Ident.t) (exp : J.expression) : t =
match exp.expression_desc with
| Undefined _ -> declare_variable ~kind v
| Undefined _ when not (Js_ts_annot.enabled ()) -> declare_variable ~kind v
| _ ->
let property : J.property = kind in
let ident_info : J.ident_info = {used_stats = NA} in
Expand Down
Loading
Loading