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

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