mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Autocorrect Stye/WhileTrue
This commit is contained in:
parent
571969265f
commit
72a3487bb6
2 changed files with 29 additions and 30 deletions
|
@ -1,43 +1,36 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
valid_source = <<-EOF
|
||||
a = 1
|
||||
loop do
|
||||
a += 1
|
||||
break if a > 5
|
||||
end
|
||||
EOF
|
||||
|
||||
invalid_source = <<-EOF
|
||||
a = 1
|
||||
while true
|
||||
a += 1
|
||||
break if a > 5
|
||||
end
|
||||
EOF
|
||||
|
||||
module Ameba::Rule::Style
|
||||
subject = WhileTrue.new
|
||||
|
||||
describe WhileTrue do
|
||||
it "passes if there is no `while true`" do
|
||||
source = Source.new valid_source
|
||||
subject.catch(source).should be_valid
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
a = 1
|
||||
loop do
|
||||
a += 1
|
||||
break if a > 5
|
||||
end
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "fails if there is `while true`" do
|
||||
source = Source.new invalid_source
|
||||
subject.catch(source).should_not be_valid
|
||||
end
|
||||
source = expect_issue subject, <<-CRYSTAL
|
||||
a = 1
|
||||
while true
|
||||
# ^^^^^^^^ error: While statement using true literal as condition
|
||||
a += 1
|
||||
break if a > 5
|
||||
end
|
||||
CRYSTAL
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
source = Source.new invalid_source, "source.cr"
|
||||
subject.catch(source).should_not be_valid
|
||||
|
||||
issue = source.issues.first
|
||||
issue.location.to_s.should eq "source.cr:2:1"
|
||||
issue.end_location.to_s.should eq "source.cr:5:3"
|
||||
issue.message.should eq "While statement using true literal as condition"
|
||||
expect_correction source, <<-CRYSTAL
|
||||
a = 1
|
||||
loop do
|
||||
a += 1
|
||||
break if a > 5
|
||||
end
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,7 +33,13 @@ module Ameba::Rule::Style
|
|||
MSG = "While statement using true literal as condition"
|
||||
|
||||
def test(source, node : Crystal::While)
|
||||
issue_for node, MSG if node.cond.true_literal?
|
||||
return unless node.cond.true_literal?
|
||||
return unless (location = node.location)
|
||||
return unless (end_location = node.cond.end_location)
|
||||
|
||||
issue_for node, MSG do |corrector|
|
||||
corrector.replace(location, end_location, "loop do")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue