From 10888316a9a0fe9ed550e3f3ec7ef9e85e36880a Mon Sep 17 00:00:00 2001 From: Iwan Date: Tue, 21 Jul 2020 16:37:00 +0200 Subject: [PATCH 1/2] Pun record declaration fields when possible. Requested in https://github.com/BuckleScript/syntax/issues/47 --- src/napkin_printer.ml | 18 ++++++++++++++++-- .../reason/__snapshots__/render.spec.js.snap | 5 +++++ tests/conversion/reason/record.re | 1 + .../comments/__snapshots__/render.spec.js.snap | 2 +- .../typeDef/__snapshots__/render.spec.js.snap | 8 ++++++++ tests/printer/typeDef/record.js | 8 ++++++++ 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/conversion/reason/record.re diff --git a/src/napkin_printer.ml b/src/napkin_printer.ml index 565e0c95..9f27bb43 100644 --- a/src/napkin_printer.ml +++ b/src/napkin_printer.ml @@ -1420,13 +1420,27 @@ 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}, + { + ptyp_desc = Ptyp_constr (({txt = Lident constr}), []); + ptyp_attributes = []; + } + ) when key = constr -> + 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/comments/__snapshots__/render.spec.js.snap b/tests/printer/comments/__snapshots__/render.spec.js.snap index 0e5a6a3a..8da08784 100644 --- a/tests/printer/comments/__snapshots__/render.spec.js.snap +++ b/tests/printer/comments/__snapshots__/render.spec.js.snap @@ -1710,7 +1710,7 @@ type t = { } type rec t = { - facing: facing, + facing, // frames } " diff --git a/tests/printer/typeDef/__snapshots__/render.spec.js.snap b/tests/printer/typeDef/__snapshots__/render.spec.js.snap index 55513097..e583ddf8 100644 --- a/tests/printer/typeDef/__snapshots__/render.spec.js.snap +++ b/tests/printer/typeDef/__snapshots__/render.spec.js.snap @@ -180,6 +180,14 @@ type user = { @attrSuperLongNaaaaaaaaaaaaaaameSooooooooLong hairStyle: Hair.t, } + +type punned = { + name, + @attr name2, + mutable name3, + name4: @attrNotPunned name4, + name5: name5, +} " `; diff --git a/tests/printer/typeDef/record.js b/tests/printer/typeDef/record.js index d39a82b2..e5f74967 100644 --- a/tests/printer/typeDef/record.js +++ b/tests/printer/typeDef/record.js @@ -51,3 +51,11 @@ type user = { hairStyle: Hair.t, @attrAbove @attrSuperLongNaaaaaaaaaaaaaaameSooooooooLong @attrSuperLongNaaaaaaaaaaaaaaameSooooooooLong hairStyle: Hair.t } + +type punned = { + name, + @attr name2, + mutable name3 + name4: @attrNotPunned name4 + name5: name5 +} From 23141a007e3fb89b2bfc32dd8c4bd6082c7e1b33 Mon Sep 17 00:00:00 2001 From: Iwan Date: Tue, 21 Jul 2020 22:26:30 +0200 Subject: [PATCH 2/2] Tweak printing to differentiate between {a: a, b: b} and {a, b} --- src/napkin_printer.ml | 9 +++++++-- tests/printer/comments/__snapshots__/render.spec.js.snap | 2 +- tests/printer/typeDef/__snapshots__/render.spec.js.snap | 6 ++++++ tests/printer/typeDef/record.js | 6 ++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/napkin_printer.ml b/src/napkin_printer.ml index 9f27bb43..f389a7b0 100644 --- a/src/napkin_printer.ml +++ b/src/napkin_printer.ml @@ -1422,12 +1422,17 @@ and printLabelDeclaration (ld : Parsetree.label_declaration) cmtTbl = in let typeDoc = match (ld.pld_name, ld.pld_type) with | ( - {txt = key}, + {txt = key; loc = keyLoc}, { ptyp_desc = Ptyp_constr (({txt = Lident constr}), []); ptyp_attributes = []; + ptyp_loc = typLoc } - ) when key = constr -> + ) 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 [ diff --git a/tests/printer/comments/__snapshots__/render.spec.js.snap b/tests/printer/comments/__snapshots__/render.spec.js.snap index 8da08784..0e5a6a3a 100644 --- a/tests/printer/comments/__snapshots__/render.spec.js.snap +++ b/tests/printer/comments/__snapshots__/render.spec.js.snap @@ -1710,7 +1710,7 @@ type t = { } type rec t = { - facing, + facing: facing, // frames } " diff --git a/tests/printer/typeDef/__snapshots__/render.spec.js.snap b/tests/printer/typeDef/__snapshots__/render.spec.js.snap index e583ddf8..0f4c0580 100644 --- a/tests/printer/typeDef/__snapshots__/render.spec.js.snap +++ b/tests/printer/typeDef/__snapshots__/render.spec.js.snap @@ -188,6 +188,12 @@ type punned = { 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 e5f74967..93ee1166 100644 --- a/tests/printer/typeDef/record.js +++ b/tests/printer/typeDef/record.js @@ -59,3 +59,9 @@ type punned = { 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}