Skip to content

Commit c113c8e

Browse files
committed
formula: allow excluding deprecate! reason when disable! exists
1 parent 0e24ee2 commit c113c8e

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

Library/Homebrew/formula.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,12 +4237,10 @@ def pour_bottle?(only_if: nil, &block)
42374237
# @see https://docs.brew.sh/Deprecating-Disabling-and-Removing-Formulae
42384238
# @see DeprecateDisable::FORMULA_DEPRECATE_DISABLE_REASONS
42394239
# @api public
4240-
def deprecate!(date:, because:)
4240+
def deprecate!(date:, because: nil)
42414241
@deprecation_date = Date.parse(date)
4242-
return if @deprecation_date > Date.today
4243-
4244-
@deprecation_reason = because
4245-
@deprecated = true
4242+
@deprecated = !disabled? && @deprecation_date <= Date.today
4243+
@deprecation_reason = because if because
42464244
end
42474245

42484246
# Whether this {Formula} is deprecated (i.e. warns on installation).
@@ -4288,8 +4286,8 @@ def disable!(date:, because:)
42884286
@disable_date = Date.parse(date)
42894287

42904288
if @disable_date > Date.today
4291-
@deprecation_reason = because
4292-
@deprecated = true
4289+
@deprecation_reason ||= because
4290+
@deprecated = true if deprecation_date.nil?
42934291
return
42944292
end
42954293

Library/Homebrew/rubocops/deprecate_disable.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ class DeprecateDisableReason < FormulaCop
4545
sig { override.params(formula_nodes: FormulaNodes).void }
4646
def audit_formula(formula_nodes)
4747
body_node = formula_nodes.body_node
48+
reason_nodes = T.let([], T::Array[T.any(AST::StrNode, AST::SymbolNode)])
4849

49-
[:deprecate!, :disable!].each do |method|
50+
[:disable!, :deprecate!].each do |method|
5051
node = find_node_method_by_name(body_node, method)
5152

5253
next if node.nil?
5354

54-
reason_found = T.let(false, T::Boolean)
5555
reason(node) do |reason_node|
56-
reason_found = true
56+
reason_nodes << reason_node
5757
next if reason_node.sym_type?
5858

5959
offending_node(reason_node)
@@ -72,7 +72,7 @@ def audit_formula(formula_nodes)
7272
end
7373
end
7474

75-
next if reason_found
75+
next unless reason_nodes.empty?
7676

7777
case method
7878
when :deprecate!
@@ -81,6 +81,18 @@ def audit_formula(formula_nodes)
8181
problem 'Add a reason for disabling: `disable! because: "..."`'
8282
end
8383
end
84+
85+
return if reason_nodes.length < 2
86+
87+
disable_reason_node = T.must(reason_nodes.first)
88+
deprecate_reason_node = T.must(reason_nodes.second)
89+
return if disable_reason_node.value != deprecate_reason_node.value
90+
91+
offending_node(deprecate_reason_node.parent)
92+
problem "Remove deprecate reason when disable reason is identical" do |corrector|
93+
range = deprecate_reason_node.parent.source_range
94+
corrector.remove(range_with_surrounding_comma(range_with_surrounding_space(range:, side: :left)))
95+
end
8496
end
8597

8698
def_node_search :reason, <<~EOS

Library/Homebrew/test/rubocops/deprecate_disable/reason_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,65 @@ class Foo < Formula
324324
RUBY
325325
end
326326
end
327+
328+
context "when auditing `deprecate!` and `disable!`" do
329+
it "reports no offense if deprecate `reason` is absent" do
330+
expect_no_offenses(<<~RUBY)
331+
class Foo < Formula
332+
url 'https://brew.sh/foo-1.0.tgz'
333+
disable! date: "2021-08-28", because: :does_not_build
334+
deprecate! date: "2020-08-28"
335+
end
336+
RUBY
337+
end
338+
339+
it "reports offense if disable `reason` is absent`" do
340+
expect_offense(<<~RUBY)
341+
class Foo < Formula
342+
url 'https://brew.sh/foo-1.0.tgz'
343+
disable! date: "2021-08-28"
344+
^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/DeprecateDisableReason: Add a reason for disabling: `disable! because: "..."`
345+
deprecate! date: "2020-08-28", because: :does_not_build
346+
end
347+
RUBY
348+
end
349+
350+
it "reports and corrects an offense if disable and deprecate `reason` are identical symbols" do
351+
expect_offense(<<~RUBY)
352+
class Foo < Formula
353+
url 'https://brew.sh/foo-1.0.tgz'
354+
disable! date: "2021-08-28", because: :does_not_build
355+
deprecate! date: "2020-08-28", because: :does_not_build
356+
^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/DeprecateDisableReason: Remove deprecate reason when disable reason is identical
357+
end
358+
RUBY
359+
360+
expect_correction(<<~RUBY)
361+
class Foo < Formula
362+
url 'https://brew.sh/foo-1.0.tgz'
363+
disable! date: "2021-08-28", because: :does_not_build
364+
deprecate! date: "2020-08-28"
365+
end
366+
RUBY
367+
end
368+
369+
it "reports and corrects an offense if disable and deprecate `reason` are identical strings" do
370+
expect_offense(<<~RUBY)
371+
class Foo < Formula
372+
url 'https://brew.sh/foo-1.0.tgz'
373+
disable! date: "2021-08-28", because: "is broken"
374+
deprecate! date: "2020-08-28", because: "is broken"
375+
^^^^^^^^^^^^^^^^^^^^ FormulaAudit/DeprecateDisableReason: Remove deprecate reason when disable reason is identical
376+
end
377+
RUBY
378+
379+
expect_correction(<<~RUBY)
380+
class Foo < Formula
381+
url 'https://brew.sh/foo-1.0.tgz'
382+
disable! date: "2021-08-28", because: "is broken"
383+
deprecate! date: "2020-08-28"
384+
end
385+
RUBY
386+
end
387+
end
327388
end

0 commit comments

Comments
 (0)