Skip to content

Commit b1e6f16

Browse files
committed
Fix printing of externals that happen to have newlines/quotes in them.
Summary:Before this diff, we would print these as "" strings with \n inside of them. Test Plan:Added test cases Reviewers: CC:
1 parent fd63f5d commit b1e6f16

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

formatTest/unit_tests/expected_output/externals.re

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,31 @@
44
external foo: type_ = "%caml_something_or_other";
55

66
external multilineStringExtern: int => int =
7-
"\n Did you know you can put whatver you want inside\n of an extern? Good luck with the linker though!\n";
7+
{|
8+
Did you know you can put whatver you want inside
9+
of an extern? Good luck with the linker though!
10+
|};
11+
12+
module Nested = {
13+
external multilineStringExtern: int => int =
14+
{|
15+
Did you know you can put whatver you want inside
16+
of an extern? Good luck with the linker though!
17+
|};
18+
external multilineStringExternWithTag:
19+
int => int =
20+
{|
21+
Did you know you can put whatver you want inside
22+
of an extern? Good luck with the linker though!
23+
|};
24+
external multilineStringExtern: int => int =
25+
{|
26+
And this has a newline in it, so will be formatted with { | | } style string|};
27+
external containsQuote: int => int =
28+
{|This has a quote in it " so will be formatted as { | | } style string|};
29+
external noIndentation: int => int =
30+
{|
31+
Did you know you can put whatver you want inside
32+
of an extern? Good luck with the linker though!
33+
|};
34+
};

formatTest/unit_tests/input/externals.re

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,21 @@ external multilineStringExtern : int => int = {|
77
Did you know you can put whatver you want inside
88
of an extern? Good luck with the linker though!
99
|};
10+
11+
module Nested = {
12+
external multilineStringExtern : int => int = {|
13+
Did you know you can put whatver you want inside
14+
of an extern? Good luck with the linker though!
15+
|};
16+
external multilineStringExternWithTag : int => int = {js|
17+
Did you know you can put whatver you want inside
18+
of an extern? Good luck with the linker though!
19+
|js};
20+
external multilineStringExtern : int => int = "
21+
And this has a newline in it, so will be formatted with { | | } style string";
22+
external containsQuote : int => int = "This has a quote in it \" so will be formatted as { | | } style string";
23+
external noIndentation : int => int = {|
24+
Did you know you can put whatver you want inside
25+
of an extern? Good luck with the linker though!
26+
|};
27+
};

src/reason-parser/reason_pprint_ast.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,8 +1991,12 @@ let paren b fu ppf x =
19911991
then Format.fprintf ppf "(%a)" fu x
19921992
else fu ppf x
19931993

1994-
let constant_string ppf s =
1995-
Format.fprintf ppf "%S" s
1994+
let constant_string_for_primitive ppf s =
1995+
let hasQuote = try String.index s '"' with Not_found -> -1 in
1996+
let hasNewline = try String.index s '\n' with Not_found -> -1 in
1997+
if hasQuote > -1 || hasNewline > -1 then
1998+
Format.fprintf ppf "{|%s|}" s
1999+
else Format.fprintf ppf "%S" s
19962000

19972001
let tyvar ppf str =
19982002
Format.fprintf ppf "'%s" str
@@ -2430,7 +2434,7 @@ let printer = object(self:'self)
24302434
method constant ?raw_literal ?(parens=true) =
24312435
wrap (constant ?raw_literal ~parens)
24322436

2433-
method constant_string = wrap constant_string
2437+
method constant_string_for_primitive = wrap constant_string_for_primitive
24342438
method tyvar = wrap tyvar
24352439

24362440
(* c ['a,'b] *)
@@ -6684,7 +6688,8 @@ let printer = object(self:'self)
66846688
| [""] -> lblBefore
66856689
| _ ->
66866690
let frstHalf = makeList ~postSpace:true [lblBefore; atom "="] in
6687-
let sndHalf = makeSpacedBreakableInlineList (List.map self#constant_string vd.pval_prim) in
6691+
let sndHalf =
6692+
makeSpacedBreakableInlineList (List.map self#constant_string_for_primitive vd.pval_prim) in
66886693
label ~space:true frstHalf sndHalf
66896694
in
66906695
match vd.pval_attributes with

0 commit comments

Comments
 (0)