mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
RedundantNext params
This commit is contained in:
parent
eb104a04f7
commit
0002c36fae
2 changed files with 103 additions and 14 deletions
|
@ -11,16 +11,6 @@ module Ameba::Rule::Style
|
|||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "reports if there is redundant next in the block" do
|
||||
s = Source.new %(
|
||||
block do |v|
|
||||
p v + 1
|
||||
next
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports if there is redundant next with argument in the block" do
|
||||
s = Source.new %(
|
||||
block do |v|
|
||||
|
@ -99,7 +89,7 @@ module Ameba::Rule::Style
|
|||
s = Source.new %(
|
||||
block do |a|
|
||||
a = 1
|
||||
next
|
||||
next a
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
|
@ -121,7 +111,7 @@ module Ameba::Rule::Style
|
|||
it "reports if there is redundant next in binary op" do
|
||||
s = Source.new %(
|
||||
block do |a|
|
||||
a && next
|
||||
a && next a
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
|
@ -136,7 +126,7 @@ module Ameba::Rule::Style
|
|||
block do |v|
|
||||
v + 1
|
||||
rescue e
|
||||
next if v > 0
|
||||
next v if v > 0
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
|
@ -150,7 +140,7 @@ module Ameba::Rule::Style
|
|||
next a + 2
|
||||
rescue Exception
|
||||
a + 2
|
||||
next
|
||||
next a
|
||||
end
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
|
@ -175,5 +165,55 @@ module Ameba::Rule::Style
|
|||
issue.end_location.to_s.should eq "source.cr:2:12"
|
||||
issue.message.should eq "Redundant `next` detected"
|
||||
end
|
||||
|
||||
context "properties" do
|
||||
context "#allow_multi_next=" do
|
||||
it "allows multi next statements by default" do
|
||||
s = Source.new %(
|
||||
block do |a, b|
|
||||
next a, b
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "allows to configure multi next statements" do
|
||||
s = Source.new %(
|
||||
block do |a, b|
|
||||
next a, b
|
||||
end
|
||||
)
|
||||
rule = Rule::Style::RedundantNext.new
|
||||
rule.allow_multi_next = false
|
||||
rule.catch(s).should_not be_valid
|
||||
s.issues.size.should eq 1
|
||||
s.issues.first.location.to_s.should eq ":2:3"
|
||||
end
|
||||
end
|
||||
|
||||
context "#allow_empty_next" do
|
||||
it "allows empty next statements by default" do
|
||||
s = Source.new %(
|
||||
block do
|
||||
next
|
||||
end
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "allows to configure empty next statements" do
|
||||
s = Source.new %(
|
||||
block do
|
||||
next
|
||||
end
|
||||
)
|
||||
rule = Rule::Style::RedundantNext.new
|
||||
rule.allow_empty_next = false
|
||||
rule.catch(s).should_not be_valid
|
||||
s.issues.size.should eq 1
|
||||
s.issues.first.location.to_s.should eq ":2:3"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -46,15 +46,61 @@ module Ameba::Rule::Style
|
|||
# end
|
||||
# ```
|
||||
#
|
||||
# ### Configuration params
|
||||
#
|
||||
# 1. *allow_multi_next*, default: true
|
||||
#
|
||||
# Allows end-user to configure whether to report or not the next statements
|
||||
# which yield tuple literals i.e.
|
||||
#
|
||||
# ```
|
||||
# block do
|
||||
# next a, b
|
||||
# end
|
||||
# ```
|
||||
#
|
||||
# If this param equals to `false`, the block above will be forced to be written as:
|
||||
#
|
||||
# ```
|
||||
# block do
|
||||
# {a, b}
|
||||
# end
|
||||
# ```
|
||||
#
|
||||
# 2. *allow_empty_next*, default: true
|
||||
#
|
||||
# Allows end-user to configure whether to report or not the next statements
|
||||
# without arguments. Sometimes such statements are used to yild the `nil` value explicitly.
|
||||
#
|
||||
# ```
|
||||
# block do
|
||||
# @foo = :empty
|
||||
# next
|
||||
# end
|
||||
# ```
|
||||
#
|
||||
# If this param equals to `false`, the block above will be forced to be written as:
|
||||
#
|
||||
# ```
|
||||
# block do
|
||||
# @foo = :empty
|
||||
# nil
|
||||
# end
|
||||
# ```
|
||||
#
|
||||
# ### YAML config example
|
||||
#
|
||||
# ```
|
||||
# Style/RedundantNext:
|
||||
# Enabled: true
|
||||
# AllowMultiNext: true
|
||||
# AllowEmptyNext: true
|
||||
# ```
|
||||
struct RedundantNext < Base
|
||||
properties do
|
||||
description "Reports redundant next expressions"
|
||||
allow_multi_next true
|
||||
allow_empty_next true
|
||||
end
|
||||
|
||||
MSG = "Redundant `next` detected"
|
||||
|
@ -68,6 +114,9 @@ module Ameba::Rule::Style
|
|||
end
|
||||
|
||||
def test(source, node : Crystal::Next, visitor : AST::RedundantControlExpressionVisitor)
|
||||
return if allow_multi_next && node.exp.is_a?(Crystal::TupleLiteral)
|
||||
return if allow_empty_next && (node.exp.nil? || node.exp.not_nil!.nop?)
|
||||
|
||||
source.try &.add_issue self, node, MSG
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue