Skip to content

Commit

Permalink
Fix printing of externals that happen to have newlines/quotes in them.
Browse files Browse the repository at this point in the history
Summary:Before this diff, we would print these as "" strings with \n inside of them.

Test Plan:Added test cases

Reviewers:

CC:
  • Loading branch information
jordwalke committed Jun 18, 2020
1 parent fd63f5d commit b1e6f16
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
29 changes: 28 additions & 1 deletion formatTest/unit_tests/expected_output/externals.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,31 @@
external foo: type_ = "%caml_something_or_other";

external multilineStringExtern: int => int =
"\n Did you know you can put whatver you want inside\n of an extern? Good luck with the linker though!\n";
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};

module Nested = {
external multilineStringExtern: int => int =
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
external multilineStringExternWithTag:
int => int =
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
external multilineStringExtern: int => int =
{|
And this has a newline in it, so will be formatted with { | | } style string|};
external containsQuote: int => int =
{|This has a quote in it " so will be formatted as { | | } style string|};
external noIndentation: int => int =
{|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
};
18 changes: 18 additions & 0 deletions formatTest/unit_tests/input/externals.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@ external multilineStringExtern : int => int = {|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};

module Nested = {
external multilineStringExtern : int => int = {|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
external multilineStringExternWithTag : int => int = {js|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|js};
external multilineStringExtern : int => int = "
And this has a newline in it, so will be formatted with { | | } style string";
external containsQuote : int => int = "This has a quote in it \" so will be formatted as { | | } style string";
external noIndentation : int => int = {|
Did you know you can put whatver you want inside
of an extern? Good luck with the linker though!
|};
};
13 changes: 9 additions & 4 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1991,8 +1991,12 @@ let paren b fu ppf x =
then Format.fprintf ppf "(%a)" fu x
else fu ppf x

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

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

method constant_string = wrap constant_string
method constant_string_for_primitive = wrap constant_string_for_primitive
method tyvar = wrap tyvar

(* c ['a,'b] *)
Expand Down Expand Up @@ -6684,7 +6688,8 @@ let printer = object(self:'self)
| [""] -> lblBefore
| _ ->
let frstHalf = makeList ~postSpace:true [lblBefore; atom "="] in
let sndHalf = makeSpacedBreakableInlineList (List.map self#constant_string vd.pval_prim) in
let sndHalf =
makeSpacedBreakableInlineList (List.map self#constant_string_for_primitive vd.pval_prim) in
label ~space:true frstHalf sndHalf
in
match vd.pval_attributes with
Expand Down

0 comments on commit b1e6f16

Please sign in to comment.