-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38c5e52
commit 4e18311
Showing
3 changed files
with
55 additions
and
0 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
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,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 |
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