Skip to content

Commit

Permalink
Raise on middle expression without start expression in EEx (elixir-la…
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosnk authored and josevalim committed Sep 28, 2017
1 parent 226838d commit 843bd0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/eex/lib/eex/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule EEx.Compiler do
generate_buffer(rest, state.engine.handle_begin(buffer), [wrapped | scope], %{state | line: line})
end

defp generate_buffer([{:middle_expr, line, modifier, chars} | t], buffer, scope, state) do
defp generate_buffer([{:middle_expr, line, modifier, chars} | t], buffer, [_ | _] = scope, state) do
message = "unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
"please remove \"#{modifier}\" accordingly"
:elixir_errors.warn line, state.file, message
Expand All @@ -62,6 +62,11 @@ defmodule EEx.Compiler do
# raise EEx.SyntaxError, message: message, file: state.file, line: line
end

defp generate_buffer([{:middle_expr, line, _, chars} | _], _buffer, [], state) do
raise EEx.SyntaxError, message: "unexpected middle of expression <%#{chars}%>",
file: state.file, line: line
end

defp generate_buffer([{:end_expr, line, '', chars} | rest], buffer, [current | _], state) do
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
tuples = Code.string_to_quoted!(wrapped, [line: state.start_line, file: state.file])
Expand Down
6 changes: 6 additions & 0 deletions lib/eex/test/eex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ defmodule EExTest do
end
end

test "when middle expression is found without a start expression" do
assert_raise EEx.SyntaxError, "nofile:1: unexpected middle of expression <% else %>", fn ->
EEx.compile_string "<% if true %> foo<% else %>bar<% end %>"
end
end

test "when end expression is found without a start expression" do
assert_raise EEx.SyntaxError, "nofile:1: unexpected end of expression <% end %>", fn ->
EEx.compile_string "foo <% end %>"
Expand Down

0 comments on commit 843bd0d

Please sign in to comment.