-
Notifications
You must be signed in to change notification settings - Fork 414
/
trailing_white_space.ex
69 lines (58 loc) · 1.79 KB
/
trailing_white_space.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
defmodule Credo.Check.Readability.TrailingWhiteSpace do
use Credo.Check,
base_priority: :low,
tags: [:formatter],
param_defaults: [
ignore_strings: true
],
explanations: [
check: """
There should be no white-space (i.e. tabs, spaces) at the end of a line.
Most text editors provide a way to remove them automatically.
""",
params: [
ignore_strings: "Set to `false` to check lines that are strings or in heredocs"
]
]
alias Credo.Code
alias Credo.Code.Strings
alias Credo.Code.Heredocs
@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
issue_meta = IssueMeta.for(source_file, params)
ignore_strings = Params.get(params, :ignore_strings, __MODULE__)
source_file
|> to_lines(ignore_strings)
|> traverse_line([], issue_meta)
end
defp to_lines(source_file, true) do
source_file
|> Strings.replace_with_spaces(".", ".")
|> Heredocs.replace_with_spaces(".", ".", ".", source_file.filename)
|> Code.to_lines()
end
defp to_lines(source_file, false) do
SourceFile.lines(source_file)
end
defp traverse_line([{line_no, line} | tail], issues, issue_meta) do
issues =
case Regex.run(~r/\h+$/u, line, return: :index) do
[{column, line_length}] ->
[issue_for(issue_meta, line_no, column + 1, line_length) | issues]
nil ->
issues
end
traverse_line(tail, issues, issue_meta)
end
defp traverse_line([], issues, _issue_meta), do: issues
defp issue_for(issue_meta, line_no, column, line_length) do
format_issue(
issue_meta,
message: "There should be no trailing white-space at the end of a line.",
line_no: line_no,
column: column,
trigger: String.duplicate(" ", line_length)
)
end
end