Skip to content

Commit

Permalink
rubocops/no_fileutils_rmrf: Fix rmtree on a method returning Pathname
Browse files Browse the repository at this point in the history
- Tidy up the node matchers. Either `FileUtils.rm_rf` or `rm_rf` on a
  `Pathname` instance or `self`.
  • Loading branch information
issyl0 committed Jul 25, 2024
1 parent f72927f commit 429028c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
29 changes: 16 additions & 13 deletions Library/Homebrew/rubocops/no_fileutils_rmrf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,38 @@ class NoFileutilsRmrf < Base

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} ...)
def_node_matcher :any_receiver_rm_r_f?, <<~PATTERN
(send
{(const {nil? cbase} :FileUtils) (self)}
{:rm_rf :rm_f}
...)
PATTERN

def_node_matcher :instance_rmtree?, <<~PATTERN
(send (lvar ...) :rmtree ...)
def_node_matcher :no_receiver_rm_r_f?, <<~PATTERN
(send nil? {:rm_rf :rm_f} ...)
PATTERN

def_node_matcher :self_rm_r_f_tree?, <<~PATTERN
(send (self) {:rm_rf :rm_f :rmtree} ...)
def_node_matcher :no_receiver_rmtree?, <<~PATTERN
(send nil? :rmtree ...)
PATTERN

def_node_matcher :plain_rm_r_f_tree?, <<~PATTERN
(send nil? {:rm_rf :rm_f :rmtree} ...)
def_node_matcher :any_receiver_rmtree?, <<~PATTERN
(send !nil? :rmtree ...)
PATTERN

def on_send(node)
return if neither_rm_rf_nor_rmtree?(node)

add_offense(node) do |corrector|
class_name = "FileUtils." if fileutils_rm_r_f_tree?(node) || instance_rmtree?(node)
class_name = "FileUtils." if any_receiver_rm_r_f?(node) || any_receiver_rmtree?(node)
new_method = if node.method?(:rm_rf) || node.method?(:rmtree)
"rm_r"
else
"rm"
end

args = if instance_rmtree?(node)
node.receiver.source
args = if any_receiver_rmtree?(node)
node.receiver&.source || node.arguments.first&.source
else
node.arguments.first.source
end
Expand All @@ -49,8 +52,8 @@ def on_send(node)
end

def neither_rm_rf_nor_rmtree?(node)
!self_rm_r_f_tree?(node) && !plain_rm_r_f_tree?(node) &&
!fileutils_rm_r_f_tree?(node) && !instance_rmtree?(node)
!any_receiver_rm_r_f?(node) && !no_receiver_rm_r_f?(node) &&
!any_receiver_rmtree?(node) && !no_receiver_rmtree?(node)
end
end
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
other_dir = Pathname("path/to/other/directory")
other_dir.rmtree
^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
def buildpath
Pathname("path/to/yet/another/directory")
end
buildpath.rmtree
^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
RUBY
end

Expand All @@ -67,12 +72,20 @@
rmtree("path/to/directory")
other_dir = Pathname("path/to/other/directory")
other_dir.rmtree
def buildpath
Pathname("path/to/yet/another/directory")
end
buildpath.rmtree
RUBY

expect(corrected).to eq(<<~RUBY)
rm_r("path/to/directory")
other_dir = Pathname("path/to/other/directory")
FileUtils.rm_r(other_dir)
def buildpath
Pathname("path/to/yet/another/directory")
end
FileUtils.rm_r(buildpath)
RUBY
end
end
Expand Down

0 comments on commit 429028c

Please sign in to comment.