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

Commit aeb4640

Browse files
committed
Print optional labels with ?.
1 parent 8a0d5db commit aeb4640

File tree

7 files changed

+15
-5
lines changed

7 files changed

+15
-5
lines changed

src/res_core.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ let jsxAttr = (Location.mknoloc "JSX", Parsetree.PStr [])
161161
let uncurryAttr = (Location.mknoloc "bs", Parsetree.PStr [])
162162
let ternaryAttr = (Location.mknoloc "ns.ternary", Parsetree.PStr [])
163163
let ifLetAttr = (Location.mknoloc "ns.iflet", Parsetree.PStr [])
164-
let optionalAttr = (Location.mknoloc "optional", Parsetree.PStr [])
164+
let optionalAttr = (Location.mknoloc "ns.optional", Parsetree.PStr [])
165165

166166
let suppressFragileMatchWarningAttr =
167167
( Location.mknoloc "warning",

src/res_parsetree_viewer.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ let filterParsingAttrs attrs =
169169
| ( {
170170
Location.txt =
171171
( "ns.ternary" | "ns.braces" | "res.template" | "bs" | "ns.iflet"
172-
| "ns.namedArgLoc" );
172+
| "ns.namedArgLoc" | "ns.optional" );
173173
},
174174
_ ) ->
175175
false
@@ -304,6 +304,12 @@ let isIfLetExpr expr =
304304
true
305305
| _ -> false
306306

307+
let rec hasOptionalAttribute attrs =
308+
match attrs with
309+
| [] -> false
310+
| ({Location.txt = "ns.optional"}, _) :: _ -> true
311+
| _ :: attrs -> hasOptionalAttribute attrs
312+
307313
let hasAttributes attrs =
308314
List.exists
309315
(fun attr ->

src/res_parsetree_viewer.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ val filterFragileMatchAttributes : Parsetree.attributes -> Parsetree.attributes
8383

8484
val isJsxExpression : Parsetree.expression -> bool
8585
val hasJsxAttribute : Parsetree.attributes -> bool
86+
val hasOptionalAttribute: Parsetree.attributes -> bool
8687

8788
val shouldIndentBinaryExpr : Parsetree.expression -> bool
8889
val shouldInlineRhsBinaryExpr : Parsetree.expression -> bool

src/res_printer.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4658,6 +4658,9 @@ and printExpressionRecordRow (lbl, expr) cmtTbl punningAllowed =
46584658
[
46594659
printLidentPath lbl cmtTbl;
46604660
Doc.text ": ";
4661+
(if Res_parsetree_viewer.hasOptionalAttribute expr.pexp_attributes then
4662+
Doc.text "?"
4663+
else Doc.nil);
46614664
(let doc = printExpressionWithComments expr cmtTbl in
46624665
match Parens.expr expr with
46634666
| Parens.Parenthesized -> addParens doc

tests/parsing/grammar/expressions/expected/record.res.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let r = { (make () : myRecord) with foo = bar }
1515
let r =
1616
{
1717
x = ((None)[@optional ]);
18-
y = ((None)[@optional ]);
18+
y = ((None)[@ns.optional ]);
1919
z = (((None : tt))[@optional ])
2020
}
2121
let z name = { name = ((name)[@optional ]); x = 3 }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ let r = {
7474
}
7575
let r = {a /* a */, b /* b */}
7676

77-
let r = {x: @optional None, y: @optional None, z: (@optional None: tt)}
77+
let r = {x: @optional None, y: ?None, z: (@optional None: tt)}
7878

7979
let z = name => {@optional name, x: 3}
8080

tests/printer/expr/record.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let r = {
6464
}
6565
let r = {a /* a */, b /* b */}
6666

67-
let r = {x: @optional None, y: @optional None, z: @optional (None:tt)}
67+
let r = {x: @optional None, y: ?None, z: @optional (None:tt)}
6868

6969
let z = name => { name : @optional name, x: 3}
7070

0 commit comments

Comments
 (0)