Skip to content

generate error for unused "do" expression in EEx - #10566

Merged
josevalim merged 2 commits into
elixir-lang:masterfrom
superhawk610:feature/warn-on-unused-eex-do-block
Dec 14, 2020
Merged

generate error for unused "do" expression in EEx#10566
josevalim merged 2 commits into
elixir-lang:masterfrom
superhawk610:feature/warn-on-unused-eex-do-block

Conversation

@superhawk610

@superhawk610 superhawk610 commented Dec 13, 2020

Copy link
Copy Markdown
Contributor

This PR adds a new compilation error when EEx encounters a do expression without the <%= modifier, since this is nearly always unintentional. For example, take this block:

<% if @password %>
Temporary Password: <%= @password %>
<% else %>
Sign in with your existing password.
<% end %

At first glance, all seems well, and in the context of a full application it may take quite some time to work out that nothing is being output because you're missing the <%= opening tag. The only case I can think of where a do block is valid without the <%= opening tag is something like this:

<% if @condition do %>
  <% some_side_effect() %>
<% end %>

...but this could trivially be refactored to the (IMO) cleaner:

<% if @condition do
  some_side_effect()
end %>

Credit to @halostatue for suggesting this https://groups.google.com/g/elixir-lang-core/c/FKA9PancUm0/m/KzkT1vWuAAAJ

Closes #10536.

Comment thread lib/eex/lib/eex/compiler.ex Outdated
@fertapric

Copy link
Copy Markdown
Member

I would also include another test that checks that it does not warn for single expressions :)

@josevalim

Copy link
Copy Markdown
Member

@fertapric we don't need said test because otherwise the suite would be warning all the time. Generally speaking, we don't test that warnings are not emitted, because the tests would emit warnings otherwise and because it means we can't run tests concurrently.

Comment thread lib/eex/test/eex/smart_engine_test.exs Outdated
@josevalim
josevalim merged commit bfa34ec into elixir-lang:master Dec 14, 2020
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

EEx compiler should provide a warning if a block is used without <%=

3 participants