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

Commit c4544b4

Browse files
authored
Specialize the printing of the rhs of a record field assignment for o… (#718)
* Specialize the printing of the rhs of a record field assignment for optional values Fixes #714 Put parens around if-then-else. * Add functions.
1 parent b69c5e0 commit c4544b4

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
- Change the internal representation of props for the lowercase components to record. https://github.com/rescript-lang/syntax/pull/665
6262
- Change the payload of Pconst_char for type safety. https://github.com/rescript-lang/rescript-compiler/pull/5759
63+
- Specialize the printing of the rhs of a record field assignment for optional values `{x: ? e}` https://github.com/rescript-lang/syntax/issues/714
6364

6465
## ReScript 10.0
6566

src/res_parens.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ let expr expr =
1515
| {pexp_desc = Pexp_constraint _} -> Parenthesized
1616
| _ -> Nothing)
1717

18+
let exprRecordRowRhs e =
19+
let kind = expr e in
20+
match kind with
21+
| Nothing when Res_parsetree_viewer.hasOptionalAttribute e.pexp_attributes
22+
-> (
23+
match e.pexp_desc with
24+
| Pexp_ifthenelse _ | Pexp_fun _ -> Parenthesized
25+
| _ -> kind)
26+
| _ -> kind
27+
1828
let callExpr expr =
1929
let optBraces, _ = ParsetreeViewer.processBracesAttr expr in
2030
match optBraces with

src/res_parens.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ val includeModExpr : Parsetree.module_expr -> bool
3535
val arrowReturnTypExpr : Parsetree.core_type -> bool
3636

3737
val patternRecordRowRhs : Parsetree.pattern -> bool
38+
39+
val exprRecordRowRhs : Parsetree.expression -> kind

src/res_printer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5188,7 +5188,7 @@ and printExpressionRecordRow ~customLayout (lbl, expr) cmtTbl punningAllowed =
51885188
Doc.text ": ";
51895189
printOptionalLabel expr.pexp_attributes;
51905190
(let doc = printExpressionWithComments ~customLayout expr cmtTbl in
5191-
match Parens.expr expr with
5191+
match Parens.exprRecordRowRhs expr with
51925192
| Parens.Parenthesized -> addParens doc
51935193
| Braced braces -> printBraces doc expr braces
51945194
| Nothing -> doc);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ let _ = switch z {
9696
type tt = {x: int, y?: string}
9797

9898
type ttt = {x: int, y?: string}
99+
100+
let optParen = {x: 3, y: ?(someBool ? Some("") : None)}
101+
let optParen = {x: 3, y: ?(3 + 4)}
102+
let optParen = {x: 3, y: ?foo(bar)}
103+
let optParen = {x: 3, y: ?foo->bar}
104+
let optParen = {x: 3, y: ?(() => 3)}

tests/printer/expr/record.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,9 @@ let _ = switch z {
8686
type tt = {x:int, @ns.optional y: string}
8787

8888
type ttt = {x:int, y?: string}
89+
90+
let optParen = { x:3, y: ? (someBool ? Some("") : None) }
91+
let optParen = { x:3, y: ? (3+4) }
92+
let optParen = { x:3, y: ? (foo(bar)) }
93+
let optParen = { x:3, y: ? (foo->bar) }
94+
let optParen = { x:3, y: ? (()=>3) }

0 commit comments

Comments
 (0)