Weird auto code formatting with pipe first operator

I’ve found some (seemingly) weird behavior with the code formatting in VS Code.

Small example

Here is an example.

let foo = (fn, value) => fn(value)
let bar = (value, fn) => fn(value)

Two simple functions…one takes a function and value, the other, a value then function. Now here is where it gets weird. Say I want to pass an anonymous function, and put comments in there. It works fine with bar, and the -> operator:

let _ = 11->bar(x => {
  let y = 100

  // Do the addition.
  x + y
})

But the code formatter does weird things using foo:

// This is what I type....
// 11->foo(x => {
//   let y = 100

//   // Do the addition.
//   x + y
// }, _)

// But when the code formatter goes, this is what it "formats" to:
let _ = 11->foo(
  x => {
    let y = 100

    x + y
  },
  // Do the addition.

  _,
)

Note the comment gets shoved out of the function.

Promises

Now the above is a just silly example, but this comes up with using promises. Here is the example on the docs page for promises

let myPromise = Js.Promise.make((~resolve, ~reject) => resolve(. 2))

myPromise->Js.Promise.then_(value => {
  Js.log(value)
  Js.Promise.resolve(value + 2)
}, _)->Js.Promise.then_(value => {
  Js.log(value)
  Js.Promise.resolve(value + 3)
}, _)->Js.Promise.catch(err => {
  Js.log2("Failure!!", err)
  Js.Promise.resolve(-2)
}, _)

Now, if I try to put some comments in those anonymous functions, you get the same (seemingly) weird code-fomatting thing going on.

Summary

Basically, I’m wondering if this is a bug or a feature…(i.e., maybe to encourage people not use complicated enough functions such that they need comments as anonymous functions?)

2 Likes

Hey! this is most likely not intended.

There’s a similar issue reported here:

It’s a detail that will be fixed eventually. You can create an explicit issue on the issue tracker in the meantime.

1 Like

Hi @Ryan, this is indeed a problem with the formatter. These things will be improved. I’ll take care of this and track issues in the syntax repo.

1 Like

Tracked here and here

2 Likes