zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

unused_variable_names.ex (1902B)


      1 defmodule Credo.Check.Consistency.UnusedVariableNames do
      2   use Credo.Check,
      3     run_on_all: true,
      4     base_priority: :high,
      5     param_defaults: [
      6       force: nil
      7     ],
      8     explanations: [
      9       check: """
     10       Elixir allows us to use `_` as a name for variables that are not meant to be
     11       used. But it’s a common practice to give these variables meaningful names
     12       anyway (`_user` instead of `_`), but some people prefer to name them all anonymously (`_`).
     13 
     14       A single style should be present in the same codebase.
     15       """,
     16       params: [
     17         force: "Force a choice, values can be `:meaningful` or `:anonymous`."
     18       ]
     19     ]
     20 
     21   @collector Credo.Check.Consistency.UnusedVariableNames.Collector
     22 
     23   @doc false
     24   @impl true
     25   def run_on_all_source_files(exec, source_files, params) do
     26     @collector.find_and_append_issues(source_files, exec, params, &issues_for/3)
     27   end
     28 
     29   defp issues_for(expected, source_file, params) do
     30     issue_meta = IssueMeta.for(source_file, params)
     31     issue_locations = @collector.find_locations_not_matching(expected, source_file)
     32 
     33     Enum.map(issue_locations, fn location ->
     34       format_issue(issue_meta, [
     35         {:message, message_for(expected, location[:trigger])} | location
     36       ])
     37     end)
     38   end
     39 
     40   defp message_for(:meaningful, trigger) do
     41     message = """
     42     Unused variables should be named consistently.
     43     It seems your strategy is to give them meaningful names (eg. `_foo`)
     44     but `#{trigger}` does not follow that convention.
     45     """
     46 
     47     to_one_line(message)
     48   end
     49 
     50   defp message_for(:anonymous, trigger) do
     51     message = """
     52     Unused variables should be named consistently.
     53     It seems your strategy is to name them anonymously (ie. `_`)
     54     but `#{trigger}` does not follow that convention.
     55     """
     56 
     57     to_one_line(message)
     58   end
     59 
     60   defp to_one_line(str) do
     61     str
     62     |> String.split()
     63     |> Enum.join(" ")
     64   end
     65 end