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

Commit 0957217

Browse files
authored
Printable attribute improvements (#504)
* printeable -> printable * Unify printable attribute checks and avoid unnecessary List construction
1 parent 61a9590 commit 0957217

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

src/res_parens.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,8 @@ type kind = Parenthesized | Braced of Location.t | Nothing
126126
Pexp_lazy _
127127
| Pexp_assert _
128128
} when isLhs -> Parenthesized
129-
| {Parsetree.pexp_attributes = (_::_) as attrs} ->
130-
begin match ParsetreeViewer.filterPrinteableAttributes attrs with
131-
| [] -> Nothing
132-
| _ -> Parenthesized
133-
end
134-
| _ -> Nothing
129+
| {Parsetree.pexp_attributes = attrs} ->
130+
if ParsetreeViewer.hasPrintableAttributes attrs then Parenthesized else Nothing
135131
end
136132

137133
let subBinaryExprOperand parentOperator childOperator =

src/res_parsetree_viewer.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,17 +451,16 @@ let shouldInlineRhsBinaryExpr rhs = match rhs.pexp_desc with
451451
| Pexp_record _ -> true
452452
| _ -> false
453453

454-
let filterPrinteableAttributes attrs =
455-
List.filter (fun attr -> match attr with
454+
let isPrintableAttribute attr =
455+
match attr with
456456
| ({Location.txt="bs" | "res.template" | "ns.ternary" | "ns.braces" | "ns.iflet" | "JSX"}, _) -> false
457457
| _ -> true
458-
) attrs
459458

460-
let partitionPrinteableAttributes attrs =
461-
List.partition (fun attr -> match attr with
462-
| ({Location.txt="bs" | "res.template"| "ns.ternary" | "ns.iflet" | "JSX"}, _) -> false
463-
| _ -> true
464-
) attrs
459+
let hasPrintableAttributes attrs = List.exists isPrintableAttribute attrs
460+
461+
let filterPrintableAttributes attrs = List.filter isPrintableAttribute attrs
462+
463+
let partitionPrintableAttributes attrs = List.partition isPrintableAttribute attrs
465464

466465
let requiresSpecialCallbackPrintingLastArg args =
467466
let rec loop args = match args with

src/res_parsetree_viewer.mli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ val hasJsxAttribute: Parsetree.attributes -> bool
8181

8282
val shouldIndentBinaryExpr: Parsetree.expression -> bool
8383
val shouldInlineRhsBinaryExpr: Parsetree.expression -> bool
84-
val filterPrinteableAttributes: Parsetree.attributes -> Parsetree.attributes
85-
val partitionPrinteableAttributes: Parsetree.attributes -> (Parsetree.attributes * Parsetree.attributes)
84+
val hasPrintableAttributes: Parsetree.attributes -> bool
85+
val filterPrintableAttributes: Parsetree.attributes -> Parsetree.attributes
86+
val partitionPrintableAttributes: Parsetree.attributes -> (Parsetree.attributes * Parsetree.attributes)
8687

8788
val requiresSpecialCallbackPrintingLastArg: (Asttypes.arg_label * Parsetree.expression) list -> bool
8889
val requiresSpecialCallbackPrintingFirstArg: (Asttypes.arg_label * Parsetree.expression) list -> bool

src/res_printer.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,7 +3479,7 @@ and printBinaryExpression (expr : Parsetree.expression) cmtTbl =
34793479
let leftPrinted = flatten ~isLhs:true left operator in
34803480
let rightPrinted =
34813481
let (_, rightAttrs) =
3482-
ParsetreeViewer.partitionPrinteableAttributes right.pexp_attributes
3482+
ParsetreeViewer.partitionPrintableAttributes right.pexp_attributes
34833483
in
34843484
let doc =
34853485
printExpressionWithComments
@@ -3491,11 +3491,11 @@ and printBinaryExpression (expr : Parsetree.expression) cmtTbl =
34913491
else
34923492
doc
34933493
in
3494-
let printeableAttrs =
3495-
ParsetreeViewer.filterPrinteableAttributes right.pexp_attributes
3494+
let printableAttrs =
3495+
ParsetreeViewer.filterPrintableAttributes right.pexp_attributes
34963496
in
3497-
let doc = Doc.concat [printAttributes printeableAttrs cmtTbl; doc] in
3498-
match printeableAttrs with
3497+
let doc = Doc.concat [printAttributes printableAttrs cmtTbl; doc] in
3498+
match printableAttrs with
34993499
| [] -> doc
35003500
| _ -> addParens doc
35013501
in

0 commit comments

Comments
 (0)