zf

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

collector.ex (1496B)


      1 defmodule Credo.Check.Consistency.SpaceInParentheses.Collector do
      2   @moduledoc false
      3 
      4   use Credo.Check.Consistency.Collector
      5 
      6   @regex [
      7     with_space: ~r/[^\?]([\{\[\(]\s+\S|\S\s+[\)\]\}]])/,
      8     without_space: ~r/[^\?]([\{\[\(]\S|\S[\)\]\}])/,
      9     without_space_allow_empty_enums: ~r/[^\?](?!\{\}|\[\])([\{\[\(]\S|\S[\)\]\}])/
     10   ]
     11 
     12   def collect_matches(source_file, _params) do
     13     source_file
     14     |> Credo.Code.clean_charlists_strings_sigils_and_comments("")
     15     |> Credo.Code.to_lines()
     16     |> Enum.reduce(%{}, &spaces/2)
     17   end
     18 
     19   def find_locations_not_matching(expected, source_file, allow_empty_enums) do
     20     actual =
     21       case expected do
     22         :with_space when allow_empty_enums == true -> :without_space_allow_empty_enums
     23         :with_space -> :without_space
     24         :without_space -> :with_space
     25       end
     26 
     27     source_file
     28     |> Credo.Code.clean_charlists_strings_sigils_and_comments("")
     29     |> Credo.Code.to_lines()
     30     |> List.foldr([], &locate(actual, &1, &2))
     31   end
     32 
     33   defp spaces({_line_no, line}, acc) do
     34     Enum.reduce(@regex, acc, fn {kind_of_space, regex}, space_map ->
     35       if Regex.match?(regex, line) do
     36         Map.update(space_map, kind_of_space, 1, &(&1 + 1))
     37       else
     38         space_map
     39       end
     40     end)
     41   end
     42 
     43   defp locate(kind_of_space, {line_no, line}, locations) do
     44     case Regex.run(@regex[kind_of_space], line) do
     45       nil ->
     46         locations
     47 
     48       match ->
     49         [[trigger: Enum.at(match, 1), line_no: line_no] | locations]
     50     end
     51   end
     52 end