brew/Library/Homebrew/rubocops/cask/array_alphabetization.rb
Issy Long 8cf58e36e6
Add a new RuboCop for alphabetizing zap trash array elements
- Part of issue 16323.
- Previously this was being done manually by Cask maintainers.
- While we're here, enforce that the `zap trash` path is not in `[]` if
  it only contains a single element.
- This is buggy on actual Casks, hence the draft PR.
2023-12-19 23:42:08 +00:00

39 lines
1.3 KiB
Ruby

# typed: true
# frozen_string_literal: true
module RuboCop
module Cop
module Cask
class ArrayAlphabetization < Base
extend AutoCorrector
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: "Remove the `[]` around a single `zap trash` path") do |corrector|
corrector.replace(array.source_range, array.children.first.source)
end
end
array.each_descendant(:str).each_cons(2) do |first, second|
next if first.source < second.source
add_offense(second, message: "The `zap trash` paths should be in alphabetical order") do |corrector|
corrector.insert_before(first.source_range, second.source)
corrector.insert_before(second.source_range, first.source)
# Using `corrector.replace` here trips the clobbering detection.
corrector.remove(first.source_range)
corrector.remove(second.source_range)
end
end
end
end
end
end
end
end
end