Merge pull request #325 from FnControlOption/unless_else

Add autocorrect for `Style/UnlessElse`
This commit is contained in:
Sijawusz Pur Rahnama 2022-12-22 21:43:49 +01:00 committed by GitHub
commit 2d9e328d97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 18 deletions

View file

@ -13,7 +13,7 @@ module Ameba::Rule::Style
end
it "fails if unless has else" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
unless something
# ^^^^^^^^^^^^^^ error: Favour if over unless with else
:one
@ -21,6 +21,14 @@ module Ameba::Rule::Style
:two
end
CRYSTAL
expect_correction source, <<-CRYSTAL
if something
:two
else
:one
end
CRYSTAL
end
end
end

View file

@ -4,46 +4,70 @@ module Ameba
describe Source do
describe ".new" do
it "allows to create a source by code and path" do
s = Source.new("code", "path")
s.path.should eq "path"
s.code.should eq "code"
s.lines.should eq ["code"]
source = Source.new "code", "path"
source.path.should eq "path"
source.code.should eq "code"
source.lines.should eq ["code"]
end
end
describe "#fullpath" do
it "returns a relative path of the source" do
s = Source.new "", "./source_spec.cr"
s.fullpath.should contain "source_spec.cr"
source = Source.new "", "./source_spec.cr"
source.fullpath.should contain "source_spec.cr"
end
it "returns fullpath if path is blank" do
s = Source.new "", ""
s.fullpath.should_not be_nil
source = Source.new "", ""
source.fullpath.should_not be_nil
end
end
describe "#spec?" do
it "returns true if the source is a spec file" do
s = Source.new "", "./source_spec.cr"
s.spec?.should be_true
source = Source.new "", "./source_spec.cr"
source.spec?.should be_true
end
it "returns false if the source is not a spec file" do
s = Source.new "", "./source.cr"
s.spec?.should be_false
source = Source.new "", "./source.cr"
source.spec?.should be_false
end
end
describe "#matches_path?" do
it "returns true if source's path is matched" do
s = Source.new "", "source.cr"
s.matches_path?("source.cr").should be_true
source = Source.new "", "source.cr"
source.matches_path?("source.cr").should be_true
end
it "returns false if source's path is not matched" do
s = Source.new "", "source.cr"
s.matches_path?("new_source.cr").should be_false
source = Source.new "", "source.cr"
source.matches_path?("new_source.cr").should be_false
end
end
describe "#pos" do
it "works" do
source = Source.new <<-CRYSTAL
foo
bar
fizz
buzz
CRYSTAL
location = Crystal::Location.new("", 2, 1)
end_location = Crystal::Location.new("", 3, 4)
range = Range.new(
source.pos(location),
source.pos(end_location, end: true),
exclusive: true
)
source.code[range].should eq <<-CRYSTAL
bar
fizz
CRYSTAL
end
end
end