mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Autocorrect various rules (#253)
This commit is contained in:
parent
255d10f921
commit
63a6c73dc0
24 changed files with 676 additions and 175 deletions
|
@ -5,35 +5,41 @@ module Ameba::Rule::Performance
|
|||
|
||||
describe AnyInsteadOfEmpty do
|
||||
it "passes if there is no potential performance improvements" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
[1, 2, 3].any?(&.zero?)
|
||||
[1, 2, 3].any?(String)
|
||||
[1, 2, 3].any?(1..3)
|
||||
[1, 2, 3].any? { |e| e > 1 }
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is any? call without a block nor argument" do
|
||||
source = Source.new %(
|
||||
source = expect_issue subject, <<-CRYSTAL
|
||||
[1, 2, 3].any?
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
# ^^^^^^^^^^^^ error: Use `!{...}.empty?` instead of `{...}.any?`
|
||||
CRYSTAL
|
||||
|
||||
expect_correction source, <<-CRYSTAL
|
||||
![1, 2, 3].empty?
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report if source is a spec" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL, "source_spec.cr"
|
||||
[1, 2, 3].any?
|
||||
), "source_spec.cr"
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "reports in macro scope" do
|
||||
source = Source.new %(
|
||||
source = expect_issue subject, <<-CRYSTAL
|
||||
{{ [1, 2, 3].any? }}
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
# ^^^^^^^^^^^^^^ error: Use `!{...}.empty?` instead of `{...}.any?`
|
||||
CRYSTAL
|
||||
|
||||
expect_correction source, <<-CRYSTAL
|
||||
{{ ![1, 2, 3].empty? }}
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -45,8 +51,8 @@ module Ameba::Rule::Performance
|
|||
issue = source.issues.first
|
||||
|
||||
issue.rule.should_not be_nil
|
||||
issue.location.to_s.should eq "source.cr:1:11"
|
||||
issue.end_location.to_s.should eq "source.cr:1:15"
|
||||
issue.location.to_s.should eq "source.cr:1:1"
|
||||
issue.end_location.to_s.should eq "source.cr:1:14"
|
||||
issue.message.should eq "Use `!{...}.empty?` instead of `{...}.any?`"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,46 +5,51 @@ module Ameba::Rule::Performance
|
|||
|
||||
describe ChainedCallWithNoBang do
|
||||
it "passes if there is no potential performance improvements" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
(1..3).select { |e| e > 1 }.sort!
|
||||
(1..3).select { |e| e > 1 }.sort_by!(&.itself)
|
||||
(1..3).select { |e| e > 1 }.uniq!
|
||||
(1..3).select { |e| e > 1 }.shuffle!
|
||||
(1..3).select { |e| e > 1 }.reverse!
|
||||
(1..3).select { |e| e > 1 }.rotate!
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is select followed by reverse" do
|
||||
source = Source.new %(
|
||||
source = expect_issue subject, <<-CRYSTAL
|
||||
[1, 2, 3].select { |e| e > 1 }.reverse
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
# ^^^^^^^ error: Use bang method variant `reverse!` after chained `select` call
|
||||
CRYSTAL
|
||||
|
||||
expect_correction source, <<-CRYSTAL
|
||||
[1, 2, 3].select { |e| e > 1 }.reverse!
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "does not report if source is a spec" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL, "source_spec.cr"
|
||||
[1, 2, 3].select { |e| e > 1 }.reverse
|
||||
), "source_spec.cr"
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
it "reports if there is select followed by reverse followed by other call" do
|
||||
source = Source.new %(
|
||||
source = expect_issue subject, <<-CRYSTAL
|
||||
[1, 2, 3].select { |e| e > 2 }.reverse.size
|
||||
)
|
||||
subject.catch(source).should_not be_valid
|
||||
# ^^^^^^^ error: Use bang method variant `reverse!` after chained `select` call
|
||||
CRYSTAL
|
||||
|
||||
expect_correction source, <<-CRYSTAL
|
||||
[1, 2, 3].select { |e| e > 2 }.reverse!.size
|
||||
CRYSTAL
|
||||
end
|
||||
|
||||
context "properties" do
|
||||
it "allows to configure `call_names`" do
|
||||
source = Source.new %(
|
||||
[1, 2, 3].select { |e| e > 2 }.reverse
|
||||
)
|
||||
rule = ChainedCallWithNoBang.new
|
||||
rule.call_names = %w(uniq)
|
||||
rule.catch(source).should be_valid
|
||||
expect_no_issues rule, <<-CRYSTAL
|
||||
[1, 2, 3].select { |e| e > 2 }.reverse
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -59,17 +64,16 @@ module Ameba::Rule::Performance
|
|||
issue = source.issues.first
|
||||
issue.rule.should_not be_nil
|
||||
issue.location.to_s.should eq "source.cr:1:32"
|
||||
issue.end_location.to_s.should eq "source.cr:1:39"
|
||||
issue.end_location.to_s.should eq "source.cr:1:38"
|
||||
|
||||
issue.message.should eq "Use bang method variant `reverse!` after chained `select` call"
|
||||
end
|
||||
|
||||
context "macro" do
|
||||
it "doesn't report in macro scope" do
|
||||
source = Source.new %(
|
||||
expect_no_issues subject, <<-CRYSTAL
|
||||
{{ [1, 2, 3].select { |e| e > 2 }.reverse }}
|
||||
)
|
||||
subject.catch(source).should be_valid
|
||||
CRYSTAL
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue