Add corrector to the Style/IsANil rule

This commit is contained in:
Sijawusz Pur Rahnama 2022-12-08 13:58:35 +01:00
parent 071a8b7afb
commit 7d88455b7f
2 changed files with 13 additions and 3 deletions

View file

@ -14,17 +14,25 @@ module Ameba::Rule::Style
end
it "reports if there is a call to is_a?(Nil) without receiver" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
a = is_a?(Nil)
# ^^^ error: Use `nil?` instead of `is_a?(Nil)`
CRYSTAL
expect_correction source, <<-CRYSTAL
a = self.nil?
CRYSTAL
end
it "reports if there is a call to is_a?(Nil) with receiver" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
a.is_a?(Nil)
# ^^^ error: Use `nil?` instead of `is_a?(Nil)`
CRYSTAL
expect_correction source, <<-CRYSTAL
a.nil?
CRYSTAL
end
it "reports rule, location and message" do

View file

@ -34,7 +34,9 @@ module Ameba::Rule::Style
const = node.const
return unless path_named?(const, "Nil")
issue_for const, MSG
issue_for const, MSG do |corrector|
corrector.replace(node, "#{node.obj}.nil?")
end
end
end
end