Skip to content

Commit

Permalink
Fix Nesting issues with non-do blocks
Browse files Browse the repository at this point in the history
Refs #1029
  • Loading branch information
rrrene committed Mar 3, 2023
1 parent c5155c0 commit 5c25cd8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/credo/check/refactor/nesting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ defmodule Credo.Check.Refactor.Nesting do
defp find_depth(arguments, nest_list, line_no, trigger)
when is_list(arguments) do
arguments
|> Credo.Code.Block.do_block_for!()
|> List.wrap()
|> Enum.map(&find_depth(&1, nest_list, line_no, trigger))
|> Credo.Code.Block.all_blocks_for!()
|> Enum.flat_map(fn block ->
block
|> List.wrap()
|> Enum.map(&find_depth(&1, nest_list, line_no, trigger))
end)
|> Enum.sort()
|> List.last()
end
Expand Down
23 changes: 23 additions & 0 deletions test/credo/check/refactor/nesting_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,27 @@ defmodule Credo.Check.Refactor.NestingTest do
|> run_check(@described_check)
|> assert_issue()
end

test "it should report a violation /6" do
"""
defmodule CredoSampleModule do
defp foo() do
if true do
"flat"
else
if true do
if true do
if true do
"nested"
end
end
end
end
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> assert_issue()
end
end

0 comments on commit 5c25cd8

Please sign in to comment.