<commit_before>defmodule Credo.Check.Consistency.ExceptionNames do
  @moduledoc """
  Exception names should end with a common suffix like "Error".

  Try to name your exception modules consistently:

      defmodule BadCodeError do
        defexception [:message]
      end

      defmodule ParserError do
        defexception [:message]
      end

  Inconsistent use should be avoided:

      defmodule BadHTTPResponse do
        defexception [:message]
      end

      defmodule HTTPHeaderException do
        defexception [:message]
      end

  While this is not necessarily a concern for the correctness of your code,
  you should use a consistent style throughout your codebase.
  """

  @explanation [check: @moduledoc]
  @code_patterns [
    Credo.Check.Consistency.ExceptionNames.PrefixAndSuffixCollector
  ]

  alias Credo.Check.Consistency.Helper
  alias Credo.Code.Module
  alias Credo.Code.Name

  use Credo.Check, run_on_all: true, base_priority: :high

  def run(source_files, params \\ []) when is_list(source_files) do
    {property_tuples, most_picked} =
      source_files
      |> Helper.run_code_patterns(@code_patterns, params)

    count =
      property_tuples
      |> Enum.reduce(0, fn({prop_list, _}, acc) ->
          acc + Enum.count(prop_list)
        end)

    if count == 2 do # we just found one prefix and one suffix
      []
    else
      {property_tuples, most_picked}
      |> Helper.add_issues_to_source_files(&check_for_issues/5, params)
    end
  end

  defp check_for_issues(_issue_meta, _actual_prop, nil, _picked_count, _total_count), do: nil
  defp check_for_issues(_issue_meta, [], _expected_prop, _picked_count, _total_count), do: nil
  defp check_for_issues(issue_meta, _actual_prop, expected_prop, _picked_count, _total_count) do
    source_file = IssueMeta.source_file(issue_meta)
    case expected_prop do
      {prefix, :prefix} ->
        find_exception_modules_without_prefix(source_file, prefix)
        |> issues_for_wrong(:prefix, issue_meta, prefix)
      {suffix, :suffix} ->
        find_exception_modules_without_suffix(source_file, suffix)
        |> issues_for_wrong(:suffix, issue_meta, suffix)
      _ -> nil
    end
  end
<commit_msg>Fix ExceptionName check<commit_after>defmodule Credo.Check.Consistency.ExceptionNames do
  @moduledoc """
  Exception names should end with a common suffix like "Error".

  Try to name your exception modules consistently:

      defmodule BadCodeError do
        defexception [:message]
      end

      defmodule ParserError do
        defexception [:message]
      end

  Inconsistent use should be avoided:

      defmodule BadHTTPResponse do
        defexception [:message]
      end

      defmodule HTTPHeaderException do
        defexception [:message]
      end

  While this is not necessarily a concern for the correctness of your code,
  you should use a consistent style throughout your codebase.
  """

  @explanation [check: @moduledoc]
  @code_patterns [
    Credo.Check.Consistency.ExceptionNames.PrefixAndSuffixCollector
  ]

  alias Credo.Check.Consistency.Helper
  alias Credo.Code.Module
  alias Credo.Code.Name

  use Credo.Check, run_on_all: true, base_priority: :high

  def run(source_files, params \\ []) when is_list(source_files) do
    {property_tuples, most_picked} =
      source_files
      |> Helper.run_code_patterns(@code_patterns, params)

    count =
      property_tuples
      |> Enum.reduce(0, fn({prop_list, _}, acc) ->
          acc + Enum.count(prop_list)
        end)

    if count == 2 do # we just found one prefix and one suffix
      source_files
    else
      {property_tuples, most_picked}
      |> Helper.add_issues_to_source_files(&check_for_issues/5, params)
    end
  end

  defp check_for_issues(_issue_meta, _actual_prop, nil, _picked_count, _total_count), do: nil
  defp check_for_issues(_issue_meta, [], _expected_prop, _picked_count, _total_count), do: nil
  defp check_for_issues(issue_meta, _actual_prop, expected_prop, _picked_count, _total_count) do
    source_file = IssueMeta.source_file(issue_meta)
    case expected_prop do
      {prefix, :prefix} ->
        find_exception_modules_without_prefix(source_file, prefix)
        |> issues_for_wrong(:prefix, issue_meta, prefix)
      {suffix, :suffix} ->
        find_exception_modules_without_suffix(source_file, suffix)
        |> issues_for_wrong(:suffix, issue_meta, suffix)
      _ -> nil
    end
  end

{'fraction_non_alphanumeric': 0.07820976491862568, 'fraction_numerical': 0.00135623869801085, 'mean_word_length': 3.368213228035538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3824771', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1361, 'n_words': 345}