Skip to content

Commit 12ab3d3

Browse files
committed
Handle comments in EEx between do and the first clause
Closes #11307.
1 parent 0bb774b commit 12ab3d3

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

lib/eex/lib/eex/compiler.ex

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ defmodule EEx.Compiler do
7575
:elixir_errors.erl_warn(start_line, state.file, message)
7676
end
7777

78-
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
78+
{rest, line, contents} =
79+
look_ahead_middle(rest, start_line, chars) || {rest, start_line, chars}
7980

8081
{contents, rest} =
8182
generate_buffer(
@@ -194,24 +195,20 @@ defmodule EEx.Compiler do
194195

195196
# Look middle expressions that immediately follow a start_expr
196197

197-
defp look_ahead_middle(
198-
[{:text, _, _, text}, {:middle_expr, line, _, _, chars} | rest] = tokens,
199-
start,
200-
contents
201-
) do
198+
defp look_ahead_middle([{:text, _, _, text} | rest], start, contents) do
202199
if only_spaces?(text) do
203-
{contents ++ text ++ chars, line, rest}
200+
look_ahead_middle(rest, start, contents ++ text)
204201
else
205-
{contents, start, tokens}
202+
nil
206203
end
207204
end
208205

209206
defp look_ahead_middle([{:middle_expr, line, _column, _, chars} | rest], _start, contents) do
210-
{contents ++ chars, line, rest}
207+
{rest, line, contents ++ chars}
211208
end
212209

213-
defp look_ahead_middle(tokens, start, contents) do
214-
{contents, start, tokens}
210+
defp look_ahead_middle(_tokens, _start, _contents) do
211+
nil
215212
end
216213

217214
defp only_spaces?(chars) do

lib/eex/test/eex/tokenizer_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ defmodule EEx.TokenizerTest do
110110
assert T.tokenize('foo <%# true do %>bar<%# end %>', 1, 1, @opts) == {:ok, exprs}
111111
end
112112

113+
test "EEx comments inside do-end" do
114+
exprs = [
115+
{:start_expr, 1, 1, '', ' if true do '},
116+
{:text, 1, 31, 'bar'},
117+
{:end_expr, 1, 34, [], ' end '},
118+
{:eof, 1, 43}
119+
]
120+
121+
assert T.tokenize('<% if true do %><%# comment %>bar<% end %>', 1, 1, @opts) == {:ok, exprs}
122+
123+
exprs = [
124+
{:start_expr, 1, 1, [], ' case true do '},
125+
{:middle_expr, 1, 33, '', ' true -> '},
126+
{:text, 1, 46, 'bar'},
127+
{:end_expr, 1, 49, [], ' end '},
128+
{:eof, 1, 58}
129+
]
130+
131+
assert T.tokenize('<% case true do %><%# comment %><% true -> %>bar<% end %>', 1, 1, @opts) ==
132+
{:ok, exprs}
133+
end
134+
113135
test "Elixir comments" do
114136
exprs = [
115137
{:text, 1, 1, 'foo '},

lib/eex/test/eex_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ defmodule EExTest do
198198
assert_eval("foo baz", "foo <%= if false do %>bar<% else %>baz<% end %>")
199199
end
200200

201+
test "embedded code with comments in do end" do
202+
assert_eval("foo bar", "foo <%= case true do %><%# comment %><% true -> %>bar<% end %>")
203+
204+
assert_eval(
205+
"foo\n\nbar\n",
206+
"foo\n<%= case true do %>\n<%# comment %>\n<% true -> %>\nbar\n<% end %>"
207+
)
208+
end
209+
201210
test "embedded code with nested do end" do
202211
assert_eval("foo bar", "foo <%= if true do %><%= if true do %>bar<% end %><% end %>")
203212
end

0 commit comments

Comments
 (0)