Skip to content

Commit

Permalink
rubocop: order uninstall methods
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanazamfirei committed Dec 26, 2023
1 parent 38c5e52 commit 4e18311
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Library/Homebrew/rubocops/cask/constants/stanza.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ module Constants
end.freeze

STANZA_ORDER = STANZA_GROUPS.flatten.freeze

UNINSTALL_METHODS_ORDER = [
:early_script,
:launchctl,
:quit,
:signal,
:login_item,
:kext,
:script,
:pkgutil,
:delete,
:trash,
:rmdir,
].freeze
end
end
end
40 changes: 40 additions & 0 deletions Library/Homebrew/rubocops/cask/uninstall_methods_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# typed: true
# frozen_string_literal: true

module RuboCop
module Cop
module Cask
class UninstallMethodsOrder < Base
extend AutoCorrector
include IgnoredNode
include CaskHelp

MSG = "`%<method>s` method out of order".freeze

def on_send(node)
return unless [:zap, :uninstall].include?(node.method_name)

hash_node = node.arguments.first
return if !hash_node.is_a?(AST::Node) && !hash_node.hash_type?

method_nodes = hash_node.pairs.map(&:key)
expected_order = method_nodes.sort_by { |method| method_order_index(method) }

method_nodes.each_with_index do |method, index|
next if method == expected_order[index]

add_offense(method, message: format(MSG, method: method.children.first)) do |corrector|
new_code = expected_order.map { |s| hash_node.pairs.find { |pair| pair.key == s }.source }.join(",\n")
corrector.replace(hash_node.source_range, new_code)
end
end
end

def method_order_index(method_node)
method_name = method_node.children.first
RuboCop::Cask::Constants::UNINSTALL_METHODS_ORDER.index(method_name) || -1
end
end
end
end
end
1 change: 1 addition & 0 deletions Library/Homebrew/rubocops/rubocop-cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_relative "cask/homepage_url_trailing_slash"
require_relative "cask/no_overrides"
require_relative "cask/on_system_conditionals"
require_relative "cask/uninstall_methods_order"
require_relative "cask/stanza_order"
require_relative "cask/stanza_grouping"
require_relative "cask/url"
Expand Down

0 comments on commit 4e18311

Please sign in to comment.