Merge pull request #19313 from Homebrew/assert-path-rubocop
rubocop/lines: prefer `assert_path_exists` and `refute_path_exists`
This commit is contained in:
commit
0c698732f6
@ -86,6 +86,8 @@ module RuboCop
|
||||
|
||||
# This cop makes sure that idiomatic `assert_*` statements are used.
|
||||
class AssertStatements < FormulaCop
|
||||
extend AutoCorrector
|
||||
|
||||
sig { override.params(formula_nodes: FormulaNodes).void }
|
||||
def audit_formula(formula_nodes)
|
||||
return if (body_node = formula_nodes.body_node).nil?
|
||||
@ -96,17 +98,41 @@ module RuboCop
|
||||
end
|
||||
|
||||
if method_called_ever?(method, :exist?) && !method_called_ever?(method, :!)
|
||||
problem "Use `assert_predicate <path_to_file>, :exist?` instead of `#{method.source}`"
|
||||
problem "Use `assert_path_exists <path_to_file>` instead of `#{method.source}`"
|
||||
end
|
||||
|
||||
if method_called_ever?(method, :exist?) && method_called_ever?(method, :!)
|
||||
problem "Use `refute_predicate <path_to_file>, :exist?` instead of `#{method.source}`"
|
||||
problem "Use `refute_path_exists <path_to_file>` instead of `#{method.source}`"
|
||||
end
|
||||
|
||||
if method_called_ever?(method, :executable?) && !method_called_ever?(method, :!)
|
||||
problem "Use `assert_predicate <path_to_file>, :executable?` instead of `#{method.source}`"
|
||||
end
|
||||
end
|
||||
|
||||
find_every_method_call_by_name(body_node, :assert_predicate).each do |method|
|
||||
args = parameters(method)
|
||||
next if args[1].source != ":exist?"
|
||||
|
||||
offending_node(method)
|
||||
problem "Use `assert_path_exists <path_to_file>` instead of `#{method.source}`" do |corrector|
|
||||
correct = "assert_path_exists #{args.first.source}"
|
||||
correct += ", #{args[2].source}" if args.length == 3
|
||||
corrector.replace(T.must(@offensive_node).source_range, correct)
|
||||
end
|
||||
end
|
||||
|
||||
find_every_method_call_by_name(body_node, :refute_predicate).each do |method|
|
||||
args = parameters(method)
|
||||
next if args[1].source != ":exist?"
|
||||
|
||||
offending_node(method)
|
||||
problem "Use `refute_path_exists <path_to_file>` instead of `#{method.source}`" do |corrector|
|
||||
correct = "refute_path_exists #{args.first.source}"
|
||||
correct += ", #{args[2].source}" if args.length == 3
|
||||
corrector.replace(T.must(@offensive_node).source_range, correct)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@ RSpec.describe RuboCop::Cop::FormulaAudit::AssertStatements do
|
||||
desc "foo"
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
assert File.exist? "default.ini"
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/AssertStatements: Use `assert_predicate <path_to_file>, :exist?` instead of `assert File.exist? "default.ini"`
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/AssertStatements: Use `assert_path_exists <path_to_file>` instead of `assert File.exist? "default.ini"`
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
@ -34,7 +34,7 @@ RSpec.describe RuboCop::Cop::FormulaAudit::AssertStatements do
|
||||
desc "foo"
|
||||
url 'https://brew.sh/foo-1.0.tgz'
|
||||
assert !File.exist?("default.ini")
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/AssertStatements: Use `refute_predicate <path_to_file>, :exist?` instead of `assert !File.exist?("default.ini")`
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/AssertStatements: Use `refute_path_exists <path_to_file>` instead of `assert !File.exist?("default.ini")`
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user