From 3a48e0aa40670170ab6934505c12d9f1b8b4635b Mon Sep 17 00:00:00 2001 From: Issy Long Date: Sun, 14 Jul 2024 14:13:37 -0400 Subject: [PATCH] Casks use `FileUtils.rm_rf` & `Pathname.rmtree` still --- Library/Homebrew/rubocops/no_fileutils_rmrf.rb | 14 ++++++++++++-- .../rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi | 6 ++++++ .../test/rubocops/no_fileutils_rmrf_spec.rb | 12 ++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/rubocops/no_fileutils_rmrf.rb b/Library/Homebrew/rubocops/no_fileutils_rmrf.rb index ed234c96992b9..cafeda7accd6f 100644 --- a/Library/Homebrew/rubocops/no_fileutils_rmrf.rb +++ b/Library/Homebrew/rubocops/no_fileutils_rmrf.rb @@ -11,6 +11,14 @@ 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} ...) + 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 @@ 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) || 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 diff --git a/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi index c6bc07663b851..6c6f3c1db7eae 100644 --- a/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi +++ b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi @@ -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 diff --git a/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb b/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb index 749808ccbd8ab..93d2f2be78d4f 100644 --- a/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb +++ b/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb @@ -10,16 +10,20 @@ 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 @@ 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 @@ 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