Skip to content

Commit a0e1604

Browse files
authored
In EEX Compiler, flatten expr list only once, not on every iteration (#15331)
1 parent 11f7d8f commit a0e1604

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

lib/eex/lib/eex/compiler.ex

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ defmodule EEx.Compiler do
303303
file: file,
304304
source: source,
305305
line: line,
306-
quoted: [],
306+
quoted: %{},
307307
parser_options: [indentation: indentation] ++ parser_options,
308308
indentation: indentation
309309
}
@@ -366,7 +366,7 @@ defmodule EEx.Compiler do
366366
rest,
367367
state.engine.handle_begin(buffer),
368368
[{contents, start_line, start_column} | scope],
369-
%{state | quoted: [], line: line}
369+
%{state | quoted: %{}, line: line}
370370
)
371371

372372
if mark == ~c"" and not match?({:=, _, [_, _]}, contents) do
@@ -410,7 +410,7 @@ defmodule EEx.Compiler do
410410
) do
411411
{wrapped, state} = wrap_expr(current, meta.line, buffer, chars, state)
412412
options = [file: state.file, line: line, column: column] ++ state.parser_options
413-
tuples = Code.string_to_quoted!(wrapped, options)
413+
tuples = Code.string_to_quoted!(:lists.flatten(wrapped), options)
414414
buffer = insert_quoted(tuples, state.quoted)
415415
{buffer, rest}
416416
end
@@ -426,7 +426,7 @@ defmodule EEx.Compiler do
426426

427427
defp generate_buffer([{:eof, _meta}], _buffer, [{content, line, column} | _scope], state) do
428428
message = "expected a closing '<% end %>' for block expression in EEx"
429-
expr_meta = non_whitespace_meta(content, line, column, state)
429+
expr_meta = non_whitespace_meta(:lists.flatten(content), line, column, state)
430430
syntax_error!(message, expr_meta, state)
431431
end
432432

@@ -443,10 +443,10 @@ defmodule EEx.Compiler do
443443

444444
defp wrap_expr(current, line, buffer, chars, state) do
445445
new_lines = List.duplicate(?\n, line - state.line)
446-
key = length(state.quoted)
447-
placeholder = ~c"__EEX__(" ++ Integer.to_charlist(key) ++ ~c");"
448-
count = current ++ placeholder ++ new_lines ++ chars
449-
new_state = %{state | quoted: [{key, state.engine.handle_end(buffer)} | state.quoted]}
446+
key = map_size(state.quoted)
447+
placeholder = [~c"__EEX__(", Integer.to_charlist(key), ~c");"]
448+
count = [current, placeholder, new_lines, chars]
449+
new_state = %{state | quoted: Map.put(state.quoted, key, state.engine.handle_end(buffer))}
450450

451451
{count, new_state}
452452
end
@@ -479,8 +479,7 @@ defmodule EEx.Compiler do
479479
# Changes placeholder to real expression
480480

481481
defp insert_quoted({:__EEX__, _, [key]}, quoted) do
482-
{^key, value} = List.keyfind(quoted, key, 0)
483-
value
482+
Map.fetch!(quoted, key)
484483
end
485484

486485
defp insert_quoted({left, line, right}, quoted) do

0 commit comments

Comments
 (0)