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

Print punned fields #480

Merged
merged 3 commits into from
Apr 18, 2022
Merged
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
37 changes: 26 additions & 11 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,10 @@ and printExpression (e : Parsetree.expression) cmtTbl =
let forceBreak =
e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum
in
let punningAllowed = match spreadExpr, rows with
| (None, [_]) -> false (* disallow punning for single-element records *)
| _ -> true
in
Doc.breakableGroup ~forceBreak (
Doc.concat([
Doc.lbrace;
Expand All @@ -2815,7 +2819,7 @@ and printExpression (e : Parsetree.expression) cmtTbl =
Doc.softLine;
spread;
Doc.join ~sep:(Doc.concat [Doc.text ","; Doc.line])
(List.map (fun row -> printRecordRow row cmtTbl) rows)
(List.map (fun row -> printRecordRow row cmtTbl punningAllowed) rows)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing punningAllowed to every row, would it be "clearer" if we special cased the one-element record?

// pseudo-code

if (one-element-record) {
  
} else {
  List.map printRecordRow…
}

What do you think about this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried that here: cknitt@d83c61f

Do you think it is an improvement?

]
);
Doc.trailingComma;
Expand Down Expand Up @@ -4818,17 +4822,28 @@ and printDirectionFlag flag = match flag with
| Asttypes.Downto -> Doc.text " downto "
| Asttypes.Upto -> Doc.text " to "

and printRecordRow (lbl, expr) cmtTbl =
and printRecordRow (lbl, expr) cmtTbl punningAllowed =
let cmtLoc = {lbl.loc with loc_end = expr.pexp_loc.loc_end} in
let doc = Doc.group (Doc.concat [
printLidentPath lbl cmtTbl;
Doc.text ": ";
(let doc = printExpressionWithComments expr cmtTbl in
match Parens.expr expr with
| Parens.Parenthesized -> addParens doc
| Braced braces -> printBraces doc expr braces
| Nothing -> doc);
]) in
let doc = Doc.group (
match expr.pexp_desc with
| Pexp_ident({txt = Lident key; loc = keyLoc}) when (
punningAllowed &&
Longident.last lbl.txt = key &&
lbl.loc.loc_start.pos_cnum == keyLoc.loc_start.pos_cnum
) ->
(* print punned field *)
printLidentPath lbl cmtTbl;
| _ ->
Doc.concat [
printLidentPath lbl cmtTbl;
Doc.text ": ";
(let doc = printExpressionWithComments expr cmtTbl in
match Parens.expr expr with
| Parens.Parenthesized -> addParens doc
| Braced braces -> printBraces doc expr braces
| Nothing -> doc);
]
) in
printComments doc cmtTbl cmtLoc

and printBsObjectRow (lbl, expr) cmtTbl =
Expand Down
2 changes: 1 addition & 1 deletion tests/conversion/reason/expected/bracedJsx.re.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ let make = () => {
],
),
}
| SetValue(input) => {...state, input: input}
| SetValue(input) => {...state, input}
}
, {history: [], input: ""})

Expand Down
2 changes: 1 addition & 1 deletion tests/conversion/reason/expected/braces.re.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ let getDailyNewCases = x =>
| Pair({prevRecord, record}) =>
let confirmed = record.confirmed - prevRecord.confirmed
let deaths = record.deaths - prevRecord.deaths
{confirmed: confirmed, deaths: deaths}
{confirmed, deaths}
}
2 changes: 1 addition & 1 deletion tests/printer/expr/expected/block.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ React.useEffect0(() => {
switch videoContainerRect {
| Some(videoContainerRect) =>
let newChapter = ({startTime: percent *. duration}: Video.chapter)
{a: a, b: b}->onChange
{a, b}->onChange
| _ => ()
}
}}
Expand Down
32 changes: 32 additions & 0 deletions tests/printer/expr/expected/record.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,35 @@ let user = {
}
// braced + constrained expr
let user = {name: {(ceo.name: string)}}

// Punning
let r = {a} // actually not a record, just an expression in braces
let r = {a, b}
let r = {a, b, c: 42}
let r = {A.a, b}
let r = {A.a: a, b}
let r = {a: a, b}
let r = {a, b: b}

// Punning + comments
let r = {
// a
a,
// b
b,
}
let r = {
a, // a
b, // b
}
let r = {
/* a */
a,
/* b */
b,
}
let r = {
a /* a */,
b /* b */,
}
let r = {a /* a */, b /* b */}
33 changes: 33 additions & 0 deletions tests/printer/expr/record.res
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,36 @@ let user = {name: {
}}
// braced + constrained expr
let user = {name: {(ceo.name: string)}}


// Punning
let r = {a} // actually not a record, just an expression in braces
let r = {a, b}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an extra test case with all kind of different comments on punned key-values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some, looks good to me.

let r = {a, b, c: 42}
let r = {A.a, b}
let r = {A.a: a, b}
let r = {a: a, b}
let r = {a, b: b}

// Punning + comments
let r = {
// a
a,
// b
b,
}
let r = {
a, // a
b, // b
}
let r = {
/* a */
a,
/* b */
b,
}
let r = {
a /* a */,
b /* b */,
}
let r = {a /* a */, b /* b */}