marcoroth / phlexing

Simple ERB to Phlex converter
https://phlexing.fun
80 stars 9 forks source link

Stray `text` calls on method calls that render or capture for `Phlex::DeferredRender #91

Open hangsu opened 1 year ago

hangsu commented 1 year ago

Is your feature request related to a problem? Please describe. Phlexing can't tell when to leave out the text method call on methods that render

image

Expected:

render SomeComponent.new do |c|
  c.section { "hello world" }
end

Not sure what the solution might be. Phlexing obviously doesn't know if c.section is rendering, capturing for Phlex::DeferredRender or returning a String.

marcoroth commented 1 year ago

Hey @hangsu, thanks for bringing this up!

This is indeed something I haven't considered yet, and I'm actually not sure if and how we could support this.

Unless we add a rule that doesn't output the text call if the method which is being called takes in a block. But I'm not sure if that would be correct in 100% of the cases. Probably not, but it's probably more cases where that would be the more "desirable" outcome.

For the string inside the block we could do something similar as we do with regular HTML elements. Currently this:

<div>123</div>
<div><span>123</span>456</div>

gets converted to:

div { "123" }

div do
  span { "123" }
  text "456"
end

where we also omit the text call in the first example if there are other siblings. Maybe we could also do something similar for this case too.