Skip to content

Commit

Permalink
fix: nested calling rule using raise
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis authored and rrrene committed Dec 11, 2023
1 parent c46f94a commit ba29ca1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/credo/check/readability/nested_function_calls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ defmodule Credo.Check.Readability.NestedFunctionCalls do
),
do: true

# Kernel.to_string() in a string interpolation
def cannot_be_in_pipeline?(
{{:., _, [Kernel, :to_string]}, meta, _} = ast,
_excluded_functions,
_excluded_argument_types
), do: meta[:from_interpolation]

# Elixir <= 1.8.0
# '__#{val}__' are compiled to String.to_charlist("__#{val}__")
# we want to consider these charlists a valid pipe chain start
Expand Down
24 changes: 24 additions & 0 deletions test/credo/check/readability/nested_function_calls_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,28 @@ defmodule Credo.Check.Readability.NestedFunctionCallsTest do
|> run_check(NestedFunctionCalls)
|> assert_issue()
end

test "it should NOT report any issues when Kernel.to_string() is called inside a string interpolation" do
~S"""
defmodule FeatureFlag.Adapters.Test do
def get(flag, context, fallback) do
args = {flag, context, fallback}
case :ets.lookup(@table, flag) do
[] ->
raise "No stubs found for #{inspect(args)}"
stubs ->
attempt_stubs(args, Enum.reverse(stubs))
end
end
defp attempt_stubs(args, []) do
raise "No stub found for args: #{inspect(args)}"
end
end
"""
|> to_source_file()
|> run_check(NestedFunctionCalls)
|> refute_issues()
end
end

0 comments on commit ba29ca1

Please sign in to comment.