rubocop/no_fileutils_rmrf: Scope to just formulae and casks

This commit is contained in:
Issy Long 2024-07-14 12:09:59 -04:00
parent 14dd3592dc
commit 99c5cc99b5
No known key found for this signature in database
4 changed files with 41 additions and 42 deletions

View File

@ -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

View File

@ -9,15 +9,14 @@ module RuboCop
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 @@ module RuboCop
"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

View File

@ -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

View File

@ -5,78 +5,59 @@ require "rubocops/no_fileutils_rmrf"
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