2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2017-11-16 16:22:41 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Style
|
2017-11-16 16:22:41 +00:00
|
|
|
describe RedundantBegin do
|
|
|
|
subject = RedundantBegin.new
|
|
|
|
|
|
|
|
it "passes if there is no redundant begin blocks" do
|
2021-10-27 22:58:58 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method
|
|
|
|
do_something
|
|
|
|
rescue
|
|
|
|
do_something_else
|
|
|
|
end
|
|
|
|
|
|
|
|
def method
|
|
|
|
do_something
|
|
|
|
do_something_else
|
|
|
|
ensure
|
|
|
|
handle_something
|
|
|
|
end
|
|
|
|
|
|
|
|
def method
|
|
|
|
yield
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
|
|
|
def method; end
|
|
|
|
def method; a = 1; rescue; end
|
|
|
|
def method; begin; rescue; end; end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "passes if there is a correct begin block in a handler" do
|
2021-10-27 22:58:58 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def handler_and_expression
|
|
|
|
begin
|
|
|
|
open_file
|
|
|
|
rescue
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
do_some_stuff
|
|
|
|
end
|
|
|
|
|
|
|
|
def multiple_handlers
|
|
|
|
begin
|
|
|
|
begin1
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
begin2
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
do_something_else
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_and_begin
|
|
|
|
@result ||=
|
|
|
|
begin
|
|
|
|
do_something
|
|
|
|
do_something_else
|
|
|
|
returnit
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
|
|
|
def inner_handler
|
|
|
|
s = begin
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
|
|
|
def begin_and_expression
|
|
|
|
begin
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
end
|
|
|
|
expr
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a redundant begin block" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method(a : String) : String
|
|
|
|
begin
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
2017-11-16 16:22:41 +00:00
|
|
|
open_file
|
|
|
|
do_some_stuff
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method(a : String) : String
|
|
|
|
#{trailing_whitespace}
|
|
|
|
open_file
|
|
|
|
do_some_stuff
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a redundant begin block in a method without args" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method
|
|
|
|
begin
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
2017-11-16 16:22:41 +00:00
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method
|
|
|
|
#{trailing_whitespace}
|
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a redundant block in a method with return type" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method : String
|
|
|
|
begin
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
2017-11-16 16:22:41 +00:00
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method : String
|
|
|
|
#{trailing_whitespace}
|
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a redundant block in a method with multiple args" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method(a : String,
|
|
|
|
b : String)
|
|
|
|
begin
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
2017-11-16 16:22:41 +00:00
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method(a : String,
|
|
|
|
b : String)
|
|
|
|
#{trailing_whitespace}
|
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a redundant block in a method with multiple args" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method(a : String,
|
|
|
|
b : String
|
|
|
|
)
|
|
|
|
begin
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
2017-11-16 16:22:41 +00:00
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method(a : String,
|
|
|
|
b : String
|
|
|
|
)
|
|
|
|
#{trailing_whitespace}
|
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
2018-05-24 19:58:58 +00:00
|
|
|
it "doesn't report if there is an inner redundant block" do
|
2021-10-27 22:58:58 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method
|
|
|
|
begin
|
|
|
|
open_file
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a redundant block with yield" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method
|
|
|
|
begin
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
2017-11-16 16:22:41 +00:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method
|
|
|
|
#{trailing_whitespace}
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
close_file
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
2022-10-14 18:09:30 +00:00
|
|
|
it "fails if there is a redundant block with string with inner quotes" do
|
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
|
|
|
def method
|
|
|
|
begin
|
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
|
|
|
"'"
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method
|
|
|
|
#{trailing_whitespace}
|
|
|
|
"'"
|
|
|
|
rescue
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
|
|
|
end
|
|
|
|
|
2017-11-16 16:22:41 +00:00
|
|
|
it "fails if there is top level redundant block in a method" do
|
2021-11-16 21:30:33 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
def method
|
|
|
|
begin
|
2021-11-16 21:30:33 +00:00
|
|
|
# ^^^^^ error: Redundant `begin` block detected
|
2017-11-16 16:22:41 +00:00
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2021-11-16 21:30:33 +00:00
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
def method
|
|
|
|
#{trailing_whitespace}
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
#{trailing_whitespace}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
|
2018-09-22 18:58:38 +00:00
|
|
|
it "doesn't report if begin-end block in a proc literal" do
|
2021-10-27 22:58:58 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2018-09-22 18:58:38 +00:00
|
|
|
foo = ->{
|
|
|
|
begin
|
|
|
|
raise "Foo!"
|
|
|
|
rescue ex
|
|
|
|
pp ex
|
|
|
|
end
|
|
|
|
}
|
2021-10-27 22:58:58 +00:00
|
|
|
CRYSTAL
|
2018-09-22 18:58:38 +00:00
|
|
|
end
|
2017-11-16 16:22:41 +00:00
|
|
|
end
|
|
|
|
end
|