Feature/Layout trailing lines, final newline checkup (#152)

This commit is contained in:
Vladislav Trotsenko 2020-05-05 16:52:24 +03:00 committed by GitHub
parent f1adf14527
commit bdeb6e3391
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 22 deletions

View file

@ -4,35 +4,55 @@ module Ameba::Rule::Layout
subject = TrailingBlankLines.new
describe TrailingBlankLines do
it "passes if there is no blank lines at the end" do
source = Source.new "no-blankline"
it "passes if there is a blank line at the end of a source" do
source = Source.new "a = 1\n", normalize: false
subject.catch(source).should be_valid
end
it "fails if there is a blank line at the end of a source" do
source = Source.new "a = 1\n \n ", normalize: false
subject.catch(source).should_not be_valid
end
it "passes if source is empty" do
source = Source.new ""
subject.catch(source).should be_valid
end
it "passes if last line is not blank" do
source = Source.new "\n\n\n puts 22", normalize: false
subject.catch(source).should be_valid
it "fails if there is no blank lines at the end" do
source = Source.new "no-blankline"
subject.catch(source).should_not be_valid
end
it "reports rule, pos and message" do
source = Source.new "a = 1\n\n ", "source.cr", normalize: false
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
subject.catch(source).should_not be_valid
end
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:1"
issue.end_location.should be_nil
issue.message.should eq "Blank lines detected at the end of the file"
it "fails if last line is not blank" do
source = Source.new "\n\n\n puts 22", normalize: false
subject.catch(source).should_not be_valid
end
context "when unnecessary blank line has been detected" do
it "reports rule, pos and message" do
source = Source.new "a = 1\n\n", "source.cr", normalize: false
subject.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:1"
issue.end_location.should be_nil
issue.message.should eq "Excessive trailing newline detected"
end
end
context "when final line has been missed" do
it "reports rule, pos and message" do
source = Source.new "a = 1", "source.cr", normalize: false
subject.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:0:1"
issue.end_location.should be_nil
issue.message.should eq "Trailing newline missing"
end
end
end
end

View file

@ -20,9 +20,8 @@ module Ameba
private def normalize_source(code, separator = "\n")
lines = code.split(separator)
# remove unneeded first and last blank lines if any
# remove unneeded first blank lines if any
lines.shift if lines[0].blank? && lines.size > 1
lines.pop if lines[-1].blank? && lines.size > 1
# find the minimum indentation
min_indent = lines.min_of do |line|