mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: trailing blank lines
This commit is contained in:
parent
c8dcddea22
commit
b8eeac469a
3 changed files with 46 additions and 0 deletions
37
spec/ameba/rules/trailing_blank_lines_spec.cr
Normal file
37
spec/ameba/rules/trailing_blank_lines_spec.cr
Normal file
|
@ -0,0 +1,37 @@
|
|||
require "../../spec_helper"
|
||||
|
||||
module Ameba::Rules
|
||||
subject = TrailingBlankLines.new
|
||||
|
||||
describe TrailingBlankLines do
|
||||
it "passes if there is no blank lines at the end" do
|
||||
source = Source.new "", "no-blankline"
|
||||
subject.catch(source).valid?.should be_true
|
||||
end
|
||||
|
||||
it "fails if there is a blank line at the end of a source" do
|
||||
source = Source.new "", "a = 1\n \n "
|
||||
subject.catch(source).valid?.should be_false
|
||||
end
|
||||
|
||||
it "passes if source is empty" do
|
||||
source = Source.new "", ""
|
||||
subject.catch(source).valid?.should be_true
|
||||
end
|
||||
|
||||
it "passes if last line is not blank" do
|
||||
source = Source.new "", "\n\n\n puts 22"
|
||||
subject.catch(source).valid?.should be_true
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new "", "a = 1\n\n "
|
||||
subject.catch(source)
|
||||
|
||||
error = source.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 3
|
||||
error.message.should eq "Blank lines detected at the end of the file"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,7 @@
|
|||
module Ameba
|
||||
RULES = [
|
||||
Rules::LineLength,
|
||||
Rules::TrailingBlankLines,
|
||||
Rules::TrailingWhitespace,
|
||||
]
|
||||
|
||||
|
|
8
src/ameba/rules/trailing_blank_lines.cr
Normal file
8
src/ameba/rules/trailing_blank_lines.cr
Normal file
|
@ -0,0 +1,8 @@
|
|||
# A rule that disallows trailing blank lines at the end of the source file.
|
||||
|
||||
Ameba.rule TrailingBlankLines do |source|
|
||||
if source.lines.size > 1 && source.lines[-2, 2].join.strip == ""
|
||||
source.error self, source.lines.size,
|
||||
"Blank lines detected at the end of the file"
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue