File tree

15 files changed

+231
-75
lines changed

15 files changed

+231
-75
lines changed
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,9 @@ let declaration_infos
677677
match elt_result with
678678
| Error _ -> (var_infos, member_infos, type_infos)
679679
| Ok elt ->
680-
let type_ = Ty_printer.string_of_elt ~exact_by_default elt in
680+
let type_ =
681+
Ty_printer.string_of_elt ~exact_by_default ~ts_syntax:(Context.ts_syntax cx) elt
682+
in
681683
(match kind with
682684
| `Declaration ->
683685
let declaration = Declaration.{ name; loc } in
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let tests =
3030
obj_props = [getter];
3131
}
3232
in
33-
let str = Ty_printer.string_of_t ~exact_by_default:true obj in
33+
let str = Ty_printer.string_of_t ~exact_by_default:true ~ts_syntax:false obj in
3434
assert_equal ~ctxt ~printer:(fun x -> x) "{get foo(): string}" str
3535
);
3636
( "type_object_property_set" >:: fun ctxt ->
@@ -53,12 +53,12 @@ let tests =
5353
obj_props = [setter];
5454
}
5555
in
56-
let str = Ty_printer.string_of_t ~exact_by_default:true obj in
56+
let str = Ty_printer.string_of_t ~exact_by_default:true ~ts_syntax:false obj in
5757
assert_equal ~ctxt ~printer:(fun x -> x) "{set foo(string): void}" str
5858
);
5959
( "empty_inexact_tuple" >:: fun ctxt ->
6060
let tup = Ty.Tup { elements = []; inexact = true } in
61-
let str = Ty_printer.string_of_t tup in
61+
let str = Ty_printer.string_of_t ~ts_syntax:false tup in
6262
assert_equal ~ctxt ~printer:(fun x -> x) "[...]" str
6363
);
6464
( "inexact_tuple" >:: fun ctxt ->
@@ -69,7 +69,7 @@ let tests =
6969
]
7070
in
7171
let tup = Ty.Tup { elements; inexact = true } in
72-
let str = Ty_printer.string_of_t tup in
72+
let str = Ty_printer.string_of_t ~ts_syntax:false tup in
7373
assert_equal ~ctxt ~printer:(fun x -> x) "[number, ...]" str
7474
);
7575
]
Original file line numberDiff line numberDiff line change
@@ -304,29 +304,29 @@ module Sorting = struct
304304
( "idempotence" >:: fun ctxt ->
305305
assert_equal
306306
~ctxt
307-
~printer:(Ty_printer.string_of_t ~exact_by_default:true)
307+
~printer:(Ty_printer.string_of_t ~exact_by_default:true ~ts_syntax:false)
308308
(simplify_base t0)
309309
(simplify_base (simplify_base t0));
310310
assert_equal
311311
~ctxt
312-
~printer:(Ty_printer.string_of_t ~exact_by_default:true)
312+
~printer:(Ty_printer.string_of_t ~exact_by_default:true ~ts_syntax:false)
313313
(simplify_base t6)
314314
(simplify_base (simplify_base (simplify_base t6)));
315315
assert_equal
316316
~ctxt
317-
~printer:(Ty_printer.string_of_t ~exact_by_default:true)
317+
~printer:(Ty_printer.string_of_t ~exact_by_default:true ~ts_syntax:false)
318318
(simplify_sort t4)
319319
(simplify_sort (simplify_sort t4));
320320
assert_equal
321321
~ctxt
322-
~printer:(Ty_printer.string_of_t ~exact_by_default:true)
322+
~printer:(Ty_printer.string_of_t ~exact_by_default:true ~ts_syntax:false)
323323
(simplify_sort t6)
324324
(simplify_sort (simplify_sort (simplify_sort t6)))
325325
);
326326
( "sorting" >:: fun ctxt ->
327327
assert_equal
328328
~ctxt
329-
~printer:(Ty_printer.string_of_t ~exact_by_default:true)
329+
~printer:(Ty_printer.string_of_t ~exact_by_default:true ~ts_syntax:false)
330330
t6_sorted
331331
(simplify_sort t6)
332332
);
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ let variance_ = function
4444
(* Main Transformation *)
4545
(*************************)
4646

47-
let layout_of_elt ~prefer_single_quotes ?(size = 5000) ?(with_comments = true) ~exact_by_default elt
48-
=
47+
let layout_of_elt
48+
~prefer_single_quotes ?(size = 5000) ?(with_comments = true) ~exact_by_default ~ts_syntax elt =
4949
let size = ref size in
5050
let local_name_of_symbol symbol =
5151
let { sym_name = name; sym_provenance = provenance; _ } = symbol in
@@ -537,9 +537,12 @@ let layout_of_elt ~prefer_single_quotes ?(size = 5000) ?(with_comments = true) ~
537537
| _ -> type_ ~depth t
538538
and enum_decl s = fuse [Atom "enum"; space; identifier (local_name_of_symbol s)]
539539
and utility ~depth u =
540-
let ctor = Ty.string_of_utility_ctor u in
541-
let ts = Ty.types_of_utility u in
542-
type_reference ~depth (identifier (Reason.OrdinaryName ctor)) ts
540+
match u with
541+
| ReadOnly (Tup _ as t) when ts_syntax -> fuse [Atom "readonly"; space; type_ ~depth t]
542+
| _ ->
543+
let ctor = Ty.string_of_utility_ctor u in
544+
let ts = Ty.types_of_utility u in
545+
type_reference ~depth (identifier (Reason.OrdinaryName ctor)) ts
543546
and type_parameter ~depth params =
544547
list
545548
~wrap:(Atom "<", Atom ">")
@@ -661,9 +664,16 @@ let layout_of_elt ~prefer_single_quotes ?(size = 5000) ?(with_comments = true) ~
661664
| Decl d -> decl ~depth:0 d
662665

663666
let layout_of_type_at_pos_types
664-
~prefer_single_quotes ?size ?with_comments ~exact_by_default (unevaluated, evaluated) =
667+
~prefer_single_quotes ?size ?with_comments ~exact_by_default ~ts_syntax (unevaluated, evaluated)
668+
=
665669
let layout_unevaluated =
666-
layout_of_elt ~prefer_single_quotes ?size ?with_comments ~exact_by_default unevaluated
670+
layout_of_elt
671+
~prefer_single_quotes
672+
?size
673+
?with_comments
674+
~exact_by_default
675+
~ts_syntax
676+
unevaluated
667677
in
668678
match (unevaluated, evaluated) with
669679
| (_, None) -> layout_unevaluated
@@ -676,7 +686,13 @@ let layout_of_type_at_pos_types
676686
| Decl (TypeAliasDecl { type_ = Some t; _ }) -> Type t
677687
| x -> x
678688
in
679-
layout_of_elt ~prefer_single_quotes ?size ?with_comments ~exact_by_default evaluated
689+
layout_of_elt
690+
~prefer_single_quotes
691+
?size
692+
?with_comments
693+
~exact_by_default
694+
~ts_syntax
695+
evaluated
680696
in
681697
fuse [layout_unevaluated; hardline; Atom "="; space; layout_evaluated]
682698

@@ -702,31 +718,58 @@ let print_single_line ~source_maps node =
702718
let print_pretty ~source_maps node = Pretty_printer.print ~source_maps ~skip_endline:true node
703719

704720
let string_of_elt
705-
?(prefer_single_quotes = false) ?(with_comments = true) (elt : Ty.elt) ~exact_by_default :
706-
string =
707-
layout_of_elt ~prefer_single_quotes ~with_comments ~exact_by_default elt
721+
?(prefer_single_quotes = false)
722+
?(with_comments = true)
723+
~exact_by_default
724+
~ts_syntax
725+
(elt : Ty.elt) : string =
726+
layout_of_elt ~prefer_single_quotes ~with_comments ~exact_by_default ~ts_syntax elt
708727
|> print_pretty ~source_maps:None
709728
|> Source.contents
710729

711730
let string_of_elt_single_line
712-
?(prefer_single_quotes = false) ?(with_comments = true) ?(exact_by_default = true) (elt : Ty.elt)
713-
=
714-
layout_of_elt ~prefer_single_quotes ~with_comments ~exact_by_default elt
731+
?(prefer_single_quotes = false)
732+
?(with_comments = true)
733+
?(exact_by_default = true)
734+
~ts_syntax
735+
(elt : Ty.elt) =
736+
layout_of_elt ~prefer_single_quotes ~with_comments ~exact_by_default ~ts_syntax elt
715737
|> print_single_line ~source_maps:None
716738
|> Source.contents
717739

718740
let string_of_t
719-
?(prefer_single_quotes = false) ?(with_comments = true) ?(exact_by_default = true) (ty : Ty.t) =
720-
string_of_elt ~prefer_single_quotes ~with_comments ~exact_by_default (Ty.Type ty)
741+
?(prefer_single_quotes = false)
742+
?(with_comments = true)
743+
?(exact_by_default = true)
744+
~ts_syntax
745+
(ty : Ty.t) =
746+
string_of_elt ~prefer_single_quotes ~with_comments ~exact_by_default ~ts_syntax (Ty.Type ty)
721747

722748
let string_of_t_single_line
723-
?(prefer_single_quotes = false) ?(with_comments = true) ?(exact_by_default = true) (ty : Ty.t) =
724-
string_of_elt_single_line ~prefer_single_quotes ~with_comments ~exact_by_default (Ty.Type ty)
749+
?(prefer_single_quotes = false)
750+
?(with_comments = true)
751+
?(exact_by_default = true)
752+
~ts_syntax
753+
(ty : Ty.t) =
754+
string_of_elt_single_line
755+
~prefer_single_quotes
756+
~with_comments
757+
~exact_by_default
758+
~ts_syntax
759+
(Ty.Type ty)
725760

726761
let string_of_decl_single_line
727-
?(prefer_single_quotes = false) ?(with_comments = true) ?(exact_by_default = true) (d : Ty.decl)
728-
=
729-
string_of_elt_single_line ~prefer_single_quotes ~with_comments ~exact_by_default (Ty.Decl d)
762+
?(prefer_single_quotes = false)
763+
?(with_comments = true)
764+
?(exact_by_default = true)
765+
~ts_syntax
766+
(d : Ty.decl) =
767+
string_of_elt_single_line
768+
~prefer_single_quotes
769+
~with_comments
770+
~exact_by_default
771+
~ts_syntax
772+
(Ty.Decl d)
730773

731774
let string_of_symbol_set syms =
732775
LocSymbolSet.elements syms
@@ -745,10 +788,11 @@ let string_of_type_at_pos_result
745788
?(prefer_single_quotes = false)
746789
?(with_comments = true)
747790
?(exact_by_default = true)
791+
~ts_syntax
748792
{ Ty.unevaluated; evaluated; refs } =
749793
let type_str =
750794
(unevaluated, evaluated)
751-
|> layout_of_type_at_pos_types ~prefer_single_quotes ~with_comments ~exact_by_default
795+
|> layout_of_type_at_pos_types ~prefer_single_quotes ~with_comments ~exact_by_default ~ts_syntax
752796
|> print_pretty ~source_maps:None
753797
|> Source.contents
754798
in
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,50 @@ val better_quote : prefer_single_quotes:bool -> string -> string
1010
val property_key_quotes_needed : string -> bool
1111

1212
val string_of_elt :
13-
?prefer_single_quotes:bool -> ?with_comments:bool -> Ty.elt -> exact_by_default:bool -> string
13+
?prefer_single_quotes:bool ->
14+
?with_comments:bool ->
15+
exact_by_default:bool ->
16+
ts_syntax:bool ->
17+
Ty.elt ->
18+
string
1419

1520
val string_of_elt_single_line :
16-
?prefer_single_quotes:bool -> ?with_comments:bool -> ?exact_by_default:bool -> Ty.elt -> string
21+
?prefer_single_quotes:bool ->
22+
?with_comments:bool ->
23+
?exact_by_default:bool ->
24+
ts_syntax:bool ->
25+
Ty.elt ->
26+
string
1727

1828
val string_of_decl_single_line :
19-
?prefer_single_quotes:bool -> ?with_comments:bool -> ?exact_by_default:bool -> Ty.decl -> string
29+
?prefer_single_quotes:bool ->
30+
?with_comments:bool ->
31+
?exact_by_default:bool ->
32+
ts_syntax:bool ->
33+
Ty.decl ->
34+
string
2035

2136
val string_of_t :
22-
?prefer_single_quotes:bool -> ?with_comments:bool -> ?exact_by_default:bool -> Ty.t -> string
37+
?prefer_single_quotes:bool ->
38+
?with_comments:bool ->
39+
?exact_by_default:bool ->
40+
ts_syntax:bool ->
41+
Ty.t ->
42+
string
2343

2444
val string_of_t_single_line :
25-
?prefer_single_quotes:bool -> ?with_comments:bool -> ?exact_by_default:bool -> Ty.t -> string
45+
?prefer_single_quotes:bool ->
46+
?with_comments:bool ->
47+
?exact_by_default:bool ->
48+
ts_syntax:bool ->
49+
Ty.t ->
50+
string
2651

2752
val string_of_type_at_pos_result :
2853
?prefer_single_quotes:bool ->
2954
?with_comments:bool ->
3055
?exact_by_default:bool ->
56+
ts_syntax:bool ->
3157
Ty.type_at_pos_result ->
3258
string * (string * Loc.t) list option
3359

Original file line numberDiff line numberDiff line change
@@ -535,7 +535,12 @@ let infer_type filename content line col js_config_object :
535535
| FailureUnparseable (loc, _, _) ->
536536
(loc, Error "Unparseable", refining_locs, refinement_invalidated)
537537
| Success (loc, result) ->
538-
let (result, _) = Ty_printer.string_of_type_at_pos_result ~exact_by_default:true result in
538+
let (result, _) =
539+
Ty_printer.string_of_type_at_pos_result
540+
~exact_by_default:true
541+
~ts_syntax:(Context.ts_syntax cx)
542+
result
543+
in
539544
(loc, Ok result, refining_locs, refinement_invalidated)
540545
end
541546

@@ -627,7 +632,9 @@ let dump_types js_file js_content js_config_object =
627632
)
628633
in
629634
let (cx, typed_ast) = infer_and_merge ~root filename js_config_object docblock ast file_sig in
630-
let printer = Ty_printer.string_of_elt_single_line ~exact_by_default:true in
635+
let printer =
636+
Ty_printer.string_of_elt_single_line ~exact_by_default:true ~ts_syntax:(Context.ts_syntax cx)
637+
in
631638
let types =
632639
Query_types.dump_types
633640
~printer
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ let infer_type_to_response
695695
~json
696696
~expanded
697697
~exact_by_default
698+
~ts_syntax
698699
~strip_root
699700
loc
700701
refining_locs
@@ -709,7 +710,11 @@ let infer_type_to_response
709710
let open Hh_json in
710711
let type_json t =
711712
let json_obj =
712-
[("type", JSON_String (Ty_printer.string_of_elt_single_line ~exact_by_default t))]
713+
[
714+
( "type",
715+
JSON_String (Ty_printer.string_of_elt_single_line ~exact_by_default ~ts_syntax t)
716+
);
717+
]
713718
in
714719
let json_obj =
715720
if expanded then
@@ -730,7 +735,9 @@ let infer_type_to_response
730735
else
731736
ServerProt.Response.InferType.Friendly
732737
(Base.Option.map tys ~f:(fun r ->
733-
let (type_str, refs) = Ty_printer.string_of_type_at_pos_result ~exact_by_default r in
738+
let (type_str, refs) =
739+
Ty_printer.string_of_type_at_pos_result ~exact_by_default ~ts_syntax r
740+
in
734741
{ ServerProt.Response.InferType.type_str; refs }
735742
)
736743
)
@@ -891,6 +898,7 @@ let infer_type
891898
~json
892899
~expanded
893900
~exact_by_default
901+
~ts_syntax:(Options.ts_syntax options)
894902
~strip_root
895903
loc
896904
refining_locs
@@ -969,6 +977,7 @@ let inlay_hint
969977
let (type_str, refs) =
970978
Ty_printer.string_of_type_at_pos_result
971979
~exact_by_default:(Options.exact_by_default options)
980+
~ts_syntax:(Options.ts_syntax options)
972981
r
973982
in
974983
{ ServerProt.Response.InferType.type_str; refs }

0 commit comments

Comments
 (0)