Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Pun record declaration fields when possible. #64

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 21 additions & 2 deletions src/napkin_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1420,13 +1420,32 @@ and printLabelDeclaration (ld : Parsetree.label_declaration) cmtTbl =
let doc = printIdentLike ld.pld_name.txt in
printComments doc cmtTbl ld.pld_name.loc
in
let typeDoc = match (ld.pld_name, ld.pld_type) with
| (
{txt = key; loc = keyLoc},
{
ptyp_desc = Ptyp_constr (({txt = Lident constr}), []);
ptyp_attributes = [];
ptyp_loc = typLoc
}
) when key = constr && keyLoc.loc_start.pos_cnum == typLoc.loc_start.pos_cnum ->
(* we detect punning by checking:
* - is the key and the name of the constructor the same
* - is the start of the location of the key and the constructor the same
* By doing this we make a difference between {a, b} and {a: a, b: b} *)
Doc.nil
| _ ->
Doc.concat [
Doc.text ": ";
printTypExpr ld.pld_type cmtTbl;
]
in
Doc.group (
Doc.concat [
attrs;
mutableFlag;
name;
Doc.text ": ";
printTypExpr ld.pld_type cmtTbl;
typeDoc;
]
)

Expand Down
5 changes: 5 additions & 0 deletions tests/conversion/reason/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,11 @@ let () = switch [Color.red, Color.blue, Color.green] {
"
`;

exports[`record.re 1`] = `
"type r = {a, b}
"
`;

exports[`recursiveType.ml 1`] = `
"type rec tree = {\\"label\\": string, \\"left\\": option<tree>, \\"right\\": option<tree>}
"
Expand Down
1 change: 1 addition & 0 deletions tests/conversion/reason/record.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type r = { a, b }
14 changes: 14 additions & 0 deletions tests/printer/typeDef/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ type user = {
@attrSuperLongNaaaaaaaaaaaaaaameSooooooooLong
hairStyle: Hair.t,
}

type punned = {
name,
@attr name2,
mutable name3,
name4: @attrNotPunned name4,
name5: name5<int>,
}

// not punned, user wrote typexpr
type t = {a: a, b: b}

// punned, user only wrote key
type t = {a, b}
"
`;

Expand Down
14 changes: 14 additions & 0 deletions tests/printer/typeDef/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ type user = {
hairStyle: Hair.t,
@attrAbove @attrSuperLongNaaaaaaaaaaaaaaameSooooooooLong @attrSuperLongNaaaaaaaaaaaaaaameSooooooooLong hairStyle: Hair.t
}

type punned = {
name,
@attr name2,
mutable name3
name4: @attrNotPunned name4
name5: name5<int>
}

// not punned, user wrote typexpr
type t = {a: a, b: b}

// punned, user only wrote key
type t = {a, b}