diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index aaddb602a6ddc..829dd2dcdc50b 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -60,6 +60,11 @@ Homebrew/CompactBlank: # `blank?` is not necessarily available here: - "Homebrew/extend/enumerable.rb" +Homebrew/NoFileutilsRmrf: + Include: + - "/**/{Formula,Casks}/**/*.rb" + - "**/{Formula,Casks}/**/*.rb" + # only used internally Homebrew/MoveToExtendOS: Enabled: false diff --git a/Library/Homebrew/rubocops/no_fileutils_rmrf.rb b/Library/Homebrew/rubocops/no_fileutils_rmrf.rb index a99c9c5452e29..ed234c96992b9 100644 --- a/Library/Homebrew/rubocops/no_fileutils_rmrf.rb +++ b/Library/Homebrew/rubocops/no_fileutils_rmrf.rb @@ -9,15 +9,14 @@ module Homebrew class NoFileutilsRmrf < Base extend AutoCorrector - MSG = "Use `FileUtils.rm` or `FileUtils.rm_r` instead of `FileUtils.rm_rf`, `FileUtils.rm_f`, " \ - "or `{FileUtils,Pathname}.rmtree`." + MSG = "Use `rm` or `rm_r` instead of `rm_rf`, `rm_f`, or `rmtree`." - def_node_matcher :fileutils_rm_r_f?, <<~PATTERN - (send (const {nil? cbase} :FileUtils) {:rm_rf :rm_f :rmtree} ...) + def_node_matcher :self_rm_r_f_tree?, <<~PATTERN + (send (self) {:rm_rf :rm_f :rmtree} ...) PATTERN - def_node_matcher :pathname_rmtree?, <<~PATTERN - (send (const {nil? cbase} :Pathname) :rmtree ...) + def_node_matcher :plain_rm_r_f_tree?, <<~PATTERN + (send nil? {:rm_rf :rm_f :rmtree} ...) PATTERN def on_send(node) @@ -30,12 +29,12 @@ def on_send(node) "rm" end - corrector.replace(node.loc.expression, "FileUtils.#{new_method}(#{node.arguments.first.source})") + corrector.replace(node.loc.expression, "#{new_method}(#{node.arguments.first.source})") end end def neither_rm_rf_nor_rmtree?(node) - !fileutils_rm_r_f?(node) && !pathname_rmtree?(node) + !self_rm_r_f_tree?(node) && !plain_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 new file mode 100644 index 0000000000000..c6bc07663b851 --- /dev/null +++ b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi @@ -0,0 +1,14 @@ +# typed: true + +# DO NOT EDIT MANUALLY +# This is an autogenerated file for dynamic methods in `RuboCop::Cop::Homebrew::NoFileutilsRmrf`. +# Please instead update this file by running `bin/tapioca dsl RuboCop::Cop::Homebrew::NoFileutilsRmrf`. + + +class RuboCop::Cop::Homebrew::NoFileutilsRmrf + 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 + + sig { params(node: RuboCop::AST::Node, kwargs: T.untyped, block: T.untyped).returns(T.untyped) } + def self_rm_r_f_tree?(node, **kwargs, &block); end +end diff --git a/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb b/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb index 4581fb2dbba70..749808ccbd8ab 100644 --- a/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb +++ b/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb @@ -5,78 +5,59 @@ RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do subject(:cop) { described_class.new } - describe "FileUtils.rm_rf" do + describe "rm_rf" do it "registers an offense" do expect_offense(<<~RUBY) - FileUtils.rm_rf("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} + rm_rf("path/to/directory") + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} RUBY end it "autocorrects" do corrected = autocorrect_source(<<~RUBY) - FileUtils.rm_rf("path/to/directory") + rm_rf("path/to/directory") RUBY expect(corrected).to eq(<<~RUBY) - FileUtils.rm_r("path/to/directory") + rm_r("path/to/directory") RUBY end end - describe "FileUtils.rm_f" do + describe "rm_f" do it "registers an offense" do expect_offense(<<~RUBY) - FileUtils.rm_f("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} + rm_f("path/to/directory") + ^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} RUBY end it "autocorrects" do corrected = autocorrect_source(<<~RUBY) - FileUtils.rm_f("path/to/directory") + rm_f("path/to/directory") RUBY expect(corrected).to eq(<<~RUBY) - FileUtils.rm("path/to/directory") + rm("path/to/directory") RUBY end end - describe "FileUtils.rmtree" do + describe "rmtree" do it "registers an offense" do expect_offense(<<~RUBY) - FileUtils.rmtree("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} + rmtree("path/to/directory") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} RUBY end it "autocorrects" do corrected = autocorrect_source(<<~RUBY) - FileUtils.rmtree("path/to/directory") + rmtree("path/to/directory") RUBY expect(corrected).to eq(<<~RUBY) - FileUtils.rm_r("path/to/directory") - RUBY - end - end - - describe "Pathname.rmtree" do - it "registers an offense" do - expect_offense(<<~RUBY) - Pathname.rmtree("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} - RUBY - end - - it "autocorrects" do - corrected = autocorrect_source(<<~RUBY) - Pathname.rmtree("path/to/directory") - RUBY - - expect(corrected).to eq(<<~RUBY) - FileUtils.rm_r("path/to/directory") + rm_r("path/to/directory") RUBY end end