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

Commit 5be5a55

Browse files
authored
Print punned fields (#480)
* Print punned fields * Avoid List.length * Add test cases for punning + comments
1 parent a5b54ff commit 5be5a55

File tree

6 files changed

+94
-14
lines changed

6 files changed

+94
-14
lines changed

src/res_printer.ml

+26-11
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,10 @@ and printExpression (e : Parsetree.expression) cmtTbl =
28072807
let forceBreak =
28082808
e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum
28092809
in
2810+
let punningAllowed = match spreadExpr, rows with
2811+
| (None, [_]) -> false (* disallow punning for single-element records *)
2812+
| _ -> true
2813+
in
28102814
Doc.breakableGroup ~forceBreak (
28112815
Doc.concat([
28122816
Doc.lbrace;
@@ -2815,7 +2819,7 @@ and printExpression (e : Parsetree.expression) cmtTbl =
28152819
Doc.softLine;
28162820
spread;
28172821
Doc.join ~sep:(Doc.concat [Doc.text ","; Doc.line])
2818-
(List.map (fun row -> printRecordRow row cmtTbl) rows)
2822+
(List.map (fun row -> printRecordRow row cmtTbl punningAllowed) rows)
28192823
]
28202824
);
28212825
Doc.trailingComma;
@@ -4818,17 +4822,28 @@ and printDirectionFlag flag = match flag with
48184822
| Asttypes.Downto -> Doc.text " downto "
48194823
| Asttypes.Upto -> Doc.text " to "
48204824

4821-
and printRecordRow (lbl, expr) cmtTbl =
4825+
and printRecordRow (lbl, expr) cmtTbl punningAllowed =
48224826
let cmtLoc = {lbl.loc with loc_end = expr.pexp_loc.loc_end} in
4823-
let doc = Doc.group (Doc.concat [
4824-
printLidentPath lbl cmtTbl;
4825-
Doc.text ": ";
4826-
(let doc = printExpressionWithComments expr cmtTbl in
4827-
match Parens.expr expr with
4828-
| Parens.Parenthesized -> addParens doc
4829-
| Braced braces -> printBraces doc expr braces
4830-
| Nothing -> doc);
4831-
]) in
4827+
let doc = Doc.group (
4828+
match expr.pexp_desc with
4829+
| Pexp_ident({txt = Lident key; loc = keyLoc}) when (
4830+
punningAllowed &&
4831+
Longident.last lbl.txt = key &&
4832+
lbl.loc.loc_start.pos_cnum == keyLoc.loc_start.pos_cnum
4833+
) ->
4834+
(* print punned field *)
4835+
printLidentPath lbl cmtTbl;
4836+
| _ ->
4837+
Doc.concat [
4838+
printLidentPath lbl cmtTbl;
4839+
Doc.text ": ";
4840+
(let doc = printExpressionWithComments expr cmtTbl in
4841+
match Parens.expr expr with
4842+
| Parens.Parenthesized -> addParens doc
4843+
| Braced braces -> printBraces doc expr braces
4844+
| Nothing -> doc);
4845+
]
4846+
) in
48324847
printComments doc cmtTbl cmtLoc
48334848

48344849
and printBsObjectRow (lbl, expr) cmtTbl =

tests/conversion/reason/expected/bracedJsx.re.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ let make = () => {
9191
],
9292
),
9393
}
94-
| SetValue(input) => {...state, input: input}
94+
| SetValue(input) => {...state, input}
9595
}
9696
, {history: [], input: ""})
9797

tests/conversion/reason/expected/braces.re.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ let getDailyNewCases = x =>
2020
| Pair({prevRecord, record}) =>
2121
let confirmed = record.confirmed - prevRecord.confirmed
2222
let deaths = record.deaths - prevRecord.deaths
23-
{confirmed: confirmed, deaths: deaths}
23+
{confirmed, deaths}
2424
}

tests/printer/expr/expected/block.res.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ React.useEffect0(() => {
6060
switch videoContainerRect {
6161
| Some(videoContainerRect) =>
6262
let newChapter = ({startTime: percent *. duration}: Video.chapter)
63-
{a: a, b: b}->onChange
63+
{a, b}->onChange
6464
| _ => ()
6565
}
6666
}}

tests/printer/expr/expected/record.res.txt

+32
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,35 @@ let user = {
3939
}
4040
// braced + constrained expr
4141
let user = {name: {(ceo.name: string)}}
42+
43+
// Punning
44+
let r = {a} // actually not a record, just an expression in braces
45+
let r = {a, b}
46+
let r = {a, b, c: 42}
47+
let r = {A.a, b}
48+
let r = {A.a: a, b}
49+
let r = {a: a, b}
50+
let r = {a, b: b}
51+
52+
// Punning + comments
53+
let r = {
54+
// a
55+
a,
56+
// b
57+
b,
58+
}
59+
let r = {
60+
a, // a
61+
b, // b
62+
}
63+
let r = {
64+
/* a */
65+
a,
66+
/* b */
67+
b,
68+
}
69+
let r = {
70+
a /* a */,
71+
b /* b */,
72+
}
73+
let r = {a /* a */, b /* b */}

tests/printer/expr/record.res

+33
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,36 @@ let user = {name: {
2828
}}
2929
// braced + constrained expr
3030
let user = {name: {(ceo.name: string)}}
31+
32+
33+
// Punning
34+
let r = {a} // actually not a record, just an expression in braces
35+
let r = {a, b}
36+
let r = {a, b, c: 42}
37+
let r = {A.a, b}
38+
let r = {A.a: a, b}
39+
let r = {a: a, b}
40+
let r = {a, b: b}
41+
42+
// Punning + comments
43+
let r = {
44+
// a
45+
a,
46+
// b
47+
b,
48+
}
49+
let r = {
50+
a, // a
51+
b, // b
52+
}
53+
let r = {
54+
/* a */
55+
a,
56+
/* b */
57+
b,
58+
}
59+
let r = {
60+
a /* a */,
61+
b /* b */,
62+
}
63+
let r = {a /* a */, b /* b */}

0 commit comments

Comments
 (0)