trailing_blank_line.ex (1077B)
1 defmodule Credo.Check.Readability.TrailingBlankLine do 2 use Credo.Check, 3 base_priority: :low, 4 tags: [:formatter], 5 explanations: [ 6 check: """ 7 Files should end in a trailing blank line. 8 9 This is mostly for historical reasons: every text file should end with a \\n, 10 or newline since this acts as `eol` or the end of the line character. 11 12 See also: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206 13 14 Most text editors ensure this "final newline" automatically. 15 """ 16 ] 17 18 @doc false 19 @impl true 20 def run(%SourceFile{} = source_file, params) do 21 issue_meta = IssueMeta.for(source_file, params) 22 23 {line_no, last_line} = 24 source_file 25 |> SourceFile.lines() 26 |> List.last() 27 28 if String.trim(last_line) == "" do 29 [] 30 else 31 [issue_for(issue_meta, line_no)] 32 end 33 end 34 35 defp issue_for(issue_meta, line_no) do 36 format_issue( 37 issue_meta, 38 message: "There should be a final \\n at the end of each file.", 39 line_no: line_no 40 ) 41 end 42 end