rubocop/no_fileutils_rmrf: Extend to cover FileUtils#rm_f too
This commit is contained in:
parent
e6976ae3d1
commit
f4e4808553
@ -4,21 +4,22 @@
|
||||
module RuboCop
|
||||
module Cop
|
||||
module Homebrew
|
||||
# This cop checks for the use of `FileUtils.rm_rf` and recommends `FileUtils.rm_r`.
|
||||
# This cop checks for the use of `FileUtils.rm_f` or `FileUtils.rm_rf` and recommends the non-`f` versions.
|
||||
class NoFileutilsRmrf < Base
|
||||
extend AutoCorrector
|
||||
|
||||
MSG = "Use `FileUtils.rm_r` instead of `FileUtils.rm_rf`."
|
||||
MSG = "Use `FileUtils.rm` or `FileUtils.rm_f` instead of `FileUtils.rm_rf` or `FileUtils.rm_f`."
|
||||
|
||||
def_node_matcher :fileutils_rm_rf?, <<~PATTERN
|
||||
(send (const {nil? cbase} :FileUtils) :rm_rf ...)
|
||||
def_node_matcher :fileutils_rm_r_f?, <<~PATTERN
|
||||
(send (const {nil? cbase} :FileUtils) {:rm_rf :rm_f} ...)
|
||||
PATTERN
|
||||
|
||||
def on_send(node)
|
||||
return unless fileutils_rm_rf?(node)
|
||||
return unless fileutils_rm_r_f?(node)
|
||||
|
||||
add_offense(node) do |corrector|
|
||||
corrector.replace(node.loc.expression, "FileUtils.rm_r(#{node.arguments.first.source})")
|
||||
new_method = node.method?(:rm_rf) ? "rm_r" : "rm"
|
||||
corrector.replace(node.loc.expression, "FileUtils.#{new_method}(#{node.arguments.first.source})")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -8,7 +8,14 @@ RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
|
||||
it "registers an offense when using FileUtils.rm_rf" do
|
||||
expect_offense(<<~RUBY)
|
||||
FileUtils.rm_rf("path/to/directory")
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: Use `FileUtils.rm_r` instead of `FileUtils.rm_rf`.
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "registers an offense when using FileUtils.rm_f" do
|
||||
expect_offense(<<~RUBY)
|
||||
FileUtils.rm_f("path/to/directory")
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
||||
RUBY
|
||||
end
|
||||
|
||||
@ -27,4 +34,10 @@ RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
|
||||
FileUtils.rm_r("path/to/directory")
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "does not register an offense when using FileUtils.rm" do
|
||||
expect_no_offenses(<<~RUBY)
|
||||
FileUtils.rm("path/to/directory")
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user