Autocorrect various rules (#253)

This commit is contained in:
fn ⌃ ⌥ 2021-11-16 13:30:33 -08:00 committed by GitHub
parent 255d10f921
commit 63a6c73dc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 676 additions and 175 deletions

View file

@ -5,13 +5,15 @@ module Ameba::Rule::Layout
describe TrailingWhitespace do
it "passes if all lines do not have trailing whitespace" do
source = Source.new "no-whispace"
subject.catch(source).should be_valid
expect_no_issues subject, "no-whispace"
end
it "fails if there is a line with trailing whitespace" do
source = Source.new "whitespace at the end "
subject.catch(source).should_not be_valid
source = expect_issue subject,
"whitespace at the end \n" \
" # ^^ error: Trailing whitespace detected"
expect_correction source, "whitespace at the end"
end
it "reports rule, pos and message" do
@ -21,7 +23,7 @@ module Ameba::Rule::Layout
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:7"
issue.end_location.should be_nil
issue.end_location.to_s.should eq "source.cr:2:7"
issue.message.should eq "Trailing whitespace detected"
end
end

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Lint
describe ComparisonToBoolean do
it "passes if there is no comparison to boolean" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
a = true
if a
@ -32,34 +32,67 @@ module Ameba::Rule::Lint
when false
:not_ok
end
)
subject.catch(source).should be_valid
CRYSTAL
end
context "boolean on the right" do
it "fails if there is == comparison to boolean" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
if s.empty? == true
# ^^^^^^^^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
)
subject.catch(source).should_not be_valid
if s.empty? == false
# ^^^^^^^^^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
CRYSTAL
expect_correction source, <<-CRYSTAL
if s.empty?
:ok
end
if !s.empty?
:ok
end
CRYSTAL
end
it "fails if there is != comparison to boolean" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
if a != false
# ^^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
)
subject.catch(source).should_not be_valid
if a != true
# ^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
CRYSTAL
expect_correction source, <<-CRYSTAL
if a
:ok
end
if !a
:ok
end
CRYSTAL
end
it "fails if there is case comparison to boolean" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
a === true
)
subject.catch(source).should_not be_valid
# ^^^^^^^^ error: Comparison to a boolean is pointless
CRYSTAL
expect_correction source, <<-CRYSTAL
a
CRYSTAL
end
it "reports rule, pos and message" do
@ -75,28 +108,62 @@ module Ameba::Rule::Lint
context "boolean on the left" do
it "fails if there is == comparison to boolean" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
if true == s.empty?
# ^^^^^^^^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
)
subject.catch(source).should_not be_valid
if false == s.empty?
# ^^^^^^^^^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
CRYSTAL
expect_correction source, <<-CRYSTAL
if s.empty?
:ok
end
if !s.empty?
:ok
end
CRYSTAL
end
it "fails if there is != comparison to boolean" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
if false != a
# ^^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
)
subject.catch(source).should_not be_valid
if true != a
# ^^^^^^^^^ error: Comparison to a boolean is pointless
:ok
end
CRYSTAL
expect_correction source, <<-CRYSTAL
if a
:ok
end
if !a
:ok
end
CRYSTAL
end
it "fails if there is case comparison to boolean" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
true === a
)
subject.catch(source).should_not be_valid
# ^^^^^^^^ error: Comparison to a boolean is pointless
CRYSTAL
expect_correction source, <<-CRYSTAL
a
CRYSTAL
end
it "reports rule, pos and message" do

View file

@ -34,7 +34,7 @@ module Ameba::Rule::Metrics
issue = source.issues.first
issue.rule.should eq subject
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:10"
issue.end_location.to_s.should eq "source.cr:1:9"
issue.message.should eq "Cyclomatic complexity too high [8/5]"
end

View file

@ -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

View file

@ -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

View file

@ -83,10 +83,10 @@ module Ameba::Rule::Style
end
it "fails if there is a redundant begin block" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
def method(a : String) : String
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
begin
# ^^^^^ error: Redundant `begin` block detected
open_file
do_some_stuff
ensure
@ -94,61 +94,115 @@ module Ameba::Rule::Style
end
end
CRYSTAL
expect_correction source, <<-CRYSTAL
def method(a : String) : String
#{trailing_whitespace}
open_file
do_some_stuff
ensure
close_file
#{trailing_whitespace}
end
CRYSTAL
end
it "fails if there is a redundant begin block in a method without args" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
def method
# ^^^^^^^^ error: Redundant `begin` block detected
begin
# ^^^^^ error: Redundant `begin` block detected
open_file
ensure
close_file
end
end
CRYSTAL
expect_correction source, <<-CRYSTAL
def method
#{trailing_whitespace}
open_file
ensure
close_file
#{trailing_whitespace}
end
CRYSTAL
end
it "fails if there is a redundant block in a method with return type" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
def method : String
# ^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
begin
# ^^^^^ error: Redundant `begin` block detected
open_file
ensure
close_file
end
end
CRYSTAL
expect_correction source, <<-CRYSTAL
def method : String
#{trailing_whitespace}
open_file
ensure
close_file
#{trailing_whitespace}
end
CRYSTAL
end
it "fails if there is a redundant block in a method with multiple args" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
def method(a : String,
# ^^^^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
b : String)
begin
# ^^^^^ error: Redundant `begin` block detected
open_file
ensure
close_file
end
end
CRYSTAL
expect_correction source, <<-CRYSTAL
def method(a : String,
b : String)
#{trailing_whitespace}
open_file
ensure
close_file
#{trailing_whitespace}
end
CRYSTAL
end
it "fails if there is a redundant block in a method with multiple args" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
def method(a : String,
# ^^^^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
b : String
)
begin
# ^^^^^ error: Redundant `begin` block detected
open_file
ensure
close_file
end
end
CRYSTAL
expect_correction source, <<-CRYSTAL
def method(a : String,
b : String
)
#{trailing_whitespace}
open_file
ensure
close_file
#{trailing_whitespace}
end
CRYSTAL
end
it "doesn't report if there is an inner redundant block" do
@ -165,28 +219,47 @@ module Ameba::Rule::Style
end
it "fails if there is a redundant block with yield" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
def method
# ^^^^^^^^ error: Redundant `begin` block detected
begin
# ^^^^^ error: Redundant `begin` block detected
yield
ensure
close_file
end
end
CRYSTAL
expect_correction source, <<-CRYSTAL
def method
#{trailing_whitespace}
yield
ensure
close_file
#{trailing_whitespace}
end
CRYSTAL
end
it "fails if there is top level redundant block in a method" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
def method
# ^^^^^^^^ error: Redundant `begin` block detected
begin
# ^^^^^ error: Redundant `begin` block detected
a = 1
b = 2
end
end
CRYSTAL
expect_correction source, <<-CRYSTAL
def method
#{trailing_whitespace}
a = 1
b = 2
#{trailing_whitespace}
end
CRYSTAL
end
it "doesn't report if begin-end block in a proc literal" do
@ -215,8 +288,8 @@ module Ameba::Rule::Style
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:7:3"
issue.location.to_s.should eq "source.cr:2:3"
issue.end_location.to_s.should eq "source.cr:2:7"
issue.message.should eq "Redundant `begin` block detected"
end
end

View file

@ -29,24 +29,36 @@ module Ameba::Rule::Style
end
it "reports if there is a call with a collapsible block" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
(1..3).any? { |i| i.odd? }
# ^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `any?(&.odd?)`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).any?(&.odd?)
CRYSTAL
end
it "reports if there is a call with an argument + collapsible block" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
(1..3).join('.') { |i| i.to_s }
# ^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `join('.', &.to_s)`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).join('.', &.to_s)
CRYSTAL
end
it "reports if there is a call with a collapsible block (with chained call)" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
(1..3).map { |i| i.to_s.split.reverse.join.strip }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `map(&.to_s.split.reverse.join.strip)`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).map(&.to_s.split.reverse.join.strip)
CRYSTAL
end
context "properties" do
@ -57,9 +69,13 @@ module Ameba::Rule::Style
(1..3).in_groups_of(1) { |i| i.map(&.to_s) }
CRYSTAL
rule.exclude_calls_with_block = false
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
(1..3).in_groups_of(1) { |i| i.map(&.to_s) }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `in_groups_of(1, &.map {...})`
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `in_groups_of(1, &.map(&.to_s))`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).in_groups_of(1, &.map(&.to_s))
CRYSTAL
end
@ -72,12 +88,16 @@ module Ameba::Rule::Style
end
CRYSTAL
rule.exclude_multiple_line_blocks = false
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
(1..3).any? do |i|
# ^^^^^^^^^^^ error: Use short block notation instead: `any?(&.odd?)`
i.odd?
end
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).any?(&.odd?)
CRYSTAL
end
it "#exclude_prefix_operators" do
@ -90,7 +110,7 @@ module Ameba::Rule::Style
CRYSTAL
rule.exclude_prefix_operators = false
rule.exclude_operators = false
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
(1..3).sum { |i| +i }
# ^^^^^^^^^^^^^^ error: Use short block notation instead: `sum(&.+)`
(1..3).sum { |i| -i }
@ -98,6 +118,12 @@ module Ameba::Rule::Style
(1..3).sum { |i| ~i }
# ^^^^^^^^^^^^^^ error: Use short block notation instead: `sum(&.~)`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).sum(&.+)
(1..3).sum(&.-)
(1..3).sum(&.~)
CRYSTAL
end
it "#exclude_operators" do
@ -107,10 +133,14 @@ module Ameba::Rule::Style
(1..3).sum { |i| i * 2 }
CRYSTAL
rule.exclude_operators = false
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
(1..3).sum { |i| i * 2 }
# ^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `sum(&.*(2))`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).sum(&.*(2))
CRYSTAL
end
it "#exclude_setters" do
@ -120,10 +150,14 @@ module Ameba::Rule::Style
Char::Reader.new("abc").tap { |reader| reader.pos = 0 }
CRYSTAL
rule.exclude_setters = false
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
Char::Reader.new("abc").tap { |reader| reader.pos = 0 }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `tap(&.pos=(0))`
CRYSTAL
expect_correction source, <<-CRYSTAL
Char::Reader.new("abc").tap(&.pos=(0))
CRYSTAL
end
it "#max_line_length" do
@ -136,12 +170,16 @@ module Ameba::Rule::Style
end
CRYSTAL
rule.max_line_length = nil
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
(1..3).tap &.tap &.tap &.tap &.tap &.tap &.tap do |i|
# ^^^^^^^^^^ error: Use short block notation instead: `tap(&.to_s.reverse.strip.blank?)`
i.to_s.reverse.strip.blank?
end
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).tap &.tap &.tap &.tap &.tap &.tap &.tap(&.to_s.reverse.strip.blank?)
CRYSTAL
end
it "#max_length" do
@ -151,26 +189,34 @@ module Ameba::Rule::Style
(1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? }
CRYSTAL
rule.max_length = nil
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
(1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `tap(&.to_s.split.reverse.join.strip.blank?)`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).tap(&.to_s.split.reverse.join.strip.blank?)
CRYSTAL
end
end
context "macro" do
it "reports in macro scope" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
{{ (1..3).any? { |i| i.odd? } }}
# ^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `any?(&.odd?)`
CRYSTAL
expect_correction source, <<-CRYSTAL
{{ (1..3).any?(&.odd?) }}
CRYSTAL
end
end
it "reports call args and named_args" do
rule = VerboseBlock.new
rule.exclude_operators = false
expect_issue rule, <<-CRYSTAL
source = expect_issue rule, <<-CRYSTAL
(1..3).map { |i| i.to_s[start: 0.to_i64, count: 3]? }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[start: 0.to_i64, count: 3]?)`
(1..3).map { |i| i.to_s[0.to_i64, count: 3]? }
@ -196,6 +242,21 @@ module Ameba::Rule::Style
(1..3).join(separator: '.') { |i| i.to_s }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `join(separator: '.', &.to_s)`
CRYSTAL
expect_correction source, <<-CRYSTAL
(1..3).map(&.to_s.[start: 0.to_i64, count: 3]?)
(1..3).map(&.to_s.[0.to_i64, count: 3]?)
(1..3).map(&.to_s.[0.to_i64, 3]?)
(1..3).map(&.to_s.[start: 0.to_i64, count: 3]=("foo"))
(1..3).map(&.to_s.[0.to_i64, count: 3]=("foo"))
(1..3).map(&.to_s.[0.to_i64, 3]=("foo"))
(1..3).map(&.to_s.camelcase(lower: true))
(1..3).map(&.to_s.camelcase)
(1..3).map(&.to_s.gsub('_', '-'))
(1..3).map(&.in?(*{1, 2, 3}, **{foo: :bar}))
(1..3).map(&.in?(1, *foo, 3, **bar))
(1..3).join(separator: '.', &.to_s)
CRYSTAL
end
it "reports rule, pos and message" do