Skip to content

Commit

Permalink
Fix #939
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Jan 15, 2022
1 parent add89bf commit ce37d04
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lib/credo/cli/output/formatter/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ defmodule Credo.CLI.Output.Formatter.JSON do
term
end

def prepare_for_json([]), do: []
def prepare_for_json([h | t]), do: [prepare_for_json(h) | prepare_for_json(t)]
def prepare_for_json(term) when is_list(term), do: Enum.map(term, &prepare_for_json/1)

def prepare_for_json(%Regex{} = regex), do: inspect(regex)

Expand All @@ -44,12 +43,12 @@ defmodule Credo.CLI.Output.Formatter.JSON do
inspect(term)
end

defp prepare_key_for_json(k) when is_atom(k) or is_binary(k) or is_number(k) do
k
defp prepare_key_for_json(key) when is_atom(key) or is_binary(key) or is_number(key) do
key
end

defp prepare_key_for_json(k) do
inspect(k)
defp prepare_key_for_json(key) do
inspect(key)
end

def issue_to_json(
Expand Down
4 changes: 2 additions & 2 deletions test/credo/cli/output/formatter/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Credo.CLI.Output.Formatter.JsonTest do
JSON.print_map(%{"option" => ~r/foo/})
end

test "prepare_for_json/1 converts values invalid in json" do
test "prepare_for_json/1 converts keys and values invalid in json" do
assert JSON.prepare_for_json(%{
"bool" => true,
"list" => ["a", %{"a" => "b", "b" => ~r/foo/}],
Expand All @@ -28,7 +28,7 @@ defmodule Credo.CLI.Output.Formatter.JsonTest do
"string" => "a",
"tuple" => ["a", 2, "~r/foo/"],
:atom_key => 0,
"{\"tuple\", \"key\"}" => 1,
~q({"tuple", "key"}) => 1,
0 => 2
}
end
Expand Down

0 comments on commit ce37d04

Please # to comment.