-
Notifications
You must be signed in to change notification settings - Fork 38
Put each jsx tag on own line #574
Put each jsx tag on own line #574
Conversation
|
This is awesome! Thanks a lot! Just wondering about performance when computing /cc @IwanKaramazow |
Indeed. |
Thank you gentlemen. Would you point me a little bit, how can I traverse expression children without creating a list? Just a function name, a very rough idea, or a hint is enough. Otherwise I can go wrong direction again :) |
|
I'd say iterate/recurse like |
So create a little helper function which is a recursive traversal. |
|
Thanks for pointing the direction. I think I’ve done it, not sure though :) Would you look at the newest commit? |
56cbf9d to
af9fa3b
Compare
cristianoc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great.
Some tiny style comments.
src/res_printer.ml
Outdated
| | Pexp_construct ({txt = Longident.Lident "[]"}, _) -> false | ||
| | Pexp_construct | ||
| ( {txt = Longident.Lident "::"}, | ||
| Some {pexp_desc = Pexp_tuple (hd :: [tail])} ) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this is normally written [hd, tail].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, this gives me:
File "src/res_printer.ml", line 106, characters 39-44:
106 | Some {pexp_desc = Pexp_tuple ([hd, _])} ) ->
^^^^^
Error: This pattern matches values of type 'a * 'b
but a pattern was expected which matches values of type
Parsetree.expression
My OCaml knowledge is not enough to understand why is it different. Perhaps, type inference fails somehow in the [hd, tail] case. Anyway, I’ve stolen this pattern from ParsetreeViewer.collectListExpressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry [hd; tail] not [hd, tail].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a final step, before and after: make bench ... If you are on macos, that is
I’m on Linux and have no access to a macOS machine, unfortunately. Would someone help with running
bench?
Will do once the PR is final.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry
[hd; tail]not[hd, tail]
Muscle memory :) Fixed it.
src/res_printer.ml
Outdated
| | Pexp_construct | ||
| ( {txt = Longident.Lident "::"}, | ||
| Some {pexp_desc = Pexp_tuple (hd :: [tail])} ) -> | ||
| hasNestedJsx hd || hasNestedJsx tail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be ParsetreeViewer.isJsxExpression hd ? and the next line do nothing? (and just return false)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Otherwise it behaves differently than before this commit (see the spread change in this commit which I think we do not want).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, now I understand what was the reason for spread behavior change. Fixed it, and the function has became even more simple and even non-rec. Thanks.
|
This is looking great. |
|
If you are on |
I’m on Linux and have no access to a macOS machine, unfortunately. Would someone help with running |
|
Hmm, and what do we actually do if we have a lot of children, but none is a JSX expression? Like <A> {a} {b} {c} {d} {e} </A>Maybe the logic would need to be something like: expand if a child is a JSX expression or if there is more than 1 child? What do you think @cristianoc? |
What does prettier do? |
<A>{a} {b} {c} {d}</A>;👇 <A>
{a} {b} {c} {d}
</A>;Do we want the same? It would made the algorithm slightly more complex (line separator between children and after/before tags differs) but doable nevertheless. |
|
I tried <A>{a}{b}{c}{d}</A>;in one of our JS projects using prettier and got <A>
{a}
{b}
{c}
{d}
</A>;Maybe our prettier config is different than yours. |
|
And <A>{a}</A>;stays unmodified for me. |
|
It is from their playground with default settings Replicating the behavior with your prettier config should be easy to implement |
|
These are significant in JS TSX, but not in ReScript, in ReScript we would need to explicitly have So I think we should look at the behavior without spaces. |
actually matches Prettier's behavior as far as I can see: |
|
Done. Now the behavior aligns with Prettier |
| <div className="meter text-4xl font-bold text-gray-600 mt-4"> | ||
| {values[0]->Js.Int.toString->string} {meterSuffix->Belt.Option.getWithDefault(null)} | ||
| {values[0]->Js.Int.toString->string} | ||
| {meterSuffix->Belt.Option.getWithDefault(null)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely an improvement in this example!
|
I think it can be done in one match without the "isConent": let hasNestedJsxOrMoreThanOneChild expr =
let rec loop inRecursion expr =
match expr.Parsetree.pexp_desc with
| Pexp_construct
({txt = Longident.Lident "::"}, Some {pexp_desc = Pexp_tuple [hd; tail]})
->
if inRecursion || ParsetreeViewer.isJsxExpression hd then true
else loop true tail
| _ -> false
in
loop false expr |
|
Yes, thanks. This is definitely more clear and I like the name improvement. |
cknitt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! 👍
Closes #570
This is my first lines of code in OCaml, so I’m not sure I’m doing it right 😺