fix: replace cond blocks with pattern matching and case (CSM-021)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
10
CODESMELL.md
10
CODESMELL.md
@@ -350,9 +350,13 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### CSM-021 — `cond` Where Pattern Matching Suffices
|
### ~~CSM-021 — `cond` Where Pattern Matching Suffices~~ ✅ FIXED
|
||||||
- **Files:** `lib/bds/ai.ex:62-70`, `lib/bds/scripting/api_docs.ex:1345-1398`, `lib/bds/scripting/api_docs.ex:1433-1447`
|
- **Fixed:** 2026-05-11
|
||||||
- **Fix:** Replace `cond do x == nil -> ...; true -> ... end` with multiple function-head clauses.
|
- **What was done:**
|
||||||
|
- **`lib/bds/ai.ex`** — `get_endpoint/2`: Replaced `cond do is_nil(x) and ...; true -> ... end` with a simple `if/else` since there are only two branches.
|
||||||
|
- **`lib/bds/scripting/api_docs.ex`** — `example_response_value/1`: Extracted `"nil"` literal match into a separate function head. Replaced remaining `cond` with `case` on a tuple of guard results.
|
||||||
|
- **`lib/bds/scripting/api_docs.ex`** — `example_field_value/1`: Replaced `cond` with `case` on a tuple of `String.contains?`/`String.ends_with?` results.
|
||||||
|
- Added 2 source-level tests in `test/bds/csm021_cond_pattern_match_test.exs` asserting no `cond do` blocks remain in either file.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -62,14 +62,12 @@ defmodule BDS.AI do
|
|||||||
model = get_setting("ai.#{kind_key}.model")
|
model = get_setting("ai.#{kind_key}.model")
|
||||||
encrypted_api_key = get_setting(encrypted_key("ai.#{kind_key}.api_key"))
|
encrypted_api_key = get_setting(encrypted_key("ai.#{kind_key}.api_key"))
|
||||||
|
|
||||||
cond do
|
if is_nil(url) and is_nil(model) and is_nil(encrypted_api_key) do
|
||||||
is_nil(url) and is_nil(model) and is_nil(encrypted_api_key) ->
|
{:ok, nil}
|
||||||
{:ok, nil}
|
else
|
||||||
|
with {:ok, api_key} <- get_secret(encrypted_api_key, backend) do
|
||||||
true ->
|
{:ok, %{kind: kind, url: url, api_key: api_key, model: model}}
|
||||||
with {:ok, api_key} <- get_secret(encrypted_api_key, backend) do
|
end
|
||||||
{:ok, %{kind: kind, url: url, api_key: api_key, model: model}}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1434,18 +1434,17 @@ defmodule BDS.Scripting.ApiDocs do
|
|||||||
|> Enum.uniq()
|
|> Enum.uniq()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp example_response_value(returns) do
|
defp example_response_value("nil"), do: nil
|
||||||
cond do
|
|
||||||
returns == "nil" ->
|
|
||||||
nil
|
|
||||||
|
|
||||||
nullable_return?(returns) ->
|
defp example_response_value(returns) do
|
||||||
|
case {nullable_return?(returns), String.ends_with?(returns, "[]")} do
|
||||||
|
{true, _} ->
|
||||||
{:nullable, example_response_value(non_nil_return(returns))}
|
{:nullable, example_response_value(non_nil_return(returns))}
|
||||||
|
|
||||||
String.ends_with?(returns, "[]") ->
|
{_, true} ->
|
||||||
[example_value_for_type(String.trim_trailing(returns, "[]"))]
|
[example_value_for_type(String.trim_trailing(returns, "[]"))]
|
||||||
|
|
||||||
true ->
|
_ ->
|
||||||
example_value_for_type(returns)
|
example_value_for_type(returns)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1475,10 +1474,10 @@ defmodule BDS.Scripting.ApiDocs do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp example_field_value(type) do
|
defp example_field_value(type) do
|
||||||
cond do
|
case {String.contains?(type, " | nil"), String.ends_with?(type, "[]")} do
|
||||||
String.contains?(type, " | nil") -> nil
|
{true, _} -> nil
|
||||||
String.ends_with?(type, "[]") -> [example_value_for_type(String.trim_trailing(type, "[]"))]
|
{_, true} -> [example_value_for_type(String.trim_trailing(type, "[]"))]
|
||||||
true -> example_value_for_type(type)
|
_ -> example_value_for_type(type)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
15
test/bds/csm021_cond_pattern_match_test.exs
Normal file
15
test/bds/csm021_cond_pattern_match_test.exs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
defmodule BDS.CSM021CondPatternMatchTest do
|
||||||
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
|
describe "source-level: no cond blocks in fixed files" do
|
||||||
|
test "ai.ex has no cond do blocks" do
|
||||||
|
source = File.read!("lib/bds/ai.ex")
|
||||||
|
refute source =~ "cond do", "ai.ex should not contain cond do"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "api_docs.ex has no cond do blocks" do
|
||||||
|
source = File.read!("lib/bds/scripting/api_docs.ex")
|
||||||
|
refute source =~ "cond do", "api_docs.ex should not contain cond do"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user