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

Commit 61a9590

Browse files
Maximcristianoc
Maxim
authored andcommitted
Fix printing of parens in binary expressions with attributes
Parens should be printed: ```rescript (@doesNotRaise [])->Belt.Array.getExn(0) ``` Fixes #499
1 parent ae772b3 commit 61a9590

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

src/res_parens.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ 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
129134
| _ -> Nothing
130135
end
131136

src/res_parsetree_viewer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ let shouldInlineRhsBinaryExpr rhs = match rhs.pexp_desc with
453453

454454
let filterPrinteableAttributes attrs =
455455
List.filter (fun attr -> match attr with
456-
| ({Location.txt="bs" | "res.template" | "ns.ternary" | "ns.iflet" | "JSX"}, _) -> false
456+
| ({Location.txt="bs" | "res.template" | "ns.ternary" | "ns.braces" | "ns.iflet" | "JSX"}, _) -> false
457457
| _ -> true
458458
) attrs
459459

src/res_printer.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,10 @@ and printBinaryExpression (expr : Parsetree.expression) cmtTbl =
34943494
let printeableAttrs =
34953495
ParsetreeViewer.filterPrinteableAttributes right.pexp_attributes
34963496
in
3497-
Doc.concat [printAttributes printeableAttrs cmtTbl; doc]
3497+
let doc = Doc.concat [printAttributes printeableAttrs cmtTbl; doc] in
3498+
match printeableAttrs with
3499+
| [] -> doc
3500+
| _ -> addParens doc
34983501
in
34993502
let doc = Doc.concat [
35003503
leftPrinted;
@@ -4827,7 +4830,7 @@ and printRecordRow (lbl, expr) cmtTbl punningAllowed =
48274830
let doc = Doc.group (
48284831
match expr.pexp_desc with
48294832
| Pexp_ident({txt = Lident key; loc = keyLoc}) when (
4830-
punningAllowed &&
4833+
punningAllowed &&
48314834
Longident.last lbl.txt = key &&
48324835
lbl.loc.loc_start.pos_cnum == keyLoc.loc_start.pos_cnum
48334836
) ->

tests/conversion/reason/expected/fastPipe.res.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Element.querySelectorAll(selector, element)
55
->Array.keepMap(Element.ofNode)
66
->Array.getBy(node => node->Element.textContent === content)
77

8-
let x = @attr (@attr2 a->f(b)->c(d))
8+
let x = @attr ((@attr2 a)->f(b)->c(d))
99

1010
5->doStuff(3, _, 7)
1111

tests/printer/expr/binary.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,8 @@ let make = (~keycap) =>
400400
<Kbd keycap="Option" /> +
401401
<Kbd keycap="Shift" /> +
402402
<Kbd keycap />
403+
404+
// rescript-lang/syntax/issues/499
405+
(@doesNotRaise [])->Belt.Array.getExn(0)
406+
@doesNotRaise
407+
[]->Belt.Array.getExn(0)

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let x = a + b
22
let x = @attr (a + b)
3-
let x = @attr (@attr a + @attr b)
3+
let x = @attr ((@attr a) + (@attr b))
44
let x = a && b + c
55
let x = (a && b) + c
66
let x = (a && b) || c
@@ -182,13 +182,13 @@ let x = a && (b || c) && d
182182
let x = a && b + c
183183
let x = a && b + c && d
184184

185-
let x = a && @attr b && c
186-
let x = @attr a && @attr b && @attr c
185+
let x = a && (@attr b) && c
186+
let x = (@attr a) && (@attr b) && (@attr c)
187187
let x = a && @attr (b && c)
188-
let x = a && @attr (b && c) && @attr (d && e)
188+
let x = a && (@attr (b && c)) && @attr (d && e)
189189

190190
let x = a && @attr (x |> f(g))
191-
let x = a && @attr (x |> f(g)) && @attr (y |> f(h))
191+
let x = a && (@attr (x |> f(g))) && @attr (y |> f(h))
192192

193193
let x = a && a.b
194194
let x = a && x.y && g.h
@@ -516,3 +516,8 @@ React.useEffect4(() => {
516516
let make = (~keycap) => <Kbd keycap="Ctrl" /> + <Kbd keycap="Shift" /> + <Kbd keycap />
517517

518518
<Kbd keycap="Cmd" /> + <Kbd keycap="Option" /> + <Kbd keycap="Shift" /> + <Kbd keycap />
519+
520+
// rescript-lang/syntax/issues/499
521+
(@doesNotRaise [])->Belt.Array.getExn(0)
522+
@doesNotRaise
523+
[]->Belt.Array.getExn(0)

0 commit comments

Comments
 (0)