Skip to content

Linebreak printer for pipe chains #6387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3585,12 +3585,25 @@ and printBinaryExpression ~state (expr : Parsetree.expression) cmtTbl =
Doc.rparen;
]
else
Doc.concat
[
leftPrinted;
printBinaryOperator ~inlineRhs:false operator;
rightPrinted;
]
match operator with
| "|." | "|.u" ->
Doc.concat
[
leftPrinted;
Doc.breakableGroup ~forceBreak:true
(Doc.concat
[
printBinaryOperator ~inlineRhs:false operator;
rightPrinted;
]);
]
| _ ->
Doc.concat
[
leftPrinted;
printBinaryOperator ~inlineRhs:false operator;
rightPrinted;
]
in

let doc =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ module Color: {

@send external map: (array<'a>, 'a => 'b) => array<'b> = "map"
@send external filter: (array<'a>, 'a => 'b) => array<'b> = "filter"
list{1, 2, 3}->map(a => a + 1)->filter(a => modulo(a, 2) == 0)->Js.log
list{1, 2, 3}
->map(a => a + 1)
->filter(a => modulo(a, 2) == 0)
->Js.log

type t
@new external make: unit => t = "DOMParser"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ let make = () => {
switch containerRef.current->Js.Nullable.toOption {
| Some(containerRef) =>
open Webapi.Dom
containerRef->Element.setScrollTop(containerRef->Element.scrollHeight->float_of_int)
containerRef->Element.setScrollTop(
containerRef
->Element.scrollHeight
->float_of_int,
)
| None => ()
}
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ let f = () => id

if isArray(children) {
// Scenario 1
let code = children->asStringArray->Js.Array2.joinWith("")
let code =
children
->asStringArray
->Js.Array2.joinWith("")
<InlineCode> {code->s} </InlineCode>
} else if isObject(children) {
// Scenario 2
Expand Down
22 changes: 18 additions & 4 deletions jscomp/syntax/tests/conversion/reason/expected/fastPipe.res.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
a->f(b, c)->g(d, e)
a
->f(b, c)
->g(d, e)

Element.querySelectorAll(selector, element)
->NodeList.toArray
->Array.keepMap(Element.ofNode)
->Array.getBy(node => node->Element.textContent === content)

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

5->doStuff(3, _, 7)

(event->target)["value"]

(Route.urlToRoute(url)->ChangeView->self).send
Route.urlToRoute(url)->ChangeView->self.send
(
Route.urlToRoute(url)
->ChangeView
->self
).send
Route.urlToRoute(url)
->ChangeView
->self.send

let aggregateTotal = (forecast, ~audienceType) =>
Js.Nullable.toOption(forecast["audiences"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ let make = (
{x, _}: props<string, ReactDOM.Ref.currentDomRef>,
ref: Js.Nullable.t<ReactDOM.Ref.currentDomRef>,
) => {
let _ = ref->Js.Nullable.toOption->Belt.Option.map(ReactDOM.Ref.domRef)
let _ =
ref
->Js.Nullable.toOption
->Belt.Option.map(ReactDOM.Ref.domRef)
React.string(x)
}
let make = React.forwardRef({
Expand Down
12 changes: 9 additions & 3 deletions jscomp/syntax/tests/printer/expr/expected/binary.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,16 @@ a && (b && (c && d))
(a && b) || c
a && (b || c)

x->(y->z)
x->(y->(z->w))
x
->(y
->z)
x
->(y
->(z->w))
x->(y && (w && z))
x->y->z
x
->y
->z

x |> y |> z
x |> (y |> z)
Expand Down
24 changes: 18 additions & 6 deletions jscomp/syntax/tests/printer/expr/expected/callback.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ let make = (arr, ~compare) => {
let tbl = data->Js.Array2.reduce((map, curr) => {
let (website, user) = curr
if map->Belt.Map.String.has(website) {
let set = map->Belt.Map.String.getExn(website)->Belt.Set.Int.add(user)
let set =
map
->Belt.Map.String.getExn(website)
->Belt.Set.Int.add(user)
map->Belt.Map.String.set(website, set)
} else {
let set = Belt.Set.Int.empty->Belt.Set.Int.add(user)
Expand All @@ -50,7 +53,10 @@ let _ = {
let tbl = data->Js.Array2.reduce((map, curr) => {
let (website, user) = curr
if map->Belt.Map.String.has(website) {
let set = map->Belt.Map.String.getExn(website)->Belt.Set.Int.add(user)
let set =
map
->Belt.Map.String.getExn(website)
->Belt.Set.Int.add(user)
map->Belt.Map.String.set(website, set)
} else {
let set = Belt.Set.Int.empty->Belt.Set.Int.add(user)
Expand All @@ -65,7 +71,10 @@ let trees =
combination->Belt.Array.reduceU(Nil, (. tree, curr) => tree->insert(curr))
)

let set = mapThatHasAVeryLongName->Belt.Map.String.getExn(website)->Belt.Set.Int.add(user)
let set =
mapThatHasAVeryLongName
->Belt.Map.String.getExn(website)
->Belt.Set.Int.add(user)

let add = (y: coll, e: key) =>
if List.exists(x => eq(x, e), y) {
Expand Down Expand Up @@ -215,13 +224,16 @@ let f = () => {
</div>
}

myPromise->Js.Promise.then_(value => {
myPromise
->Js.Promise.then_(value => {
Js.log(value)
Js.Promise.resolve(value + 2)
}, _)->Js.Promise.then_(value => {
}, _)
->Js.Promise.then_(value => {
Js.log(value)
Js.Promise.resolve(value + 3)
}, _)->Js.Promise.catch(err => {
}, _)
->Js.Promise.catch(err => {
Js.log2("Failure!!", err)
Js.Promise.resolve(-2)
}, _)
Expand Down
28 changes: 21 additions & 7 deletions jscomp/syntax/tests/printer/expr/expected/firstClassModule.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,29 @@ let x = @attr module(Foo)
let x = @attr module(Foo: FirstClass)

module(Teenager)[0]
module(Teenager)->age->Js.log
module(Teenager)
->age
->Js.log
module(Teenager)[0]->Js.log
module(Teenager)->age->isAdult
module(Teenager)
->age
->isAdult
? Js.log("has responsibilities")
: Js.log("can play in the playground")
module(Streets)[0]->isExpensive ? Js.log("big money") : Js.log("affordable")

let () = {
module(Teenager)->age->Js.log
module(Teenager)
->age
->Js.log
}
let () = {
module(Teenager)[0]
}
let () = {
module(Teenager)->age->isAdult
module(Teenager)
->age
->isAdult
? Js.log("has responsibilities")
: Js.log("can play in the playground")
}
Expand All @@ -34,14 +42,20 @@ let () = {
let a = 1
let b = 2
module(Teenager)[0]
module(Teenager)->age->Js.log
module(Teenager)
->age
->Js.log
}

let () = {
let a = 1
let b = 2
module(Teenager)->age->Js.log
module(Teenager)[0]->age->isAdult
module(Teenager)
->age
->Js.log
module(Teenager)[0]
->age
->isAdult
? Js.log("has responsibilities")
: Js.log("can play in the playground")
}
4 changes: 3 additions & 1 deletion jscomp/syntax/tests/printer/expr/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ module App = {
amount={
// ->BN.new_("86400")
// There are 86400 seconds in a day.
amount->BN.div(BN.new_("86400"))->BN.toString
amount
->BN.div(BN.new_("86400"))
->BN.toString
}
/>

Expand Down
4 changes: 3 additions & 1 deletion jscomp/syntax/tests/printer/other/expected/fatSlider.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ let make = (~min=50, ~max=250, ~meterSuffix=?) => {
/>
</div>
<div className="meter text-4xl font-bold text-gray-600 mt-4">
{values[0]->Js.Int.toString->string}
{values[0]
->Js.Int.toString
->string}
{meterSuffix->Belt.Option.getWithDefault(null)}
</div>
</div>
Expand Down
8 changes: 6 additions & 2 deletions jscomp/syntax/tests/printer/other/expected/nesting.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ let unitsCommands = state.units->Js.Array2.mapi((
),
coordinates: {x: sparksX, y: sparksY, z: 0.},
}
particlesToAdd->Js.Array2.push(spark)->ignore
particlesToAdd
->Js.Array2.push(spark)
->ignore

res->Js.Array2.push(Unit.CommandAttacked({damage: damage}))->ignore
res
->Js.Array2.push(Unit.CommandAttacked({damage: damage}))
->ignore
}
| _ => ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ let make = () => {
{options
->Belt.List.map(option =>
<option key={option->TimeSignature.toString}>
{option->TimeSignature.toString->string}
{option
->TimeSignature.toString
->string}
</option>
)
->Belt.List.toArray
Expand Down