2021-01-24 19:26:11 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::Rule::Style
|
|
|
|
subject = VerboseBlock.new
|
|
|
|
|
|
|
|
describe VerboseBlock do
|
|
|
|
it "passes if there is no potential performance improvements" do
|
2021-10-27 22:51:00 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
(1..3).any?(&.odd?)
|
|
|
|
(1..3).join('.', &.to_s)
|
2021-02-04 21:24:29 +00:00
|
|
|
(1..3).each_with_index { |i, idx| i * idx }
|
2021-01-24 19:26:11 +00:00
|
|
|
(1..3).map { |i| typeof(i) }
|
|
|
|
(1..3).map { |i| i || 0 }
|
2021-02-04 21:24:29 +00:00
|
|
|
(1..3).map { |i| :foo }
|
|
|
|
(1..3).map { |i| :foo.to_s.split.join('.') }
|
|
|
|
(1..3).map { :foo }
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
2021-02-02 12:52:03 +00:00
|
|
|
it "passes if the block argument is used within the body" do
|
2021-10-27 22:51:00 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2021-02-02 12:52:03 +00:00
|
|
|
(1..3).map { |i| i * i }
|
|
|
|
(1..3).map { |j| j * j.to_i64 }
|
|
|
|
(1..3).map { |k| k.to_i64 * k }
|
|
|
|
(1..3).map { |l| l.to_i64 * l.to_i64 }
|
|
|
|
(1..3).map { |m| m.to_s[start: m.to_i64, count: 3]? }
|
|
|
|
(1..3).map { |n| n.to_s.split.map { |z| n.to_i * z.to_i }.join }
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2021-02-02 12:52:03 +00:00
|
|
|
end
|
|
|
|
|
2021-01-24 19:26:11 +00:00
|
|
|
it "reports if there is a call with a collapsible block" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
(1..3).any? { |i| i.odd? }
|
2021-10-27 22:51:00 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `any?(&.odd?)`
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).any?(&.odd?)
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is a call with an argument + collapsible block" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
(1..3).join('.') { |i| i.to_s }
|
2021-10-27 22:51:00 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `join('.', &.to_s)`
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).join('.', &.to_s)
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is a call with a collapsible block (with chained call)" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
(1..3).map { |i| i.to_s.split.reverse.join.strip }
|
2021-10-27 22:51:00 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `map(&.to_s.split.reverse.join.strip)`
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).map(&.to_s.split.reverse.join.strip)
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "properties" do
|
|
|
|
it "#exclude_calls_with_block" do
|
|
|
|
rule = VerboseBlock.new
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_calls_with_block = true
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
|
|
|
(1..3).in_groups_of(1) { |i| i.map(&.to_s) }
|
|
|
|
CRYSTAL
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_calls_with_block = false
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-10-27 22:51:00 +00:00
|
|
|
(1..3).in_groups_of(1) { |i| i.map(&.to_s) }
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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))
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "#exclude_multiple_line_blocks" do
|
2021-10-27 22:51:00 +00:00
|
|
|
rule = VerboseBlock.new
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_multiple_line_blocks = true
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
(1..3).any? do |i|
|
|
|
|
i.odd?
|
|
|
|
end
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_multiple_line_blocks = false
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-10-27 22:51:00 +00:00
|
|
|
(1..3).any? do |i|
|
|
|
|
# ^^^^^^^^^^^ error: Use short block notation instead: `any?(&.odd?)`
|
|
|
|
i.odd?
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).any?(&.odd?)
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
2021-02-03 01:00:49 +00:00
|
|
|
it "#exclude_prefix_operators" do
|
2021-10-27 22:51:00 +00:00
|
|
|
rule = VerboseBlock.new
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_prefix_operators = true
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
2021-02-03 01:00:49 +00:00
|
|
|
(1..3).sum { |i| +i }
|
|
|
|
(1..3).sum { |i| -i }
|
2021-03-02 11:40:46 +00:00
|
|
|
(1..3).sum { |i| ~i }
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_prefix_operators = false
|
|
|
|
rule.exclude_operators = false
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-10-27 22:51:00 +00:00
|
|
|
(1..3).sum { |i| +i }
|
|
|
|
# ^^^^^^^^^^^^^^ error: Use short block notation instead: `sum(&.+)`
|
|
|
|
(1..3).sum { |i| -i }
|
|
|
|
# ^^^^^^^^^^^^^^ error: Use short block notation instead: `sum(&.-)`
|
|
|
|
(1..3).sum { |i| ~i }
|
|
|
|
# ^^^^^^^^^^^^^^ error: Use short block notation instead: `sum(&.~)`
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).sum(&.+)
|
|
|
|
(1..3).sum(&.-)
|
|
|
|
(1..3).sum(&.~)
|
|
|
|
CRYSTAL
|
2021-02-03 01:00:49 +00:00
|
|
|
end
|
|
|
|
|
2021-01-24 19:26:11 +00:00
|
|
|
it "#exclude_operators" do
|
|
|
|
rule = VerboseBlock.new
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_operators = true
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
|
|
|
(1..3).sum { |i| i * 2 }
|
|
|
|
CRYSTAL
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_operators = false
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-10-27 22:51:00 +00:00
|
|
|
(1..3).sum { |i| i * 2 }
|
|
|
|
# ^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `sum(&.*(2))`
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).sum(&.*(2))
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "#exclude_setters" do
|
|
|
|
rule = VerboseBlock.new
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_setters = true
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
|
|
|
Char::Reader.new("abc").tap { |reader| reader.pos = 0 }
|
|
|
|
CRYSTAL
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.exclude_setters = false
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-10-27 22:51:00 +00:00
|
|
|
Char::Reader.new("abc").tap { |reader| reader.pos = 0 }
|
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `tap(&.pos=(0))`
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
Char::Reader.new("abc").tap(&.pos=(0))
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
|
2021-01-30 17:22:10 +00:00
|
|
|
it "#max_line_length" do
|
2021-10-27 22:51:00 +00:00
|
|
|
rule = VerboseBlock.new
|
|
|
|
rule.exclude_multiple_line_blocks = false
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.max_line_length = 60
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
2021-01-30 17:22:10 +00:00
|
|
|
(1..3).tap &.tap &.tap &.tap &.tap &.tap &.tap do |i|
|
|
|
|
i.to_s.reverse.strip.blank?
|
|
|
|
end
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.max_line_length = nil
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-10-27 22:51:00 +00:00
|
|
|
(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
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).tap &.tap &.tap &.tap &.tap &.tap &.tap(&.to_s.reverse.strip.blank?)
|
|
|
|
CRYSTAL
|
2021-01-30 17:22:10 +00:00
|
|
|
end
|
|
|
|
|
2021-01-24 19:26:11 +00:00
|
|
|
it "#max_length" do
|
|
|
|
rule = VerboseBlock.new
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.max_length = 30
|
|
|
|
expect_no_issues rule, <<-CRYSTAL
|
|
|
|
(1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? }
|
|
|
|
CRYSTAL
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-10-27 22:51:00 +00:00
|
|
|
rule.max_length = nil
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-10-27 22:51:00 +00:00
|
|
|
(1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `tap(&.to_s.split.reverse.join.strip.blank?)`
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
(1..3).tap(&.to_s.split.reverse.join.strip.blank?)
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "macro" do
|
|
|
|
it "reports in macro scope" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
{{ (1..3).any? { |i| i.odd? } }}
|
2021-10-27 22:51:00 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^ error: Use short block notation instead: `any?(&.odd?)`
|
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
{{ (1..3).any?(&.odd?) }}
|
|
|
|
CRYSTAL
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-03 12:39:35 +00:00
|
|
|
it "reports call args and named_args" do
|
2021-10-27 22:51:00 +00:00
|
|
|
rule = VerboseBlock.new
|
|
|
|
rule.exclude_operators = false
|
2022-12-19 17:03:11 +00:00
|
|
|
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s[start: 0.to_i64, count: 3]? }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[start: 0.to_i64, count: 3]?)`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s[0.to_i64, count: 3]? }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[0.to_i64, count: 3]?)`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s[0.to_i64, 3]? }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[0.to_i64, 3]?)`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s[start: 0.to_i64, count: 3] = "foo" }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[start: 0.to_i64, count: 3]=("foo"))`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s[0.to_i64, count: 3] = "foo" }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[0.to_i64, count: 3]=("foo"))`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s[0.to_i64, 3] = "foo" }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[0.to_i64, 3]=("foo"))`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s.camelcase(lower: true) }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.camelcase(lower: true))`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s.camelcase }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.camelcase)`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.to_s.gsub('_', '-') }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.gsub('_', '-'))`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.in?(*{1, 2, 3}, **{foo: :bar}) }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.in?(*{1, 2, 3}, **{foo: :bar}))`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).map { |i| i.in?(1, *foo, 3, **bar) }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.in?(1, *foo, 3, **bar))`
|
2021-02-03 12:39:35 +00:00
|
|
|
(1..3).join(separator: '.') { |i| i.to_s }
|
2021-11-09 17:05:57 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `join(separator: '.', &.to_s)`
|
2021-10-27 22:51:00 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
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
|
2021-02-03 12:39:35 +00:00
|
|
|
end
|
2021-01-24 19:26:11 +00:00
|
|
|
end
|
|
|
|
end
|