Skip to content
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

[Formatting] Syntax issues with block capturing #76

Open
rdimartino opened this issue Apr 22, 2024 · 1 comment
Open

[Formatting] Syntax issues with block capturing #76

rdimartino opened this issue Apr 22, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@rdimartino
Copy link

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
    <%= render 'shared/template', { greeting: capture do %>
        <p>This block is captured as a string object and is available to the template as `greeting`</p>
        <p>The date and time is <%= Time.now %></p>
    <% end } do %>
        <p>This content is displayed where the template `yield`s</p>
    <% end %>
    Actual formatting
    Error: syntax error, unexpected end-of-input, expecting '}'
    > 1 | <%= render 'shared/template', { greeting: capture do %>
        |                                              ^
      2 |     <p>This block is captured as a string object and is available to the template as `greeting`</p>
      3 |     <p>The date and time is <%= Time.now %></p>
      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
    <%= render 'shared/template', { greeting: capture { %>
        <p>This block is captured as a string object and is available to the template as `greeting`</p>
        <p>The date and time is <%= Time.now %></p>
    <% } } do %>
        <p>This content is displayed where the template `yield`s</p>
    <% end %>
    Actual formatting Syntax error, even though Rails renders
    Error: syntax error, unexpected end-of-input
    > 1 | <%= render 'shared/template', { greeting: capture { %>
        |                                                ^
      2 |     <p>This block is captured as a string object and is available to the template as `greeting`</p>
      3 |     <p>The date and time is <%= Time.now %></p>
      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
    <%= render 'shared/template', greeting: capture { %>
        <p>This block is captured as a string object and is available to the template as `greeting`</p>
        <p>The date and time is <%= Time.now %></p>
    <% } do %>
        <p>This content is displayed where the template `yield`s</p>
    <% end %>
    Actual formatting Syntax error, even though Rails renders
    Error: syntax error, unexpected end-of-input
    > 1 | <%= render 'shared/template', greeting: capture { %>
        |                                              ^
      2 |     <p>This block is captured as a string object and is available to the template as `greeting`</p>
      3 |     <p>The date and time is <%= Time.now %></p>
      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
    <%= render 'shared/template', greeting: capture do %>
        <p>This block is captured as a string object and is available to the template as `greeting`</p>
        <p>The date and time is <%= Time.now %></p>
    <% end do %>
        <p>This content is displayed where the template `yield`s</p>
    <% end %>

    the Rails error is

    SyntaxError: /<path>/app/views/capture.html.erb:4: syntax error, unexpected `do', expecting `end'
    '.freeze; end do 
                  ^~
    /<path>/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.

    <%= render "shared/template", greeting: capture do %>
      <p>This block is captured as a string object and is available to the template as `greeting`</p>
      <p>The date and time is <%= Time.now %></p>
    <% end do %>
    <p>This content is displayed where the template `yield`s</p>
    <% end %>
Expected formatting Not 100% sure what the expected format should be, but I sort of like this:
<%= render 'shared/template', {
        greeting: capture do %>
            <p>This block is captured as a string object and is available to the template as `greeting`</p>
            <p>The date and time is <%= Time.now %></p>
        <% end
    } do %>
    <p>This content is displayed where the template `yield`s</p>
<% 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

@rdimartino rdimartino added the bug Something isn't working label Apr 22, 2024
@rdimartino rdimartino mentioned this issue Apr 22, 2024
2 tasks
@davidwessman
Copy link
Owner

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 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants