Casks use FileUtils.rm_rf & Pathname.rmtree still

This commit is contained in:
Issy Long 2024-07-14 14:13:37 -04:00
parent 99c5cc99b5
commit 0889df837a
No known key found for this signature in database
3 changed files with 30 additions and 2 deletions

View File

@ -11,6 +11,14 @@ module RuboCop
MSG = "Use `rm` or `rm_r` instead of `rm_rf`, `rm_f`, or `rmtree`."
def_node_matcher :fileutils_rm_r_f_tree?, <<~PATTERN
(send (const {nil? cbase} :FileUtils) {:rm_rf :rm_f :rmtree} ...)
PATTERN
def_node_matcher :pathname_rm_r_f_tree?, <<~PATTERN
(send (const {nil? cbase} :Pathname) :rmtree ...)
PATTERN
def_node_matcher :self_rm_r_f_tree?, <<~PATTERN
(send (self) {:rm_rf :rm_f :rmtree} ...)
PATTERN
@ -23,18 +31,20 @@ module RuboCop
return if neither_rm_rf_nor_rmtree?(node)
add_offense(node) do |corrector|
class_name = "FileUtils." if fileutils_rm_r_f_tree?(node) || pathname_rm_r_f_tree?(node)
new_method = if node.method?(:rm_rf) || node.method?(:rmtree)
"rm_r"
else
"rm"
end
corrector.replace(node.loc.expression, "#{new_method}(#{node.arguments.first.source})")
corrector.replace(node.loc.expression, "#{class_name}#{new_method}(#{node.arguments.first.source})")
end
end
def neither_rm_rf_nor_rmtree?(node)
!self_rm_r_f_tree?(node) && !plain_rm_r_f_tree?(node)
!self_rm_r_f_tree?(node) && !plain_rm_r_f_tree?(node) &&
!fileutils_rm_r_f_tree?(node) && !pathname_rm_r_f_tree?(node)
end
end
end

View File

@ -6,6 +6,12 @@
class RuboCop::Cop::Homebrew::NoFileutilsRmrf
sig { params(node: RuboCop::AST::Node, kwargs: T.untyped, block: T.untyped).returns(T.untyped) }
def fileutils_rm_r_f_tree?(node, **kwargs, &block); end
sig { params(node: RuboCop::AST::Node, kwargs: T.untyped, block: T.untyped).returns(T.untyped) }
def pathname_rm_r_f_tree?(node, **kwargs, &block); end
sig { params(node: RuboCop::AST::Node, kwargs: T.untyped, block: T.untyped).returns(T.untyped) }
def plain_rm_r_f_tree?(node, **kwargs, &block); end

View File

@ -10,16 +10,20 @@ RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
expect_offense(<<~RUBY)
rm_rf("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
FileUtils.rm_rf("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
RUBY
end
it "autocorrects" do
corrected = autocorrect_source(<<~RUBY)
rm_rf("path/to/directory")
FileUtils.rm_rf("path/to/other/directory")
RUBY
expect(corrected).to eq(<<~RUBY)
rm_r("path/to/directory")
FileUtils.rm_r("path/to/other/directory")
RUBY
end
end
@ -29,16 +33,20 @@ RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
expect_offense(<<~RUBY)
rm_f("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
FileUtils.rm_f("path/to/other/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
RUBY
end
it "autocorrects" do
corrected = autocorrect_source(<<~RUBY)
rm_f("path/to/directory")
FileUtils.rm_f("path/to/other/directory")
RUBY
expect(corrected).to eq(<<~RUBY)
rm("path/to/directory")
FileUtils.rm("path/to/other/directory")
RUBY
end
end
@ -48,16 +56,20 @@ RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
expect_offense(<<~RUBY)
rmtree("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
Pathname.rmtree("path/to/other/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
RUBY
end
it "autocorrects" do
corrected = autocorrect_source(<<~RUBY)
rmtree("path/to/directory")
Pathname.rmtree("path/to/other/directory")
RUBY
expect(corrected).to eq(<<~RUBY)
rm_r("path/to/directory")
FileUtils.rm_r("path/to/other/directory")
RUBY
end
end