Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/credo/check/readability/max_line_length.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Credo.Check.Readability.MaxLineLength do
max_length: 120,
ignore_definitions: true,
ignore_heredocs: true,
ignore_comments: false,
ignore_specs: false,
ignore_sigils: true,
ignore_strings: true,
Expand All @@ -26,6 +27,7 @@ defmodule Credo.Check.Readability.MaxLineLength do
max_length: "The maximum number of characters a line may consist of.",
ignore_definitions: "Set to `true` to ignore lines including function definitions.",
ignore_specs: "Set to `true` to ignore lines including `@spec`s.",
ignore_comments: "Set to `true` to ignore lines that are purely comments.",
ignore_sigils: "Set to `true` to ignore lines that are sigils, e.g. regular expressions.",
ignore_strings: "Set to `true` to ignore lines that are strings or in heredocs.",
ignore_urls: "Set to `true` to ignore lines that contain urls."
Expand All @@ -47,6 +49,7 @@ defmodule Credo.Check.Readability.MaxLineLength do
ignore_definitions = Params.get(params, :ignore_definitions, __MODULE__)

ignore_specs = Params.get(params, :ignore_specs, __MODULE__)
ignore_comments = Params.get(params, :ignore_comments, __MODULE__)
ignore_sigils = Params.get(params, :ignore_sigils, __MODULE__)
ignore_strings = Params.get(params, :ignore_strings, __MODULE__)
ignore_heredocs = Params.get(params, :ignore_heredocs, __MODULE__)
Expand Down Expand Up @@ -89,6 +92,13 @@ defmodule Credo.Check.Readability.MaxLineLength do
lines_for_comparison
end

lines_for_comparison =
if ignore_comments do
Enum.reject(lines_for_comparison, fn {_, line} -> line =~ ~r/^\s*#/ end)
else
lines_for_comparison
end

Enum.reduce(lines_for_comparison, [], fn {line_no, line_for_comparison}, issues ->
if String.length(line_for_comparison) > max_length do
if refute_issue?(line_no, definitions, ignore_definitions, specs, ignore_specs) do
Expand Down
32 changes: 32 additions & 0 deletions test/credo/check/readability/max_line_length_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@
|> refute_issues()
end

test "it should NOT report a violation if comments are excluded" do

Check failure on line 216 in test/credo/check/readability/max_line_length_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test it should NOT report a violation if comments are excluded (Credo.Check.Readability.MaxLineLengthTest)
~S'''
defmodule CredoSampleModule do
use ExUnit.Case

def some_fun do
# This is a very long comment line that should definitely be ignored if the ignore_comments parameter is set to true.
assert 1 + 1 == 2
end
end
'''
|> to_source_file
|> run_check(@described_check, max_length: 80, ignore_comments: true)
|> refute_issues()
end

#
# cases raising issues
#
Expand Down Expand Up @@ -247,6 +263,22 @@
|> assert_issue(%{column: 81, message: ~r/max is 80, was 112/})
end

test "it should report a violation if comments are NOT excluded" do
~S'''
defmodule CredoSampleModule do
use ExUnit.Case

def some_fun do
# This is a very long comment line that should definitely NOT be ignored if the ignore_comments parameter is set to false.
assert 1 + 1 == 2
end
end
'''
|> to_source_file
|> run_check(@described_check, max_length: 80, ignore_comments: false)
|> assert_issue()
end

test "it should report a violation /3" do
~S'''
def fun do
Expand Down
Loading