zf

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

parameter_pattern_matching.ex (1794B)


      1 defmodule Credo.Check.Consistency.ParameterPatternMatching 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       When capturing a parameter using pattern matching you can either put the parameter name before or after the value
     11       i.e.
     12 
     13           def parse({:ok, values} = pair)
     14 
     15       or
     16 
     17           def parse(pair = {:ok, values})
     18 
     19       Neither of these is better than the other, but it seems a good idea not to mix the two patterns in the same codebase.
     20 
     21       While this is not necessarily a concern for the correctness of your code,
     22       you should use a consistent style throughout your codebase.
     23       """,
     24       params: [
     25         force: "Force a choice, values can be `:after` or `:before`."
     26       ]
     27     ]
     28 
     29   @collector Credo.Check.Consistency.ParameterPatternMatching.Collector
     30 
     31   @doc false
     32   @impl true
     33   def run_on_all_source_files(exec, source_files, params) do
     34     @collector.find_and_append_issues(source_files, exec, params, &issues_for/3)
     35   end
     36 
     37   defp issues_for(expected, source_file, params) do
     38     issue_meta = IssueMeta.for(source_file, params)
     39 
     40     issue_locations = @collector.find_locations_not_matching(expected, source_file)
     41 
     42     Enum.map(issue_locations, fn location ->
     43       format_issue(issue_meta, [{:message, message_for(expected)} | location])
     44     end)
     45   end
     46 
     47   defp message_for(expected) do
     48     actual = @collector.actual_for(expected)
     49 
     50     "File has #{message_for_kind(actual)} while most of the files " <>
     51       "have #{message_for_kind(expected)} when naming parameter pattern matches"
     52   end
     53 
     54   defp message_for_kind(:after), do: "the variable name after the pattern"
     55   defp message_for_kind(:before), do: "the variable name before the pattern"
     56 end