Skip to content

Commit

Permalink
rubocop/no_fileutils_rmrf: Scope to just formulae and casks
Browse files Browse the repository at this point in the history
  • Loading branch information
issyl0 committed Jul 14, 2024
1 parent 2e9053b commit a2ff442
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
5 changes: 5 additions & 0 deletions Library/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions Library/Homebrew/rubocops/no_fileutils_rmrf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
49 changes: 15 additions & 34 deletions Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2ff442

Please sign in to comment.