mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00

- Use `sort_by` to sort the array, rather than comparing each element to the next. - This doesn't error with complaints about clobbering at all when run on `homebrew/cask`, hurray. And it also handles interpolations correctly, rather than ignoring them. Co-authored-by: Bevan Kay <email@bevankay.me>
41 lines
1.3 KiB
Ruby
41 lines
1.3 KiB
Ruby
# typed: true
|
|
# frozen_string_literal: true
|
|
|
|
module RuboCop
|
|
module Cop
|
|
module Cask
|
|
class ArrayAlphabetization < Base
|
|
extend AutoCorrector
|
|
|
|
SINGLE_MSG = "Remove the `[]` around a single `zap trash` path".freeze
|
|
NON_ALPHABETICAL_MSG = "The `zap trash` paths should be in alphabetical order".freeze
|
|
|
|
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
|
|
add_offense(array, message: SINGLE_MSG) do |corrector|
|
|
corrector.replace(array.source_range, array.children.first.source)
|
|
end
|
|
end
|
|
|
|
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)
|
|
|
|
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)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|