davidwessman / syntax_tree-erb

Syntax Tree support for ERB
MIT License
21 stars 3 forks source link

[Formatting] Syntax issues with block capturing #76

Open rdimartino opened 5 months ago

rdimartino commented 5 months ago

There are several related examples of this (kind of weird) pattern using capture to extract part of a template as a string to be passed into another template:

  1. capture with do ... end for block and curly braces for template variable

    Rails renders the template, but stree format raises a syntax error

    ERB-snippet ```html <%= render 'shared/template', { greeting: capture do %>

    This block is captured as a string object and is available to the template as `greeting`

    The date and time is <%= Time.now %>

    <% end } do %>

    This content is displayed where the template `yield`s

    <% end %> ```
    Actual formatting ```html Error: syntax error, unexpected end-of-input, expecting '}' > 1 | <%= render 'shared/template', { greeting: capture do %> | ^ 2 |

    This block is captured as a string object and is available to the template as `greeting`

    3 |

    The date and time is <%= Time.now %>

    4 | <% end } do %> ```
  2. capture with { ... } for block and curly braces for template variable

    Rails renders the template, but stree format raises a syntax error

    ERB-snippet ```html <%= render 'shared/template', { greeting: capture { %>

    This block is captured as a string object and is available to the template as `greeting`

    The date and time is <%= Time.now %>

    <% } } do %>

    This content is displayed where the template `yield`s

    <% end %> ```
    Actual formatting Syntax error, even though Rails renders ```html Error: syntax error, unexpected end-of-input > 1 | <%= render 'shared/template', { greeting: capture { %> | ^ 2 |

    This block is captured as a string object and is available to the template as `greeting`

    3 |

    The date and time is <%= Time.now %>

    4 | <% } } do %> ```
  3. capture with { ... } for block and no braces for template variable

    Rails renders the template, but stree format raises a syntax error

    ERB-snippet ```html <%= render 'shared/template', greeting: capture { %>

    This block is captured as a string object and is available to the template as `greeting`

    The date and time is <%= Time.now %>

    <% } do %>

    This content is displayed where the template `yield`s

    <% end %> ```
    Actual formatting Syntax error, even though Rails renders ```html Error: syntax error, unexpected end-of-input > 1 | <%= render 'shared/template', greeting: capture { %> | ^ 2 |

    This block is captured as a string object and is available to the template as `greeting`

    3 |

    The date and time is <%= Time.now %>

    4 | <% } do %> ```
  4. capture with do ... end for block and no braces for template variable

    The opposite issue. Rails raises a syntax error, but stree format formats successfully

    ERB-snippet ```html <%= render 'shared/template', greeting: capture do %>

    This block is captured as a string object and is available to the template as `greeting`

    The date and time is <%= Time.now %>

    <% end do %>

    This content is displayed where the template `yield`s

    <% end %> ``` the Rails error is ```sh SyntaxError: //app/views/capture.html.erb:4: syntax error, unexpected `do', expecting `end' '.freeze; end do ^~ //app/views/capture.html.erb:8: syntax error, unexpected `end', expecting end-of-input end ^~~ ```
    Actual formatting "Successful" output of `bundle exec stree format --plugins=erb app/views/capture.html.erb`. ```html <%= render "shared/template", greeting: capture do %>

    This block is captured as a string object and is available to the template as `greeting`

    The date and time is <%= Time.now %>

    <% end do %>

    This content is displayed where the template `yield`s

    <% end %> ```
Expected formatting Not 100% sure what the expected format should be, but I sort of like this: ```html <%= render 'shared/template', { greeting: capture do %>

This block is captured as a string object and is available to the template as `greeting`

The date and time is <%= Time.now %>

<% end } do %>

This content is displayed where the template `yield`s

<% end %> ```

Comment

Had a couple examples of this in some legacy code I maintain. Also worth noting that this was hard to find at first because the syntax doesn't usually point to the correct location. It often (in my case) would point to line 3 even if the offending code was nowhere near there (e.g. around line 100+)

Versions

syntax_tree: 0.10.5 syntax_tree-erb: 6.1.1

davidwessman commented 5 months ago

Ah, have not added any handling for capture. Will investigate 🙂

Yeah, the error reference is hard - will see if I can make that better too.

Thanks for clear error reports 😊