Check uninstall too, avoiding arrays containing commands

This commit is contained in:
Issy Long 2024-01-02 23:31:13 +00:00
parent fb124f92ed
commit 9070f6d829
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 44 additions and 10 deletions

View File

@ -7,18 +7,19 @@ module RuboCop
class ArrayAlphabetization < Base class ArrayAlphabetization < Base
extend AutoCorrector 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) 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| 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| pair.each_descendant(:array).each do |array|
if array.children.length == 1 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) corrector.replace(array.source_range, array.children.first.source)
end end
end end
@ -28,7 +29,7 @@ module RuboCop
sorted_array = array.children.sort_by { |child| child.source.downcase } sorted_array = array.children.sort_by { |child| child.source.downcase }
next if sorted_array.map(&:source) == array.children.map(&:source) 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| array.children.each_with_index do |child, index|
corrector.replace(child.source_range, sorted_array[index].source) corrector.replace(child.source_range, sorted_array[index].source)
end end

View File

@ -9,7 +9,7 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do
url "https://example.com/foo.zip" url "https://example.com/foo.zip"
zap trash: ["~/Library/Application Support/Foo"] zap trash: ["~/Library/Application Support/Foo"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the `[]` around a single `zap trash` path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid single-element arrays by removing the []
end end
CASK CASK
@ -28,7 +28,7 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do
url "https://example.com/foo.zip" url "https://example.com/foo.zip"
zap trash: [ 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/Foo",
"/Library/Application Support/Baz", "/Library/Application Support/Baz",
"~/Library/Application Support/Foo", "~/Library/Application Support/Foo",
@ -59,7 +59,7 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do
url "https://example.com/foo.zip" url "https://example.com/foo.zip"
zap trash: [ 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/Foo",
"~/Library/Application Support/Bar\#{version.major}", "~/Library/Application Support/Bar\#{version.major}",
] ]
@ -90,4 +90,37 @@ describe RuboCop::Cop::Cask::ArrayAlphabetization, :config do
end end
CASK CASK
end 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 end