diff --git a/Library/Homebrew/rubocops/all.rb b/Library/Homebrew/rubocops/all.rb index 85ebc75082..ebe0525542 100644 --- a/Library/Homebrew/rubocops/all.rb +++ b/Library/Homebrew/rubocops/all.rb @@ -28,6 +28,7 @@ require_relative "components_redundancy" require_relative "conflicts" require_relative "dependency_order" require_relative "deprecate_disable" +require_relative "no_autobump" require_relative "desc" require_relative "files" require_relative "homepage" diff --git a/Library/Homebrew/rubocops/cask/constants/stanza.rb b/Library/Homebrew/rubocops/cask/constants/stanza.rb index 400aa7cc17..d6d2e9a278 100644 --- a/Library/Homebrew/rubocops/cask/constants/stanza.rb +++ b/Library/Homebrew/rubocops/cask/constants/stanza.rb @@ -26,6 +26,7 @@ module RuboCop [:language], [:url, :appcast, :name, :desc, :homepage], [:livecheck], + [:no_autobump!], [:deprecate!, :disable!], [ :auto_updates, diff --git a/Library/Homebrew/rubocops/cask/no_autobump.rb b/Library/Homebrew/rubocops/cask/no_autobump.rb new file mode 100644 index 0000000000..6f6a93007f --- /dev/null +++ b/Library/Homebrew/rubocops/cask/no_autobump.rb @@ -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 diff --git a/Library/Homebrew/rubocops/cask/no_autobump.rbi b/Library/Homebrew/rubocops/cask/no_autobump.rbi new file mode 100644 index 0000000000..18510d4014 --- /dev/null +++ b/Library/Homebrew/rubocops/cask/no_autobump.rbi @@ -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 diff --git a/Library/Homebrew/rubocops/no_autobump.rb b/Library/Homebrew/rubocops/no_autobump.rb new file mode 100644 index 0000000000..bd237f82ca --- /dev/null +++ b/Library/Homebrew/rubocops/no_autobump.rb @@ -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 diff --git a/Library/Homebrew/rubocops/rubocop-cask.rb b/Library/Homebrew/rubocops/rubocop-cask.rb index 668fcea57d..f304b53f01 100644 --- a/Library/Homebrew/rubocops/rubocop-cask.rb +++ b/Library/Homebrew/rubocops/rubocop-cask.rb @@ -16,6 +16,7 @@ require_relative "cask/array_alphabetization" require_relative "cask/desc" require_relative "cask/discontinued" require_relative "cask/homepage_url_styling" +require_relative "cask/no_autobump" require_relative "cask/no_overrides" require_relative "cask/on_system_conditionals" require_relative "cask/shared_filelist_glob" diff --git a/Library/Homebrew/rubocops/shared/no_autobump_helper.rb b/Library/Homebrew/rubocops/shared/no_autobump_helper.rb new file mode 100644 index 0000000000..08c30d5397 --- /dev/null +++ b/Library/Homebrew/rubocops/shared/no_autobump_helper.rb @@ -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 diff --git a/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit/no_autobump.rbi b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit/no_autobump.rbi new file mode 100644 index 0000000000..85b3b24303 --- /dev/null +++ b/Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/formula_audit/no_autobump.rbi @@ -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 diff --git a/Library/Homebrew/test/rubocops/cask/no_autobump.rb b/Library/Homebrew/test/rubocops/cask/no_autobump.rb new file mode 100644 index 0000000000..c97fe0c13b --- /dev/null +++ b/Library/Homebrew/test/rubocops/cask/no_autobump.rb @@ -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 diff --git a/Library/Homebrew/test/rubocops/no_autobump.rb b/Library/Homebrew/test/rubocops/no_autobump.rb new file mode 100644 index 0000000000..2b66a85398 --- /dev/null +++ b/Library/Homebrew/test/rubocops/no_autobump.rb @@ -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