2024-08-12 10:30:59 +01:00
|
|
|
# typed: true # rubocop:todo Sorbet/StrictSigil
|
2023-12-19 23:35:16 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Cask
|
|
|
|
class ArrayAlphabetization < Base
|
|
|
|
extend AutoCorrector
|
|
|
|
|
2025-02-01 23:54:35 +00:00
|
|
|
sig { params(node: RuboCop::AST::SendNode).void }
|
2023-12-19 23:35:16 +00:00
|
|
|
def on_send(node)
|
2024-01-21 19:21:40 +00:00
|
|
|
return unless [:zap, :uninstall].include?(node.method_name)
|
2023-12-19 23:35:16 +00:00
|
|
|
|
|
|
|
node.each_descendant(:pair).each do |pair|
|
2024-01-14 20:55:22 +00:00
|
|
|
symbols = pair.children.select(&:sym_type?).map(&:value)
|
2024-01-21 19:21:40 +00:00
|
|
|
next if symbols.intersect?([:signal, :script, :early_script, :args, :input])
|
2023-12-22 00:41:56 +00:00
|
|
|
|
2023-12-19 23:35:16 +00:00
|
|
|
pair.each_descendant(:array).each do |array|
|
|
|
|
if array.children.length == 1
|
2024-01-02 23:31:13 +00:00
|
|
|
add_offense(array, message: "Avoid single-element arrays by removing the []") 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
|
|
|
|
|
2024-01-20 00:03:03 +00:00
|
|
|
sorted_array = sort_array(array.source.split("\n")).join("\n")
|
2024-01-18 12:53:23 +00:00
|
|
|
|
2024-01-20 00:03:03 +00:00
|
|
|
next if array.source == sorted_array
|
2023-12-19 23:35:16 +00:00
|
|
|
|
2024-01-02 23:31:13 +00:00
|
|
|
add_offense(array, message: "The array elements should be ordered alphabetically") do |corrector|
|
2024-01-20 00:03:03 +00:00
|
|
|
corrector.replace(array.source_range, sorted_array)
|
2023-12-19 23:35:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-01-18 12:53:23 +00:00
|
|
|
|
2024-01-20 00:03:03 +00:00
|
|
|
def sort_array(source)
|
2024-01-21 12:42:22 +00:00
|
|
|
# Combine each comment with the line(s) below so that they remain in the same relative location
|
2024-02-22 23:29:55 +00:00
|
|
|
combined_source = source.each_with_index.filter_map do |line, index|
|
2024-04-17 13:48:06 -04:00
|
|
|
next if line.blank?
|
2024-01-21 12:42:22 +00:00
|
|
|
next if line.strip.start_with?("#")
|
2024-01-21 12:47:33 +00:00
|
|
|
|
2024-01-21 12:42:22 +00:00
|
|
|
next recursively_find_comments(source, index, line)
|
2024-02-22 23:29:55 +00:00
|
|
|
end
|
2024-01-20 00:03:03 +00:00
|
|
|
|
|
|
|
# Separate the lines into those that should be sorted and those that should not
|
2024-04-30 11:10:23 +02:00
|
|
|
# i.e. skip the opening and closing brackets of the array.
|
2024-01-20 00:35:40 +00:00
|
|
|
to_sort, to_keep = combined_source.partition { |line| !line.include?("[") && !line.include?("]") }
|
2024-01-20 00:03:03 +00:00
|
|
|
|
|
|
|
# Sort the lines that should be sorted
|
|
|
|
to_sort.sort! do |a, b|
|
2024-01-20 00:35:40 +00:00
|
|
|
a_non_comment = a.split("\n").reject { |line| line.strip.start_with?("#") }.first
|
|
|
|
b_non_comment = b.split("\n").reject { |line| line.strip.start_with?("#") }.first
|
2024-01-20 00:03:03 +00:00
|
|
|
a_non_comment.downcase <=> b_non_comment.downcase
|
2024-01-18 12:53:23 +00:00
|
|
|
end
|
|
|
|
|
2024-01-20 00:03:03 +00:00
|
|
|
# Merge the sorted lines and the unsorted lines, preserving the original positions of the unsorted lines
|
|
|
|
combined_source.map { |line| to_keep.include?(line) ? line : to_sort.shift }
|
2024-01-18 12:53:23 +00:00
|
|
|
end
|
2024-01-21 12:42:22 +00:00
|
|
|
|
|
|
|
def recursively_find_comments(source, index, line)
|
|
|
|
if source[index - 1].strip.start_with?("#")
|
|
|
|
return recursively_find_comments(source, index - 1, "#{source[index - 1]}\n#{line}")
|
|
|
|
end
|
|
|
|
|
|
|
|
line
|
|
|
|
end
|
2023-12-19 23:35:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|