Skip to content

Commit

Permalink
Support metadata :ansi_color in MissedMetadataKeyInLoggerConfig
Browse files Browse the repository at this point in the history
Refs #1036
  • Loading branch information
rrrene committed Dec 17, 2023
1 parent 8551c2a commit edc5f06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/credo/check/warning/missed_metadata_key_in_logger_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfig do
]

@logger_functions ~w(alert critical debug emergency error info notice warn warning metadata log)a
@native_logger_metadata_keys [:ansi_color]

@doc false
@impl Credo.Check
Expand All @@ -53,39 +54,44 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfig do

defp do_run(source_file, params) do
issue_meta = IssueMeta.for(source_file, params)
metadata_keys = find_metadata_keys(params) ++ @native_logger_metadata_keys

state = {false, []}

{_, issues} = Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta), state)
{_, issues} = Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta, metadata_keys), state)

issues
end

defp traverse(
{{:., _, [{:__aliases__, _, [:Logger]}, fun_name]}, meta, arguments} = ast,
state,
issue_meta
issue_meta,
metadata_keys
)
when fun_name in @logger_functions do
issue = find_issue(fun_name, arguments, meta, issue_meta)
issue = issue_for_call(fun_name, arguments, meta, issue_meta, metadata_keys)

{ast, add_issue_to_state(state, issue)}
end

defp traverse(
{fun_name, meta, arguments} = ast,
{true, _issues} = state,
issue_meta
issue_meta,
metadata_keys
)
when fun_name in @logger_functions do
issue = find_issue(fun_name, arguments, meta, issue_meta)
issue = issue_for_call(fun_name, arguments, meta, issue_meta, metadata_keys)

{ast, add_issue_to_state(state, issue)}
end

defp traverse(
{:import, _meta, arguments} = ast,
{_module_contains_import?, issues} = state,
_issue_meta
_issue_meta,
_metadata_keys
) do
if logger_import?(arguments) do
{ast, {true, issues}}
Expand All @@ -94,7 +100,7 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfig do
end
end

defp traverse(ast, state, _issue_meta) do
defp traverse(ast, state, _issue_meta, _metadata_keys) do
{ast, state}
end

Expand All @@ -104,13 +110,6 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfig do
{module_contains_import?, [issue | issues]}
end

defp find_issue(fun_name, arguments, meta, issue_meta) do
params = IssueMeta.params(issue_meta)
metadata_keys = find_metadata_keys(params)

issue_for_call(fun_name, arguments, meta, issue_meta, metadata_keys)
end

defp issue_for_call(:metadata, [logger_metadata], meta, issue_meta, metadata_keys) do
issue_for_call(logger_metadata, meta, issue_meta, metadata_keys)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,17 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfigTest do
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report when Logger.log/2 is used with natively supported metadata" do
"""
defmodule CredoSampleModule do
def some_function(parameter1, parameter2) do
Logger.debug("test", ansi_color: :yellow)
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end
end

0 comments on commit edc5f06

Please sign in to comment.