diff --git a/Library/Homebrew/rubocops/cask/shared_filelist_glob.rb b/Library/Homebrew/rubocops/cask/shared_filelist_glob.rb new file mode 100644 index 0000000000..8f2e4abbac --- /dev/null +++ b/Library/Homebrew/rubocops/cask/shared_filelist_glob.rb @@ -0,0 +1,37 @@ +# typed: true +# frozen_string_literal: true + +module RuboCop + module Cop + module Cask + class SharedFilelistGlob < Base + extend AutoCorrector + + def on_send(node) + return if node.method_name != :zap + + node.each_descendant(:pair).each do |pair| + symbols = pair.children.select(&:sym_type?).map(&:value) + next unless symbols.include?(:trash) + + pair.each_descendant(:array).each do |array| + regex = /\.sfl\d"$/ + message = "Use a glob (*) instead of a specific version (ie. sfl2) for trashing Shared File List paths" + + array.children.each do |item| + next unless item.source.match?(regex) + + corrected_item = item.source.sub(/sfl\d"$/, "sfl*\"") + + add_offense(item, + message: message) do |corrector| + corrector.replace(item, corrected_item) + end + end + end + end + end + end + end + end +end diff --git a/Library/Homebrew/rubocops/rubocop-cask.rb b/Library/Homebrew/rubocops/rubocop-cask.rb index 572db73393..44abad52f9 100644 --- a/Library/Homebrew/rubocops/rubocop-cask.rb +++ b/Library/Homebrew/rubocops/rubocop-cask.rb @@ -18,6 +18,7 @@ require_relative "cask/discontinued" require_relative "cask/homepage_url_trailing_slash" require_relative "cask/no_overrides" require_relative "cask/on_system_conditionals" +require_relative "cask/shared_filelist_glob" require_relative "cask/stanza_order" require_relative "cask/stanza_grouping" require_relative "cask/uninstall_methods_order" diff --git a/Library/Homebrew/test/rubocops/cask/shared_filelist_glob_spec.rb b/Library/Homebrew/test/rubocops/cask/shared_filelist_glob_spec.rb new file mode 100644 index 0000000000..cc375563d3 --- /dev/null +++ b/Library/Homebrew/test/rubocops/cask/shared_filelist_glob_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "rubocops/rubocop-cask" + +describe RuboCop::Cop::Cask::SharedFilelistGlob, :config do + it "reports an offense when a zap trash array includes an .sfl2 or .sfl3 file" do + expect_offense(<<~CASK) + cask "foo" do + url "https://example.com/foo.zip" + + zap trash: ["~/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/org.mozilla.firefox.sfl2"] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a glob (*) instead of a specific version (ie. sfl2) for trashing Shared File List paths + end + CASK + + expect_correction(<<~CASK) + cask "foo" do + url "https://example.com/foo.zip" + + zap trash: ["~/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/org.mozilla.firefox.sfl*"] + end + CASK + end +end