rubocop/no_fileutils_rmrf: Scope to just formulae and casks
This commit is contained in:
parent
14dd3592dc
commit
99c5cc99b5
@ -60,6 +60,11 @@ Homebrew/CompactBlank:
|
|||||||
# `blank?` is not necessarily available here:
|
# `blank?` is not necessarily available here:
|
||||||
- "Homebrew/extend/enumerable.rb"
|
- "Homebrew/extend/enumerable.rb"
|
||||||
|
|
||||||
|
Homebrew/NoFileutilsRmrf:
|
||||||
|
Include:
|
||||||
|
- "/**/{Formula,Casks}/**/*.rb"
|
||||||
|
- "**/{Formula,Casks}/**/*.rb"
|
||||||
|
|
||||||
# only used internally
|
# only used internally
|
||||||
Homebrew/MoveToExtendOS:
|
Homebrew/MoveToExtendOS:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|||||||
@ -9,15 +9,14 @@ module RuboCop
|
|||||||
class NoFileutilsRmrf < Base
|
class NoFileutilsRmrf < Base
|
||||||
extend AutoCorrector
|
extend AutoCorrector
|
||||||
|
|
||||||
MSG = "Use `FileUtils.rm` or `FileUtils.rm_r` instead of `FileUtils.rm_rf`, `FileUtils.rm_f`, " \
|
MSG = "Use `rm` or `rm_r` instead of `rm_rf`, `rm_f`, or `rmtree`."
|
||||||
"or `{FileUtils,Pathname}.rmtree`."
|
|
||||||
|
|
||||||
def_node_matcher :fileutils_rm_r_f?, <<~PATTERN
|
def_node_matcher :self_rm_r_f_tree?, <<~PATTERN
|
||||||
(send (const {nil? cbase} :FileUtils) {:rm_rf :rm_f :rmtree} ...)
|
(send (self) {:rm_rf :rm_f :rmtree} ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def_node_matcher :pathname_rmtree?, <<~PATTERN
|
def_node_matcher :plain_rm_r_f_tree?, <<~PATTERN
|
||||||
(send (const {nil? cbase} :Pathname) :rmtree ...)
|
(send nil? {:rm_rf :rm_f :rmtree} ...)
|
||||||
PATTERN
|
PATTERN
|
||||||
|
|
||||||
def on_send(node)
|
def on_send(node)
|
||||||
@ -30,12 +29,12 @@ module RuboCop
|
|||||||
"rm"
|
"rm"
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def neither_rm_rf_nor_rmtree?(node)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
14
Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi
generated
Normal file
14
Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/no_fileutils_rmrf.rbi
generated
Normal 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
|
||||||
@ -5,78 +5,59 @@ require "rubocops/no_fileutils_rmrf"
|
|||||||
RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
|
RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do
|
||||||
subject(:cop) { described_class.new }
|
subject(:cop) { described_class.new }
|
||||||
|
|
||||||
describe "FileUtils.rm_rf" do
|
describe "rm_rf" do
|
||||||
it "registers an offense" do
|
it "registers an offense" do
|
||||||
expect_offense(<<~RUBY)
|
expect_offense(<<~RUBY)
|
||||||
FileUtils.rm_rf("path/to/directory")
|
rm_rf("path/to/directory")
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it "autocorrects" do
|
it "autocorrects" do
|
||||||
corrected = autocorrect_source(<<~RUBY)
|
corrected = autocorrect_source(<<~RUBY)
|
||||||
FileUtils.rm_rf("path/to/directory")
|
rm_rf("path/to/directory")
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
expect(corrected).to eq(<<~RUBY)
|
expect(corrected).to eq(<<~RUBY)
|
||||||
FileUtils.rm_r("path/to/directory")
|
rm_r("path/to/directory")
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "FileUtils.rm_f" do
|
describe "rm_f" do
|
||||||
it "registers an offense" do
|
it "registers an offense" do
|
||||||
expect_offense(<<~RUBY)
|
expect_offense(<<~RUBY)
|
||||||
FileUtils.rm_f("path/to/directory")
|
rm_f("path/to/directory")
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it "autocorrects" do
|
it "autocorrects" do
|
||||||
corrected = autocorrect_source(<<~RUBY)
|
corrected = autocorrect_source(<<~RUBY)
|
||||||
FileUtils.rm_f("path/to/directory")
|
rm_f("path/to/directory")
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
expect(corrected).to eq(<<~RUBY)
|
expect(corrected).to eq(<<~RUBY)
|
||||||
FileUtils.rm("path/to/directory")
|
rm("path/to/directory")
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "FileUtils.rmtree" do
|
describe "rmtree" do
|
||||||
it "registers an offense" do
|
it "registers an offense" do
|
||||||
expect_offense(<<~RUBY)
|
expect_offense(<<~RUBY)
|
||||||
FileUtils.rmtree("path/to/directory")
|
rmtree("path/to/directory")
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it "autocorrects" do
|
it "autocorrects" do
|
||||||
corrected = autocorrect_source(<<~RUBY)
|
corrected = autocorrect_source(<<~RUBY)
|
||||||
FileUtils.rmtree("path/to/directory")
|
rmtree("path/to/directory")
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
expect(corrected).to eq(<<~RUBY)
|
expect(corrected).to eq(<<~RUBY)
|
||||||
FileUtils.rm_r("path/to/directory")
|
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")
|
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user