Skip to content

Commit

Permalink
Ensure properly formatted triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Jan 9, 2024
1 parent bf0af1b commit 44587f2
Show file tree
Hide file tree
Showing 29 changed files with 147 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Credo.Check.Consistency.MultiAliasImportRequireUse do
issue_locations = @collector.find_locations_not_matching(expected, source_file)

Enum.map(issue_locations, fn line_no ->
format_issue(issue_meta, message: message_for(expected), line_no: line_no)
format_issue(issue_meta, message: message_for(expected), line_no: line_no, trigger: "")
end)
end

Expand Down
9 changes: 8 additions & 1 deletion lib/credo/check/consistency/tabs_or_spaces.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ defmodule Credo.Check.Consistency.TabsOrSpaces do
lines_with_issues = @collector.find_locations_not_matching(expected, source_file)

Enum.map(lines_with_issues, fn line_no ->
format_issue(issue_meta, message: message_for(expected), line_no: line_no)
format_issue(issue_meta,
message: message_for(expected),
line_no: line_no,
trigger: trigger_for(expected)
)
end)
end

defp trigger_for(:spaces = _expected), do: "\t"
defp trigger_for(:tabs = _expected), do: " "

defp message_for(:spaces = _expected) do
"File is using tabs while most of the files use spaces for indentation."
end
Expand Down
1 change: 1 addition & 0 deletions lib/credo/check/design/duplicated_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ defmodule Credo.Check.Design.DuplicatedCode do
issue_meta,
message: "Duplicate code found in #{filenames} (mass: #{node_mass}).",
line_no: line_no,
trigger: {:__no_trigger__},
severity: Severity.compute(1 + Enum.count(other_nodes), 1)
)
end
Expand Down
7 changes: 4 additions & 3 deletions lib/credo/check/readability/one_arity_function_in_pipe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ defmodule Credo.Check.Readability.OneArityFunctionInPipe do
end

defp traverse({:|>, _, [_, {name, meta, nil}]} = ast, issues, issue_meta) when is_atom(name) do
{ast, [issue(issue_meta, meta[:line]) | issues]}
{ast, [issue(issue_meta, meta[:line], name) | issues]}
end

defp traverse(ast, issues, _) do
{ast, issues}
end

defp issue(meta, line) do
defp issue(meta, line, name) do
format_issue(
meta,
message: "One arity functions should have parentheses in pipes",
line_no: line
line_no: line,
trigger: to_string(name)
)
end
end
8 changes: 4 additions & 4 deletions lib/credo/check/readability/parentheses_on_zero_arity_defs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ defmodule Credo.Check.Readability.ParenthesesOnZeroArityDefs do

cond do
parens? and not enforce_parens? ->
issues ++ [issue_for(issue_meta, line_no, :present)]
issues ++ [issue_for(issue_meta, name, line_no, :present)]

not parens? and enforce_parens? ->
issues ++ [issue_for(issue_meta, line_no, :missing)]
issues ++ [issue_for(issue_meta, name, line_no, :missing)]

true ->
issues
Expand All @@ -92,7 +92,7 @@ defmodule Credo.Check.Readability.ParenthesesOnZeroArityDefs do
Credo.Backports.String.slice(line, skip..-1)
end

defp issue_for(issue_meta, line_no, kind) do
defp issue_for(issue_meta, name, line_no, kind) do
message =
case kind do
:present ->
Expand All @@ -102,6 +102,6 @@ defmodule Credo.Check.Readability.ParenthesesOnZeroArityDefs do
"Use parentheses () when defining a function which has no arguments."
end

format_issue(issue_meta, message: message, line_no: line_no)
format_issue(issue_meta, message: message, line_no: line_no, trigger: to_string(name))
end
end
3 changes: 2 additions & 1 deletion lib/credo/check/readability/redundant_blank_lines.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ defmodule Credo.Check.Readability.RedundantBlankLines do
format_issue(
issue_meta,
message: "There should be no more than #{max_blank_lines} consecutive blank lines.",
line_no: line
line_no: line,
trigger: ""
)
end

Expand Down
8 changes: 6 additions & 2 deletions lib/credo/check/readability/separate_alias_require.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ defmodule Credo.Check.Readability.SeparateAliasRequire do
{ast, issues}
end

