tabs_or_spaces.ex (1656B)
1 defmodule Credo.Check.Consistency.TabsOrSpaces do 2 use Credo.Check, 3 run_on_all: true, 4 base_priority: :high, 5 tags: [:formatter], 6 param_defaults: [ 7 force: nil 8 ], 9 explanations: [ 10 check: """ 11 Tabs should be used consistently. 12 13 NOTE: This check does not verify the indentation depth, but checks whether 14 or not soft/hard tabs are used consistently across all source files. 15 16 It is very common to use 2 spaces wide soft-tabs, but that is not a strict 17 requirement and you can use hard-tabs if you like that better. 18 19 While this is not necessarily a concern for the correctness of your code, 20 you should use a consistent style throughout your codebase. 21 """, 22 params: [ 23 force: "Force a choice, values can be `:spaces` or `:tabs`." 24 ] 25 ] 26 27 @collector Credo.Check.Consistency.TabsOrSpaces.Collector 28 29 @doc false 30 @impl true 31 def run_on_all_source_files(exec, source_files, params) do 32 @collector.find_and_append_issues(source_files, exec, params, &issues_for/3) 33 end 34 35 defp issues_for(expected, source_file, params) do 36 issue_meta = IssueMeta.for(source_file, params) 37 38 lines_with_issues = @collector.find_locations_not_matching(expected, source_file) 39 40 Enum.map(lines_with_issues, fn line_no -> 41 format_issue(issue_meta, message: message_for(expected), line_no: line_no) 42 end) 43 end 44 45 defp message_for(:spaces = _expected) do 46 "File is using tabs while most of the files use spaces for indentation." 47 end 48 49 defp message_for(:tabs = _expected) do 50 "File is using spaces while most of the files use tabs for indentation." 51 end 52 end