diff --git a/src/napkin_printer.ml b/src/napkin_printer.ml index 565e0c95..f389a7b0 100644 --- a/src/napkin_printer.ml +++ b/src/napkin_printer.ml @@ -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; ] ) diff --git a/tests/conversion/reason/__snapshots__/render.spec.js.snap b/tests/conversion/reason/__snapshots__/render.spec.js.snap index e0f40a8f..5692f216 100644 --- a/tests/conversion/reason/__snapshots__/render.spec.js.snap +++ b/tests/conversion/reason/__snapshots__/render.spec.js.snap @@ -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, \\"right\\": option} " diff --git a/tests/conversion/reason/record.re b/tests/conversion/reason/record.re new file mode 100644 index 00000000..a692e5cf --- /dev/null +++ b/tests/conversion/reason/record.re @@ -0,0 +1 @@ +type r = { a, b } diff --git a/tests/printer/typeDef/__snapshots__/render.spec.js.snap b/tests/printer/typeDef/__snapshots__/render.spec.js.snap index 55513097..0f4c0580 100644 --- a/tests/printer/typeDef/__snapshots__/render.spec.js.snap +++ b/tests/printer/typeDef/__snapshots__/render.spec.js.snap @@ -180,6 +180,20 @@ type user = { @attrSuperLongNaaaaaaaaaaaaaaameSooooooooLong hairStyle: Hair.t, } + +type punned = { + name, + @attr name2, + mutable name3, + name4: @attrNotPunned name4, + name5: name5, +} + +// not punned, user wrote typexpr +type t = {a: a, b: b} + +// punned, user only wrote key +type t = {a, b} " `; diff --git a/tests/printer/typeDef/record.js b/tests/printer/typeDef/record.js index d39a82b2..93ee1166 100644 --- a/tests/printer/typeDef/record.js +++ b/tests/printer/typeDef/record.js @@ -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 +} + +// not punned, user wrote typexpr +type t = {a: a, b: b} + +// punned, user only wrote key +type t = {a, b}