Skip to content

Commit

Permalink
Only return error for duplicate blocks if blocks are nested - fix (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell authored Dec 15, 2024
1 parent 7e9cabe commit 14930af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/template_compiler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -514,28 +514,28 @@ reset_block_ws(Ws) ->

%% @doc Extract all block definitions from the parse tree, returns deepest nested blocks first
find_blocks(Elements) ->
case find_blocks(Elements, {ok, [], []}) of
{ok, Acc, _Stack} ->
{ok, Acc};
find_blocks(Elements, [], []).

find_blocks([], Acc, _Stack) ->
{ok, Acc};
find_blocks([B|Bs], Acc, Stack) ->
case find_blocks(B, Acc, Stack) of
{ok, Acc1} ->
find_blocks(Bs, Acc1, Stack);
{error, _} = Error ->
Error
end.

find_blocks(_, {error, _} = Error) ->
Error;
find_blocks(List, Acc) when is_list(List) ->
lists:foldl(fun find_blocks/2, Acc, List);
find_blocks({block, {identifier, _Pos, Name}, Elements} = Block, {ok, Acc, Stack}) ->
end;
find_blocks({block, {identifier, _Pos, Name}, Elements} = Block, Acc, Stack) ->
case lists:member(Name, Stack) of
true ->
{error, {duplicate_block, Name}};
{error, {duplicate_nested_block, Name}};
false ->
Acc1 = [ Block | Acc ],
Stack1 = [ Name | Stack ],
find_blocks(Elements, {ok, Acc1, Stack1})
find_blocks(Elements, Acc1, Stack1)
end;
find_blocks(Element, Acc) ->
find_blocks(block_elements(Element), Acc).
find_blocks(Element, Acc, Stack) ->
find_blocks(block_elements(Element), Acc, Stack).

block_elements({for, _, Loop, Empty}) -> [Loop,Empty];
block_elements({'if', _, If, Else}) -> [If, Else];
Expand Down
2 changes: 1 addition & 1 deletion test/template_compiler_basic_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ hello_world_comment_test(_Config) ->
ok.

block_nested_error_test(_Config) ->
{error, {duplicate_block, <<"main">>}} = template_compiler:render("block_nested_error.tpl", #{}, [], undefined),
{error, {duplicate_nested_block, <<"main">>}} = template_compiler:render("block_nested_error.tpl", #{}, [], undefined),
ok.

block_render_test(_Config) ->
Expand Down

0 comments on commit 14930af

Please sign in to comment.