2023-12-19 23:35:16 +00:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Cask
|
|
|
|
class ArrayAlphabetization < Base
|
|
|
|
extend AutoCorrector
|
|
|
|
|
2023-12-22 00:22:33 +00:00
|
|
|
SINGLE_MSG = "Remove the `[]` around a single `zap trash` path".freeze
|
|
|
|
NON_ALPHABETICAL_MSG = "The `zap trash` paths should be in alphabetical order".freeze
|
|
|
|
|
2023-12-19 23:35:16 +00:00
|
|
|
def on_send(node)
|
|
|
|
return if node.method_name != :zap
|
|
|
|
|
|
|
|
node.each_descendant(:pair).each do |pair|
|
|
|
|
pair.each_descendant(:array).each do |array|
|
|
|
|
if array.children.length == 1
|
2023-12-22 00:22:33 +00:00
|
|
|
add_offense(array, message: SINGLE_MSG) do |corrector|
|
2023-12-19 23:35:16 +00:00
|
|
|
corrector.replace(array.source_range, array.children.first.source)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-22 00:22:33 +00:00
|
|
|
next if array.children.length <= 1
|
|
|
|
|
|
|
|
sorted_array = array.children.sort_by { |child| child.source.downcase }
|
|
|
|
next if sorted_array.map(&:source) == array.children.map(&:source)
|
2023-12-19 23:35:16 +00:00
|
|
|
|
2023-12-22 00:22:33 +00:00
|
|
|
add_offense(array, message: NON_ALPHABETICAL_MSG) do |corrector|
|
|
|
|
array.children.each_with_index do |child, index|
|
|
|
|
corrector.replace(child.source_range, sorted_array[index].source)
|
2023-12-19 23:35:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|