Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rubocops/cask: add a cop for specific numbered shared file list files #16581

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions Library/Homebrew/rubocops/cask/shared_filelist_glob.rb
Original file line number Diff line number Diff line change
@@ -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"$/
bevanjkay marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 1 addition & 0 deletions Library/Homebrew/rubocops/rubocop-cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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"
Expand Down
24 changes: 24 additions & 0 deletions Library/Homebrew/test/rubocops/cask/shared_filelist_glob_spec.rb
Original file line number Diff line number Diff line change
@@ -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