rubocops: add no_autobump! rubocop

Signed-off-by: botantony <antonsm21@gmail.com>
This commit is contained in:
botantony 2025-06-17 13:11:10 +02:00
parent 768c10c6fb
commit aecdcd4840
No known key found for this signature in database
GPG Key ID: 7FE721557EA6AAD6
10 changed files with 382 additions and 0 deletions

View File

@ -28,6 +28,7 @@ require_relative "components_redundancy"
require_relative "conflicts" require_relative "conflicts"
require_relative "dependency_order" require_relative "dependency_order"
require_relative "deprecate_disable" require_relative "deprecate_disable"
require_relative "no_autobump"
require_relative "desc" require_relative "desc"
require_relative "files" require_relative "files"
require_relative "homepage" require_relative "homepage"

View File

@ -26,6 +26,7 @@ module RuboCop
[:language], [:language],
[:url, :appcast, :name, :desc, :homepage], [:url, :appcast, :name, :desc, :homepage],
[:livecheck], [:livecheck],
[:no_autobump!],
[:deprecate!, :disable!], [:deprecate!, :disable!],
[ [
:auto_updates, :auto_updates,

View File

@ -0,0 +1,50 @@
# typed: strict
# frozen_string_literal: true
require "forwardable"
require "rubocops/shared/no_autobump_helper"
module RuboCop
module Cop
module Cask
# This cop audits `no_autobump!` reason.
# See the {NoAutobumpHelper} module for details of the checks.
class NoAutobump < Base
extend Forwardable
extend AutoCorrector
include CaskHelp
include NoAutobumpHelper
sig { override.params(cask_block: RuboCop::Cask::AST::CaskBlock).void }
def on_cask(cask_block)
@cask_block = T.let(cask_block, T.nilable(RuboCop::Cask::AST::CaskBlock))
toplevel_stanzas.select(&:no_autobump?).each do |stanza|
no_autobump_node = stanza.stanza_node
reason_found = T.let(false, T::Boolean)
reason(no_autobump_node) do |reason_node|
reason_found = true
audit_no_autobump(:cask, reason_node)
end
next if reason_found
problem 'Add a reason for exclusion from autobump: `no_autobump! because: "..."`'
end
end
private
sig { returns(T.nilable(RuboCop::Cask::AST::CaskBlock)) }
attr_reader :cask_block
def_delegators :cask_block, :toplevel_stanzas
def_node_search :reason, <<~EOS
(pair (sym :because) ${str sym})
EOS
end
end
end
end

View File

@ -0,0 +1,17 @@
# typed: strict
module RuboCop
module Cop
module Cask
class NoAutobump < Base
sig {
params(
base_node: RuboCop::AST::Node,
block: T.proc.params(node: RuboCop::AST::SendNode).void,
).void
}
def reason(base_node, &block); end
end
end
end
end

View File

@ -0,0 +1,41 @@
# typed: strict
# frozen_string_literal: true
require "rubocops/extend/formula_cop"
require "rubocops/shared/no_autobump_helper"
module RuboCop
module Cop
module FormulaAudit
# This cop audits `no_autobump!` reason.
# See the {NoAutobumpHelper} module for details of the checks.
class NoAutobump < FormulaCop
include NoAutobumpHelper
extend AutoCorrector
sig { override.params(formula_nodes: FormulaNodes).void }
def audit_formula(formula_nodes)
body_node = formula_nodes.body_node
no_autobump_call = find_node_method_by_name(body_node, :no_autobump!)
return if no_autobump_call.nil?
reason_found = T.let(false, T::Boolean)
reason(no_autobump_call) do |reason_node|
reason_found = true
offending_node(reason_node)
audit_no_autobump(:formula, reason_node)
end
return if reason_found
problem 'Add a reason for exclusion from autobump: `no_autobump! because: "..."`'
end
def_node_search :reason, <<~EOS
(pair (sym :because) ${str sym})
EOS
end
end
end
end

View File

@ -16,6 +16,7 @@ require_relative "cask/array_alphabetization"
require_relative "cask/desc" require_relative "cask/desc"
require_relative "cask/discontinued" require_relative "cask/discontinued"
require_relative "cask/homepage_url_styling" require_relative "cask/homepage_url_styling"
require_relative "cask/no_autobump"
require_relative "cask/no_overrides" require_relative "cask/no_overrides"
require_relative "cask/on_system_conditionals" require_relative "cask/on_system_conditionals"
require_relative "cask/shared_filelist_glob" require_relative "cask/shared_filelist_glob"

View File

@ -0,0 +1,41 @@
# typed: strict
# frozen_string_literal: true
require "rubocops/shared/helper_functions"
module RuboCop
module Cop
# This cop audits `no_autobump!` reason.
module NoAutobumpHelper
include HelperFunctions
PUNCTUATION_MARKS = %w[. ! ?].freeze
DISALLOWED_NO_AUTOBUMP_REASONS = %w[extract_plist latest_version].freeze
sig { params(_type: Symbol, reason_node: RuboCop::AST::Node).void }
def audit_no_autobump(_type, reason_node)
@offensive_node = T.let(reason_node, T.nilable(RuboCop::AST::Node))
reason_string = string_content(reason_node)
if reason_node.sym_type? && DISALLOWED_NO_AUTOBUMP_REASONS.include?(reason_string)
problem "`:#{reason_string}` reason should not be used"
end
return if reason_node.sym_type?
if reason_string.start_with?("it ")
problem "Do not start the reason with `it`" do |corrector|
corrector.replace(T.must(@offensive_node).source_range, "\"#{reason_string[3..]}\"")
end
end
return unless PUNCTUATION_MARKS.include?(reason_string[-1])
problem "Do not end the reason with a punctuation mark" do |corrector|
corrector.replace(T.must(@offensive_node).source_range, "\"#{reason_string.chop}\"")
end
end
end
end
end

View File

@ -0,0 +1,18 @@
# typed: true
# DO NOT EDIT MANUALLY
# This is an autogenerated file for dynamic methods in `RuboCop::Cop::FormulaAudit::DeprecateDisableReason`.
# Please instead update this file by running `bin/tapioca dsl RuboCop::Cop::FormulaAudit::DeprecateDisableReason`.
class RuboCop::Cop::FormulaAudit::NoAutobump
sig do
params(
node: RuboCop::AST::Node,
pattern: T.any(String, Symbol),
kwargs: T.untyped,
block: T.untyped
).returns(T.untyped)
end
def reason(node, *pattern, **kwargs, &block); end
end

View File

@ -0,0 +1,99 @@
# frozen_string_literal: true
require "rubocops/rubocop-cask"
RSpec.describe RuboCop::Cop::Cask::NoAutobump, :config do
it "reports no offenses if `reason` is acceptable" do
expect_no_offenses <<~CASK
cask 'foo' do
no_autobump! because: "some reason"
end
CASK
end
it "reports no offenses if `reason` is acceptable as a symbol" do
expect_no_offenses <<~CASK
cask 'foo' do
no_autobump! because: :bumped_by_upstream
end
CASK
end
it "reports an offense if `reason` is absent" do
expect_offense <<~CASK
cask 'foo' do
no_autobump!
^^^^^^^^^^^ Add a reason for exclusion from autobump: `no_autobump! because: "..."`
end
CASK
end
it "reports an offense is `reason` should not be set manually" do
expect_offense <<~CASK
cask 'foo' do
no_autobump! because: :extract_plist
^^^^^^^^^^^^^^ `:extract_plist` reason should not be used directly
end
CASK
end
it "reports and corrects an offense if `reason` starts with 'it'" do
expect_offense <<~CASK
cask 'foo' do
no_autobump! because: "it does something"
^^^^^^^^^^^^^^^^^^^ Do not start the reason with `it`
end
CASK
expect_correction <<~CASK
cask 'foo' do
no_autobump! because: "does something"
end
CASK
end
it "reports and corrects an offense if `reason` ends with a period" do
expect_offense <<~CASK
cask 'foo' do
no_autobump! because: "does something."
^^^^^^^^^^^^^^^^^ Do not end the reason with a punctuation mark
end
CASK
expect_correction <<~CASK
cask 'foo' do
no_autobump! because: "does something"
end
CASK
end
it "reports and corrects an offense if `reason` ends with an exclamation point" do
expect_offense <<~CASK
cask 'foo' do
no_autobump! because: "does something!"
^^^^^^^^^^^^^^^^^ Do not end the reason with a punctuation mark
end
CASK
expect_correction <<~CASK
cask 'foo' do
no_autobump! because: "does something"
end
CASK
end
it "reports and corrects an offense if `reason` ends with a question mark" do
expect_offense <<~CASK
cask 'foo' do
no_autobump! because: "does something?"
^^^^^^^^^^^^^^^^^ Do not end the reason with a punctuation mark
end
CASK
expect_correction <<~CASK
cask 'foo' do
no_autobump! because: "does something"
end
CASK
end
end

View File

@ -0,0 +1,113 @@
# frozen_string_literal: true
require "rubocops/no_autobump"
RSpec.describe RuboCop::Cop::FormulaAudit::NoAutobump do
subject(:cop) { described_class.new }
it "reports no offenses if `reason` is acceptable" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "some reason"
end
RUBY
end
it "reports no offenses if `reason` is acceptable as a symbol" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: :bumped_by_upstream
end
RUBY
end
it "reports an offense if `reason` is absent" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump!
^^^^^^^^^^^^ FormulaAudit/NoAutobumpReason: Add a reason for exclusion from autobump: `no_autobump! because: "..."`
end
RUBY
end
it "reports an offense is `reason` should not be set manually" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: :extract_plist
^^^^^^^^^^^^^^ FormulaAudit/NoAutobumpReason: `:extract_plist` reason should not be used directly
end
RUBY
end
it "reports and corrects an offense if `reason` starts with 'it'" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "it does something"
^^^^^^^^^^^^^^^^^^^ FormulaAudit/NoAutobumpReason: Do not start the reason with `it`
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "does something"
end
RUBY
end
it "reports and corrects an offense if `reason` ends with a period" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "does something."
^^^^^^^^^^^^^^^^^ FormulaAudit/NoAutobumpReason: Do not end the reason with a punctuation mark
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "does something"
end
RUBY
end
it "reports and corrects an offense if `reason` ends with an exclamation point" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "does something!"
^^^^^^^^^^^^^^^^^ FormulaAudit/NoAutobumpReason: Do not end the reason with a punctuation mark
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "does something"
end
RUBY
end
it "reports and corrects an offense if `reason` ends with a question mark" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "does something?"
^^^^^^^^^^^^^^^^^ FormulaAudit/NoAutobumpReason: Do not end the reason with a punctuation mark
end
RUBY
expect_correction(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
no_autobump! because: "does something"
end
RUBY
end
end