diff --git a/CODESMELL.md b/CODESMELL.md index 7a1b6cc..0fd75c0 100644 --- a/CODESMELL.md +++ b/CODESMELL.md @@ -350,9 +350,13 @@ --- -### CSM-021 — `cond` Where Pattern Matching Suffices -- **Files:** `lib/bds/ai.ex:62-70`, `lib/bds/scripting/api_docs.ex:1345-1398`, `lib/bds/scripting/api_docs.ex:1433-1447` -- **Fix:** Replace `cond do x == nil -> ...; true -> ... end` with multiple function-head clauses. +### ~~CSM-021 — `cond` Where Pattern Matching Suffices~~ ✅ FIXED +- **Fixed:** 2026-05-11 +- **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. --- diff --git a/lib/bds/ai.ex b/lib/bds/ai.ex index e049d3f..8e629d3 100644 --- a/lib/bds/ai.ex +++ b/lib/bds/ai.ex @@ -62,14 +62,12 @@ defmodule BDS.AI do model = get_setting("ai.#{kind_key}.model") encrypted_api_key = get_setting(encrypted_key("ai.#{kind_key}.api_key")) - cond do - is_nil(url) and is_nil(model) and is_nil(encrypted_api_key) -> - {:ok, nil} - - true -> - with {:ok, api_key} <- get_secret(encrypted_api_key, backend) do - {:ok, %{kind: kind, url: url, api_key: api_key, model: model}} - end + if is_nil(url) and is_nil(model) and is_nil(encrypted_api_key) do + {:ok, nil} + else + with {:ok, api_key} <- get_secret(encrypted_api_key, backend) do + {:ok, %{kind: kind, url: url, api_key: api_key, model: model}} + end end end diff --git a/lib/bds/scripting/api_docs.ex b/lib/bds/scripting/api_docs.ex index e89d5ba..16d94b2 100644 --- a/lib/bds/scripting/api_docs.ex +++ b/lib/bds/scripting/api_docs.ex @@ -1434,18 +1434,17 @@ defmodule BDS.Scripting.ApiDocs do |> Enum.uniq() end - defp example_response_value(returns) do - cond do - returns == "nil" -> - nil + defp example_response_value("nil"), do: 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))} - String.ends_with?(returns, "[]") -> + {_, true} -> [example_value_for_type(String.trim_trailing(returns, "[]"))] - true -> + _ -> example_value_for_type(returns) end end @@ -1475,10 +1474,10 @@ defmodule BDS.Scripting.ApiDocs do end defp example_field_value(type) do - cond do - String.contains?(type, " | nil") -> nil - String.ends_with?(type, "[]") -> [example_value_for_type(String.trim_trailing(type, "[]"))] - true -> example_value_for_type(type) + case {String.contains?(type, " | nil"), String.ends_with?(type, "[]")} do + {true, _} -> nil + {_, true} -> [example_value_for_type(String.trim_trailing(type, "[]"))] + _ -> example_value_for_type(type) end end diff --git a/test/bds/csm021_cond_pattern_match_test.exs b/test/bds/csm021_cond_pattern_match_test.exs new file mode 100644 index 0000000..a657751 --- /dev/null +++ b/test/bds/csm021_cond_pattern_match_test.exs @@ -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