From aa409e9cfbd739258bec62957ac53c7f4d1a1ac4 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 7 Jun 2025 01:08:38 +0200 Subject: [PATCH 1/3] Do not report CRLF sequences in `Layout/TrailingWhitespace` rule --- spec/ameba/rule/layout/trailing_whitespace_spec.cr | 4 ++++ src/ameba/rule/layout/trailing_whitespace.cr | 1 + 2 files changed, 5 insertions(+) diff --git a/spec/ameba/rule/layout/trailing_whitespace_spec.cr b/spec/ameba/rule/layout/trailing_whitespace_spec.cr index 0d63c112c..414c723c2 100644 --- a/spec/ameba/rule/layout/trailing_whitespace_spec.cr +++ b/spec/ameba/rule/layout/trailing_whitespace_spec.cr @@ -8,6 +8,10 @@ module Ameba::Rule::Layout expect_no_issues subject, "no-whitespace" end + it "passes a line ends with trailing CRLF sequence" do + expect_no_issues subject, "no-whitespace\r\n" + end + it "fails if there is a line with trailing whitespace" do source = expect_issue subject, "whitespace at the end \n" \ diff --git a/src/ameba/rule/layout/trailing_whitespace.cr b/src/ameba/rule/layout/trailing_whitespace.cr index 83f6badd1..8e4a344f9 100644 --- a/src/ameba/rule/layout/trailing_whitespace.cr +++ b/src/ameba/rule/layout/trailing_whitespace.cr @@ -18,6 +18,7 @@ module Ameba::Rule::Layout def test(source) source.lines.each_with_index do |line, index| next unless ws_index = line =~ /\s+$/ + next if $0 == "\r" location = {index + 1, ws_index + 1} end_location = {index + 1, line.size} From 6c07b90f5bb97033184f03e01984356d2ed3f83c Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 7 Jun 2025 01:30:20 +0200 Subject: [PATCH 2/3] Make `Source#lines` CRLF-aware --- src/ameba/rule/layout/trailing_whitespace.cr | 1 - src/ameba/source.cr | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ameba/rule/layout/trailing_whitespace.cr b/src/ameba/rule/layout/trailing_whitespace.cr index 8e4a344f9..83f6badd1 100644 --- a/src/ameba/rule/layout/trailing_whitespace.cr +++ b/src/ameba/rule/layout/trailing_whitespace.cr @@ -18,7 +18,6 @@ module Ameba::Rule::Layout def test(source) source.lines.each_with_index do |line, index| next unless ws_index = line =~ /\s+$/ - next if $0 == "\r" location = {index + 1, ws_index + 1} end_location = {index + 1, line.size} diff --git a/src/ameba/source.cr b/src/ameba/source.cr index 13682a459..994bbe8ef 100644 --- a/src/ameba/source.cr +++ b/src/ameba/source.cr @@ -48,7 +48,7 @@ module Ameba # source = Ameba::Source.new "a = 1\nb = 2", path # source.lines # => ["a = 1", "b = 2"] # ``` - getter lines : Array(String) { code.split('\n') } + getter lines : Array(String) { code.split(/\r?\n/) } # Returns AST nodes constructed by `Crystal::Parser`. # From f3a9b9a4640585093a7592de10246da41a9c1e8f Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sat, 7 Jun 2025 01:42:48 +0200 Subject: [PATCH 3/3] Add additional test cases --- .../rule/layout/trailing_whitespace_spec.cr | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/ameba/rule/layout/trailing_whitespace_spec.cr b/spec/ameba/rule/layout/trailing_whitespace_spec.cr index 414c723c2..ff8580545 100644 --- a/spec/ameba/rule/layout/trailing_whitespace_spec.cr +++ b/spec/ameba/rule/layout/trailing_whitespace_spec.cr @@ -12,6 +12,24 @@ module Ameba::Rule::Layout expect_no_issues subject, "no-whitespace\r\n" end + it "fails if a line ends with trailing \\r character" do + source = expect_issue subject, <<-TEXT + carriage return at the end\r + # ^ error: Trailing whitespace detected + TEXT + + expect_correction source, "carriage return at the end" + end + + it "fails if there is a line with trailing tab" do + source = expect_issue subject, <<-TEXT + tab at the end\t + # ^ error: Trailing whitespace detected + TEXT + + expect_correction source, "tab at the end" + end + it "fails if there is a line with trailing whitespace" do source = expect_issue subject, "whitespace at the end \n" \