-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16351 from Rylan12/rubocop-for-cask-discontinued
Create rubocop requiring `deprecate!` over `discontinued` for casks
- Loading branch information
Showing
9 changed files
with
196 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Cask | ||
# This cop corrects `caveats { discontinued }` to `deprecate!`. | ||
class Discontinued < Base | ||
include CaskHelp | ||
extend AutoCorrector | ||
|
||
MESSAGE = "Use `deprecate!` instead of `caveats { discontinued }`." | ||
|
||
def on_cask_stanza_block(stanza_block) | ||
stanza_block.stanzas.select(&:caveats?).each do |stanza| | ||
find_discontinued_method_call(stanza.stanza_node) do |node| | ||
if caveats_constains_only_discontinued?(node.parent) | ||
add_offense(node.parent, message: MESSAGE) do |corrector| | ||
corrector.replace(node.parent.source_range, | ||
"deprecate! date: \"#{Date.today}\", because: :discontinued") | ||
end | ||
else | ||
add_offense(node, message: MESSAGE) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def_node_matcher :caveats_constains_only_discontinued?, <<~EOS | ||
(block | ||
(send nil? :caveats) | ||
(args) | ||
(send nil? :discontinued)) | ||
EOS | ||
|
||
def_node_search :find_discontinued_method_call, <<~EOS | ||
$(send nil? :discontinued) | ||
EOS | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# typed: strict | ||
|
||
module RuboCop | ||
module Cop | ||
module Cask | ||
class Discontinued < Base | ||
sig { | ||
params( | ||
base_node: RuboCop::AST::BlockNode, | ||
block: T.nilable(T.proc.params(node: RuboCop::AST::SendNode).void), | ||
).returns(T::Boolean) | ||
} | ||
def caveats_constains_only_discontinued?(base_node, &block); end | ||
|
||
sig { | ||
params( | ||
base_node: RuboCop::AST::BlockNode, | ||
block: T.proc.params(node: RuboCop::AST::SendNode).void, | ||
).void | ||
} | ||
def find_discontinued_method_call(base_node, &block); end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubocops/rubocop-cask" | ||
|
||
describe RuboCop::Cop::Cask::Discontinued, :config do | ||
it "reports no offenses when there is no `caveats` stanza" do | ||
expect_no_offenses <<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "example.com/download/" | ||
end | ||
CASK | ||
end | ||
|
||
it "reports no offenses when there is a `caveats` stanza without `discontinued`" do | ||
expect_no_offenses <<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "example.com/download/" | ||
caveats do | ||
files_in_usr_local | ||
end | ||
end | ||
CASK | ||
end | ||
|
||
it "reports an offense when there is a `caveats` stanza with `discontinued` and other caveats" do | ||
expect_offense <<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "example.com/download/" | ||
caveats do | ||
discontinued | ||
^^^^^^^^^^^^ Use `deprecate!` instead of `caveats { discontinued }`. | ||
files_in_usr_local | ||
end | ||
end | ||
CASK | ||
end | ||
|
||
it "corrects `caveats { discontinued }` to `deprecate!`" do | ||
expect_offense <<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "example.com/download/" | ||
caveats do | ||
^^^^^^^^^^ Use `deprecate!` instead of `caveats { discontinued }`. | ||
discontinued | ||
end | ||
end | ||
CASK | ||
|
||
expect_correction <<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "example.com/download/" | ||
deprecate! date: "#{Date.today}", because: :discontinued | ||
end | ||
CASK | ||
end | ||
end |
12 changes: 6 additions & 6 deletions
12
...check/livecheck-discontinued-reference.rb → ...vecheck/livecheck-deprecated-reference.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Library/Homebrew/test/support/fixtures/cask/Casks/livecheck/livecheck-deprecated.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cask "livecheck-deprecated" do | ||
version "1.2.3" | ||
sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" | ||
|
||
# This cask is used in --online tests, so we use fake URLs to avoid impacting | ||
# real servers. The URL paths are specific enough that they'll be | ||
# understandable if they appear in local server logs. | ||
url "http://localhost/homebrew/test/cask/audit/livecheck/deprecated-#{version}.dmg" | ||
name "Deprecated" | ||
desc "Cask for testing deprecated in livecheck" | ||
homepage "http://localhost/homebrew/test/cask/audit/livecheck/deprecated" | ||
|
||
deprecate! date: "2023-01-01", because: :discontinued | ||
|
||
app "TestCask.app" | ||
end |
18 changes: 18 additions & 0 deletions
18
Library/Homebrew/test/support/fixtures/cask/Casks/livecheck/livecheck-disabled-reference.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
cask "livecheck-disabled-reference" do | ||
version "1.2.3" | ||
sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" | ||
|
||
# This cask is used in --online tests, so we use fake URLs to avoid impacting | ||
# real servers. The URL paths are specific enough that they'll be | ||
# understandable if they appear in local server logs. | ||
url "http://localhost/homebrew/test/cask/audit/livecheck/disabled-#{version}.dmg" | ||
name "Disabled Reference" | ||
desc "Cask for testing a livecheck reference to a disabled cask" | ||
homepage "http://localhost/homebrew/test/cask/audit/livecheck/disabled" | ||
|
||
livecheck do | ||
cask "livecheck/livecheck-disabled" | ||
end | ||
|
||
app "TestCask.app" | ||
end |
16 changes: 7 additions & 9 deletions
16
...Casks/livecheck/livecheck-discontinued.rb → ...ask/Casks/livecheck/livecheck-disabled.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
cask "livecheck-discontinued" do | ||
cask "livecheck-disabled" do | ||
version "1.2.3" | ||
sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" | ||
|
||
# This cask is used in --online tests, so we use fake URLs to avoid impacting | ||
# real servers. The URL paths are specific enough that they'll be | ||
# understandable if they appear in local server logs. | ||
url "http://localhost/homebrew/test/cask/audit/livecheck/discontinued-#{version}.dmg" | ||
name "Discontinued" | ||
desc "Cask for testing discontinued in livecheck" | ||
homepage "http://localhost/homebrew/test/cask/audit/livecheck/discontinued" | ||
url "http://localhost/homebrew/test/cask/audit/livecheck/disabled-#{version}.dmg" | ||
name "Disabled" | ||
desc "Cask for testing disabled in livecheck" | ||
homepage "http://localhost/homebrew/test/cask/audit/livecheck/disabled" | ||
|
||
app "TestCask.app" | ||
disable! date: "2023-01-01", because: :discontinued | ||
|
||
caveats do | ||
discontinued | ||
end | ||
app "TestCask.app" | ||
end |