rubocop: Discourage the use of FileUtils.rm_rf
- This cop checks for the use of `FileUtils.rm_rf` and suggests using `FileUtils.rm_r` because we should know if we couldn't delete a thing for some reason, not just force it.
This commit is contained in:
parent
885cfb79df
commit
e6976ae3d1
@ -8,6 +8,7 @@ require_relative "compact_blank"
|
||||
require_relative "io_read"
|
||||
require_relative "move_to_extend_os"
|
||||
require_relative "negate_include"
|
||||
require_relative "no_fileutils_rmrf"
|
||||
require_relative "presence"
|
||||
require_relative "present"
|
||||
require_relative "safe_navigation_with_blank"
|
||||
|
27
Library/Homebrew/rubocops/no_fileutils_rmrf.rb
Normal file
27
Library/Homebrew/rubocops/no_fileutils_rmrf.rb
Normal file
@ -0,0 +1,27 @@
|
||||
# typed: true
|
||||
# frozen_string_literal: true
|
||||
|
||||
module RuboCop
|
||||
module Cop
|
||||
module Homebrew
|
||||
# This cop checks for the use of `FileUtils.rm_rf` and recommends `FileUtils.rm_r`.
|
||||
class NoFileutilsRmrf < Base
|
||||
extend AutoCorrector
|
||||
|
||||
MSG = "Use `FileUtils.rm_r` instead of `FileUtils.rm_rf`."
|
||||
|
||||
def_node_matcher :fileutils_rm_rf?, <<~PATTERN
|
||||
(send (const {nil? cbase} :FileUtils) :rm_rf ...)
|
||||
PATTERN
|
||||
|
||||
def on_send(node)
|
||||
return unless fileutils_rm_rf?(node)
|
||||
|
||||
add_offense(node) do |corrector|
|
||||
corrector.replace(node.loc.expression, "FileUtils.rm_r(#{node.arguments.first.source})")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
30
Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb
Normal file
30
Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb
Normal file
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "rubocops/no_fileutils_rmrf"
|
||||
|
||||
RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
|
||||
subject(:cop) { described_class.new }
|
||||
|
||||
it "registers an offense when using FileUtils.rm_rf" do
|
||||
expect_offense(<<~RUBY)
|
||||
FileUtils.rm_rf("path/to/directory")
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: Use `FileUtils.rm_r` instead of `FileUtils.rm_rf`.
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "autocorrects" do
|
||||
corrected = autocorrect_source(<<~RUBY)
|
||||
FileUtils.rm_rf("path/to/directory")
|
||||
RUBY
|
||||
|
||||
expect(corrected).to eq(<<~RUBY)
|
||||
FileUtils.rm_r("path/to/directory")
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "does not register an offense when using FileUtils.rm_r" do
|
||||
expect_no_offenses(<<~RUBY)
|
||||
FileUtils.rm_r("path/to/directory")
|
||||
RUBY
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user