defp issue_for(issue_meta, line_no, type) do
format_issue(issue_meta, message: message(type), line_no: line_no)
defp issue_for(issue_meta, line_no, macro_name) do
format_issue(issue_meta,
message: message(macro_name),
line_no: line_no,
trigger: to_string(macro_name)
)
end

def message(:alias), do: "aliases should be consecutive within a file"
Expand Down
7 changes: 4 additions & 3 deletions lib/credo/check/readability/single_function_to_block_pipe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Credo.Check.Readability.SingleFunctionToBlockPipe do

defp issue({:|>, meta, [arg, {marker, _case_meta, _case_args}]}, issue_meta)
when marker in [:case, :if] do
if issue?(arg), do: issue_for(issue_meta, meta[:line]), else: nil
if issue?(arg), do: issue_for(issue_meta, meta[:line], marker), else: nil
end

defp issue(_, _), do: nil
Expand All @@ -70,11 +70,12 @@ defmodule Credo.Check.Readability.SingleFunctionToBlockPipe do

defp issue?(_), do: false

defp issue_for(issue_meta, line_no) do
defp issue_for(issue_meta, line_no, marker) do
format_issue(
issue_meta,
message: "Avoid single pipes to a block",
line_no: line_no
line_no: line_no,
trigger: to_string(marker)
)
end
end
3 changes: 2 additions & 1 deletion lib/credo/check/readability/trailing_blank_line.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ defmodule Credo.Check.Readability.TrailingBlankLine do
format_issue(
issue_meta,
message: "There should be a final \\n at the end of each file.",
line_no: line_no
line_no: line_no,
trigger: {:__no_trigger__}
)
end
end
3 changes: 2 additions & 1 deletion lib/credo/check/readability/with_custom_tagged_tuple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule Credo.Check.Readability.WithCustomTaggedTuple do

defp errors(source_file) do
{_ast, errors} = Macro.prewalk(Credo.Code.ast(source_file), MapSet.new(), &traverse/2)

Enum.sort_by(errors, &{&1.line, &1.column})
end

Expand All @@ -76,7 +77,7 @@ defmodule Credo.Check.Readability.WithCustomTaggedTuple do
issue_meta,
message: "Invalid usage of placeholder `#{inspect(error.placeholder)}` in with",
line_no: error.line,
column: error.column
trigger: inspect(error.placeholder)
)
end
end
3 changes: 2 additions & 1 deletion lib/credo/check/readability/with_single_clause.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ defmodule Credo.Check.Readability.WithSingleClause do
format_issue(issue_meta,
message:
"`with` contains only one <- clause and an `else` branch, consider using `case` instead",
line_no: line
line_no: line,
trigger: "with"
)
]

Expand Down
3 changes: 2 additions & 1 deletion lib/credo/check/refactor/apply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ defmodule Credo.Check.Refactor.Apply do
format_issue(
issue_meta,
message: "Avoid `apply/2` and `apply/3` when the number of arguments is known",
line_no: meta[:line]
line_no: meta[:line],
trigger: "apply"
)
end
end
12 changes: 6 additions & 6 deletions lib/credo/check/refactor/module_dependencies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule Credo.Check.Refactor.ModuleDependencies do
else
module_dependencies = get_dependencies(ast, dependency_namespaces)

issues_for_module(module_dependencies, max, issue_meta, meta)
issues_for_module(module_dependencies, max, issue_meta, meta, module_name)
end

{ast, issues ++ new_issues}
Expand All @@ -105,19 +105,19 @@ defmodule Credo.Check.Refactor.ModuleDependencies do
|> filter_namespaces(dependency_namespaces)
end

defp issues_for_module(deps, max_deps, issue_meta, meta) when length(deps) > max_deps do
defp issues_for_module(deps, max_deps, issue_meta, meta, module_name)
when length(deps) > max_deps do
[
format_issue(
issue_meta,
message: "Module has too many dependencies: #{length(deps)} (max is #{max_deps})",
trigger: deps,
line_no: meta[:line],
column_no: meta[:column]
trigger: module_name,
line_no: meta[:line]
)
]
end

defp issues_for_module(_, _, _, _), do: []
defp issues_for_module(_, _, _, _, _), do: []

# Resolve dependencies to full module names
defp with_fullnames(dependencies, aliases) do
Expand Down
2 changes: 1 addition & 1 deletion lib/credo/check/refactor/pass_async_in_test_cases.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Credo.Check.Refactor.PassAsyncInTestCases do
module_name = module_name_from(module_namespace)

