diff --git a/Library/Homebrew/rubocops/cask/array_alphabetization.rb b/Library/Homebrew/rubocops/cask/array_alphabetization.rb index 335a5e22c2..2b0766b47b 100644 --- a/Library/Homebrew/rubocops/cask/array_alphabetization.rb +++ b/Library/Homebrew/rubocops/cask/array_alphabetization.rb @@ -7,18 +7,19 @@ module RuboCop 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 + return unless [:zap, :uninstall].include?(name = node.method_name) node.each_descendant(:pair).each do |pair| - next if pair.children.select(&:sym_type?).map(&:value) != [:trash] + symbols = pair.children.select { |child| child.sym_type? }.map(&:value) + # For `zap`s, we only care about `trash` arrays. + next if name == :zap && !symbols.include?(:trash) + # Don't order `uninstall` arrays that contain commands. + next if name == :uninstall && (symbols & [:signal, :script, :early_script]).any? pair.each_descendant(:array).each do |array| if array.children.length == 1 - add_offense(array, message: SINGLE_MSG) do |corrector| + add_offense(array, message: "Avoid single-element arrays by removing the []") do |corrector| corrector.replace(array.source_range, array.children.first.source) end end @@ -28,7 +29,7 @@ module RuboCop 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| + add_offense(array, message: "The array elements should be ordered alphabetically") do |corrector| array.children.each_with_index do |child, index| corrector.replace(child.source_range, sorted_array[index].source) end diff --git a/Library/Homebrew/test/rubocops/cask/array_alphabetization_spec.rb b/Library/Homebrew/test/rubocops/cask/array_alphabetization_spec.rb index 73f79a2e6e..d576ef9573 100644 --- a/Library/Homebrew/test/rubocops/cask/array_alphabetization_spec.rb +++ b/Library/Homebrew/test/rubocops/cask/array_alphabetization_spec.rb @@ -9,7 +9,7 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do url "https://example.com/foo.zip" zap trash: ["~/Library/Application Support/Foo"] - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the `[]` around a single `zap trash` path + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid single-element arrays by removing the [] end CASK @@ -28,7 +28,7 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do url "https://example.com/foo.zip" zap trash: [ - ^ The `zap trash` paths should be in alphabetical order + ^ The array elements should be ordered alphabetically "/Library/Application Support/Foo", "/Library/Application Support/Baz", "~/Library/Application Support/Foo", @@ -59,7 +59,7 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do url "https://example.com/foo.zip" zap trash: [ - ^ The `zap trash` paths should be in alphabetical order + ^ The array elements should be ordered alphabetically "~/Library/Application Support/Foo", "~/Library/Application Support/Bar\#{version.major}", ] @@ -90,4 +90,37 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do end CASK end + + it "autocorrects alphabetization in `uninstall` methods" do + expect_offense(<<~CASK) + cask "foo" do + url "https://example.com/foo.zip" + + uninstall pkgutil: [ + ^ The array elements should be ordered alphabetically + "something", + "other", + ], + script: [ + "ordered", + "differently", + ] + end + CASK + + expect_correction(<<~CASK) + cask "foo" do + url "https://example.com/foo.zip" + + uninstall pkgutil: [ + "other", + "something", + ], + script: [ + "ordered", + "differently", + ] + end + CASK + end end