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

Conversation

cknitt
Copy link
Member

@cknitt cknitt commented Mar 24, 2022

Currently, the printer removes record field punning as discussed in #252 (for record expressions) and #47 (for record type declarations). I.e., it expands {a, b} to {a: a, b: b}.

I gather from the discussions that we want to leave the behavior for record type declarations as it is, but improve the behavior for record expressions. This PR addresses that.

To avoid breaking changes for now, the printer will keep record expressions as the user wrote them, whether punned or not. That is, {a, b} will remain {a, b} and {a: a, b: b} will remain {a: a, b: b}.


// 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.

@@ -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?

@cknitt cknitt requested a review from IwanKaramazow March 30, 2022 13:40
Copy link
Contributor

@IwanKaramazow IwanKaramazow left a comment

Choose a reason for hiding this comment

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

@cknitt Thanks for the improvements, good to ship! Sorry for the delay.

@IwanKaramazow IwanKaramazow merged commit 5be5a55 into rescript-lang:master Apr 18, 2022
@cknitt cknitt deleted the print-punned-fields branch April 18, 2022 13:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants