mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Autocorrect Layout/TrailingBlankLines
partially
This commit is contained in:
parent
e5fb0526e0
commit
573881cb8a
2 changed files with 17 additions and 13 deletions
|
@ -5,28 +5,26 @@ module Ameba::Rule::Layout
|
||||||
|
|
||||||
describe TrailingBlankLines do
|
describe TrailingBlankLines do
|
||||||
it "passes if there is a blank line at the end of a source" do
|
it "passes if there is a blank line at the end of a source" do
|
||||||
source = Source.new "a = 1\n", normalize: false
|
expect_no_issues subject, "a = 1\n", normalize: false
|
||||||
subject.catch(source).should be_valid
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "passes if source is empty" do
|
it "passes if source is empty" do
|
||||||
source = Source.new ""
|
expect_no_issues subject, ""
|
||||||
subject.catch(source).should be_valid
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is no blank lines at the end" do
|
it "fails if there is no blank lines at the end" do
|
||||||
source = Source.new "no-blankline"
|
expect_issue subject, "no-blankline # error: Trailing newline missing"
|
||||||
subject.catch(source).should_not be_valid
|
expect_correction "no-blankline\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there more then one blank line at the end of a source" do
|
it "fails if there more then one blank line at the end of a source" do
|
||||||
source = Source.new "a = 1\n \n", normalize: false
|
expect_issue subject, "a = 1\n \n # error: Excessive trailing newline detected", normalize: false
|
||||||
subject.catch(source).should_not be_valid
|
expect_no_corrections
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if last line is not blank" do
|
it "fails if last line is not blank" do
|
||||||
source = Source.new "\n\n\n puts 22", normalize: false
|
expect_issue subject, "\n\n\n puts 22 # error: Trailing newline missing", normalize: false
|
||||||
subject.catch(source).should_not be_valid
|
expect_correction "\n\n\n puts 22\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when unnecessary blank line has been detected" do
|
context "when unnecessary blank line has been detected" do
|
||||||
|
@ -36,7 +34,7 @@ module Ameba::Rule::Layout
|
||||||
|
|
||||||
issue = source.issues.first
|
issue = source.issues.first
|
||||||
issue.rule.should_not be_nil
|
issue.rule.should_not be_nil
|
||||||
issue.location.to_s.should eq "source.cr:2:1"
|
issue.location.to_s.should eq "source.cr:3:1"
|
||||||
issue.end_location.should be_nil
|
issue.end_location.should be_nil
|
||||||
issue.message.should eq "Excessive trailing newline detected"
|
issue.message.should eq "Excessive trailing newline detected"
|
||||||
end
|
end
|
||||||
|
@ -49,7 +47,7 @@ module Ameba::Rule::Layout
|
||||||
|
|
||||||
issue = source.issues.first
|
issue = source.issues.first
|
||||||
issue.rule.should_not be_nil
|
issue.rule.should_not be_nil
|
||||||
issue.location.to_s.should eq "source.cr:0:1"
|
issue.location.to_s.should eq "source.cr:1:1"
|
||||||
issue.end_location.should be_nil
|
issue.end_location.should be_nil
|
||||||
issue.message.should eq "Trailing newline missing"
|
issue.message.should eq "Trailing newline missing"
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,7 +25,13 @@ module Ameba::Rule::Layout
|
||||||
|
|
||||||
last_line_empty = last_source_line.empty?
|
last_line_empty = last_source_line.empty?
|
||||||
if source_lines_size >= 1 && (source_lines.last(2).join.blank? || !last_line_empty)
|
if source_lines_size >= 1 && (source_lines.last(2).join.blank? || !last_line_empty)
|
||||||
issue_for({source_lines_size - 1, 1}, last_line_empty ? MSG : MSG_FINAL_NEWLINE)
|
if last_line_empty
|
||||||
|
issue_for({source_lines_size, 1}, MSG)
|
||||||
|
else
|
||||||
|
issue_for({source_lines_size, 1}, MSG_FINAL_NEWLINE) do |corrector|
|
||||||
|
corrector.insert_before({source_lines_size + 1, 1}, '\n')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue