
- Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
72 lines
2.7 KiB
Ruby
72 lines
2.7 KiB
Ruby
# typed: true # rubocop:todo Sorbet/StrictSigil
|
|
# frozen_string_literal: true
|
|
|
|
module RuboCop
|
|
module Cop
|
|
module Cask
|
|
class ArrayAlphabetization < Base
|
|
extend AutoCorrector
|
|
|
|
def on_send(node)
|
|
return unless [:zap, :uninstall].include?(node.method_name)
|
|
|
|
node.each_descendant(:pair).each do |pair|
|
|
symbols = pair.children.select(&:sym_type?).map(&:value)
|
|
next if symbols.intersect?([:signal, :script, :early_script, :args, :input])
|
|
|
|
pair.each_descendant(:array).each do |array|
|
|
if array.children.length == 1
|
|
add_offense(array, message: "Avoid single-element arrays by removing the []") do |corrector|
|
|
corrector.replace(array.source_range, array.children.first.source)
|
|
end
|
|
end
|
|
|
|
next if array.children.length <= 1
|
|
|
|
sorted_array = sort_array(array.source.split("\n")).join("\n")
|
|
|
|
next if array.source == sorted_array
|
|
|
|
add_offense(array, message: "The array elements should be ordered alphabetically") do |corrector|
|
|
corrector.replace(array.source_range, sorted_array)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def sort_array(source)
|
|
# Combine each comment with the line(s) below so that they remain in the same relative location
|
|
combined_source = source.each_with_index.filter_map do |line, index|
|
|
next if line.blank?
|
|
next if line.strip.start_with?("#")
|
|
|
|
next recursively_find_comments(source, index, line)
|
|
end
|
|
|
|
# Separate the lines into those that should be sorted and those that should not
|
|
# i.e. skip the opening and closing brackets of the array.
|
|
to_sort, to_keep = combined_source.partition { |line| !line.include?("[") && !line.include?("]") }
|
|
|
|
# Sort the lines that should be sorted
|
|
to_sort.sort! do |a, b|
|
|
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
|
|
a_non_comment.downcase <=> b_non_comment.downcase
|
|
end
|
|
|
|
# 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 }
|
|
end
|
|
|
|
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
|
|
end
|
|
end
|
|
end
|
|
end
|