space_in_parentheses.ex (2098B)
1 defmodule Credo.Check.Consistency.SpaceInParentheses do 2 use Credo.Check, 3 run_on_all: true, 4 base_priority: :high, 5 tags: [:formatter], 6 param_defaults: [allow_empty_enums: false], 7 explanations: [ 8 check: """ 9 Don't use spaces after `(`, `[`, and `{` or before `}`, `]`, and `)`. This is 10 the **preferred** way, although other styles are possible, as long as it is 11 applied consistently. 12 13 # preferred 14 15 Helper.format({1, true, 2}, :my_atom) 16 17 # also okay 18 19 Helper.format( { 1, true, 2 }, :my_atom ) 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 allow_empty_enums: 26 "Allows [], %{} and similar empty enum values to be used regardless of spacing throughout the codebase." 27 ] 28 ] 29 30 @collector Credo.Check.Consistency.SpaceInParentheses.Collector 31 32 @doc false 33 @impl true 34 def run_on_all_source_files(exec, source_files, params) do 35 @collector.find_and_append_issues(source_files, exec, params, &issues_for/3) 36 end 37 38 defp issues_for(expected, source_file, params) do 39 issue_meta = IssueMeta.for(source_file, params) 40 allow_empty_enums = Params.get(params, :allow_empty_enums, __MODULE__) 41 42 lines_with_issues = 43 @collector.find_locations_not_matching(expected, source_file, allow_empty_enums) 44 45 lines_with_issues 46 |> Enum.filter(&create_issue?(expected, &1[:trigger])) 47 |> Enum.map(fn location -> 48 format_issue(issue_meta, [{:message, message_for(expected)} | location]) 49 end) 50 end 51 52 # Don't create issues for `&Mod.fun/4` 53 defp create_issue?(:without_space, ", ]"), do: false 54 defp create_issue?(_expected, _trigger), do: true 55 56 defp message_for(:without_space = _expected) do 57 "There is no whitespace around parentheses/brackets most of the time, but here there is." 58 end 59 60 defp message_for(:with_space = _expected) do 61 "There is whitespace around parentheses/brackets most of the time, but here there is not." 62 end 63 end