if String.ends_with?(module_name, "Case") and !Keyword.has_key?(options, :async) do
{ast, issues ++ [issue_for(ast, meta[:line], issue_meta)]}
{ast, issues ++ [issue_for(module_name, meta[:line], issue_meta)]}
else
{ast, issues}
end
Expand Down
8 changes: 6 additions & 2 deletions lib/credo/check/refactor/redundant_with_clause_result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ defmodule Credo.Check.Refactor.RedundantWithClauseResult do
defp issue_for({clauses, body}, meta, issue_meta) do
case {redundant?(List.last(clauses), body), length(clauses)} do
{true, 1} ->
format_issue(issue_meta, message: @redundant_with, line_no: meta[:line])
format_issue(issue_meta, message: @redundant_with, line_no: meta[:line], trigger: "with")

{true, _length} ->
format_issue(issue_meta, message: @redundant_clause, line_no: meta[:line])
format_issue(issue_meta,
message: @redundant_clause,
line_no: meta[:line],
trigger: "with"
)

_else ->
nil
Expand Down
16 changes: 14 additions & 2 deletions lib/credo/check/refactor/with_clauses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,24 @@ defmodule Credo.Check.Refactor.WithClauses do
end

defp issue_if_not_starting_with_pattern_clause(_clauses, line, issue_meta) do
[format_issue(issue_meta, message: @message_first_clause_not_pattern, line_no: line)]
[
format_issue(issue_meta,
message: @message_first_clause_not_pattern,
line_no: line,
trigger: "with"
)
]
end

defp issue_if_not_ending_with_pattern_clause(clauses, line, issue_meta) do
if length(clauses) > 1 and not match?({:<-, _, _}, Enum.at(clauses, -1)) do
[format_issue(issue_meta, message: @message_last_clause_not_pattern, line_no: line)]
[
format_issue(issue_meta,
message: @message_last_clause_not_pattern,
line_no: line,
trigger: "with"
)
]
else
[]
end
Expand Down
13 changes: 7 additions & 6 deletions lib/credo/check/warning/lazy_logging.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,27 @@ defmodule Credo.Check.Warning.LazyLogging do
ignored_functions = Params.get(params, :ignore, __MODULE__)

unless Enum.member?(ignored_functions, fun_name) do
issue_for_call(arguments, meta, issue_meta)
issue_for_call(arguments, meta, fun_name, issue_meta)
end
end

defp issue_for_call([{:<<>>, _, [_ | _]} | _] = _args, meta, issue_meta) do
issue_for(issue_meta, meta[:line])
defp issue_for_call([{:<<>>, _, [_ | _]} | _] = _args, meta, fun_name, issue_meta) do
issue_for(issue_meta, meta[:line], fun_name)
end

defp issue_for_call(_args, _meta, _issue_meta) do
defp issue_for_call(_args, _meta, _trigger, _issue_meta) do
nil
end

defp logger_import?([{:__aliases__, _meta, [:Logger]}]), do: true
defp logger_import?(_), do: false

defp issue_for(issue_meta, line_no) do
defp issue_for(issue_meta, line_no, trigger) do
format_issue(
issue_meta,
message: "Prefer lazy Logger calls.",
line_no: line_no
line_no: line_no,
trigger: to_string(trigger)
)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ defmodule Credo.Check.Warning.MissedMetadataKeyInLoggerConfig do
defp logger_import?([{:__aliases__, _meta, [:Logger]}]), do: true
defp logger_import?(_), do: false

defp issue_for(issue_meta, line_no, missed_keys) do
defp issue_for(issue_meta, line_no, [trigger | _] = missed_keys) do
message = "Logger metadata key #{Enum.join(missed_keys, ", ")} not found in Logger config"

format_issue(issue_meta, message: message, line_no: line_no)
format_issue(issue_meta, message: message, line_no: line_no, trigger: to_string(trigger))
end

defp find_metadata_keys(params) do
Expand Down
4 changes: 3 additions & 1 deletion lib/credo/check/warning/wrong_test_file_extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ defmodule Credo.Check.Warning.WrongTestFileExtension do
defp issue_for(issue_meta) do
format_issue(
issue_meta,
message: "Test files should end with .exs"
message: "Test files should end with .exs",
line_no: 1,
trigger: ""
)
end

Expand Down
Loading

0 comments on commit 44587f2

Please sign in to comment.