Skip to content

Commit

Permalink
Polyvariants printing similar to variants (#2708)
Browse files Browse the repository at this point in the history
* printing: break poly variant type

* add history entry

* revert a bad change

* fix 4.10 test

* fix 4.12 test

* avoid a List.length

---------

Co-authored-by: Antonio Nuno Monteiro <[email protected]>
  • Loading branch information
SanderSpies and anmonteiro authored Jul 28, 2024
1 parent e1d0b71 commit d3fd746
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[#2773](https://github.com/reasonml/reason/pull/2773))
- Wrap `let lazy patterns = ..` in parentheses (`let lazy(patterns) = ..`)
(@anmonteiro, [#2774](https://github.com/reasonml/reason/pull/2774))
- Print poly variants as normal variansts (@Sander Spies) [#2708](https://github.com/reasonml/reason/pull/2708)

## 3.12.0

Expand Down
6 changes: 5 additions & 1 deletion src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3118,7 +3118,11 @@ let printer = object(self:'self)
let ll = (List.map (fun t -> atom ("`" ^ t)) tl) in
let tag_list = makeList ~postSpace:true ~break:IfNeed ((atom ">")::ll) in
let type_list = if tl != [] then node_list@[tag_list] else node_list in
makeList ~wrap:("[" ^ designator,"]") ~pad:(true, false) ~postSpace:true ~break:IfNeed type_list
let break = match type_list with
| _ :: _ :: _ -> Layout.Always_rec
| [] | _ :: [] -> IfNeed
in
makeList ~wrap:("[" ^ designator,"]") ~pad:(true, false) ~postSpace:true ~break type_list
| Ptyp_class (li, []) -> makeList [atom "#"; self#longident_loc li]
| Ptyp_class (li, l) ->
label
Expand Down
9 changes: 6 additions & 3 deletions test/4.10/type-jsx.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ Print the formatted file
/**
* Test no conflict with polymorphic variant types.
*/
type thisType = [ | `Foo | `Bar];
type thisType = [
| `Foo
| `Bar
];
type t('a) = [< thisType] as 'a;

let asd =
Expand Down Expand Up @@ -590,8 +593,8 @@ Print the formatted file

Type-check basics
$ ocamlc -c -pp 'refmt --print binary' -intf-suffix .rei -impl formatted.re
File "formatted.re", line 460, characters 23-26:
460 | <Optional1 required=?zzz />;
File "formatted.re", line 463, characters 23-26:
463 | <Optional1 required=?zzz />;
^^^
Warning 43: the label required is not optional.

Expand Down
9 changes: 6 additions & 3 deletions test/4.12/type-jsx.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ Print the formatted file
/**
* Test no conflict with polymorphic variant types.
*/
type thisType = [ | `Foo | `Bar];
type thisType = [
| `Foo
| `Bar
];
type t('a) = [< thisType] as 'a;

let asd =
Expand Down Expand Up @@ -590,8 +593,8 @@ Print the formatted file

Type-check basics
$ ocamlc -c -pp 'refmt --print binary' -intf-suffix .rei -impl formatted.re
File "formatted.re", line 460, characters 23-26:
460 | <Optional1 required=?zzz />;
File "formatted.re", line 463, characters 23-26:
463 | <Optional1 required=?zzz />;
^^^
Warning 43 [nonoptional-label]: the label required is not optional.

Expand Down
5 changes: 4 additions & 1 deletion test/ocaml_identifiers.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ Format OCaml identifiers file

/* Polymorphic variants (probably ok as-is?) */
module P = {
type t = [ | `pub_ | `method];
type t = [
| `pub_
| `method
];

let x = `method;

Expand Down

0 comments on commit d3fd746

Please sign in to comment.