brew/Library/Homebrew/rubocops/cask/on_system_conditionals.rb

87 lines
2.4 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - 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 ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "forwardable"
require "rubocops/shared/on_system_conditionals_helper"
module RuboCop
module Cop
module Cask
# This cop makes sure that OS conditionals are consistent.
#
# ### Example
#
# ```ruby
# # bad
# cask 'foo' do
# if MacOS.version == :high_sierra
# sha256 "..."
# end
# end
#
# # good
# cask 'foo' do
# on_high_sierra do
# sha256 "..."
# end
# end
# ```
class OnSystemConditionals < Base
extend Forwardable
extend AutoCorrector
include OnSystemConditionalsHelper
include CaskHelp
FLIGHT_STANZA_NAMES = [:preflight, :postflight, :uninstall_preflight, :uninstall_postflight].freeze
def on_cask(cask_block)
@cask_block = cask_block
toplevel_stanzas.each do |stanza|
next unless FLIGHT_STANZA_NAMES.include? stanza.stanza_name
audit_on_system_blocks(stanza.stanza_node, stanza.stanza_name)
end
audit_arch_conditionals(cask_body, allowed_blocks: FLIGHT_STANZA_NAMES)
2022-12-28 16:09:05 -05:00
audit_macos_version_conditionals(cask_body, recommend_on_system: false)
simplify_sha256_stanzas
end
private
attr_reader :cask_block
def_delegators :cask_block, :toplevel_stanzas, :cask_body
def simplify_sha256_stanzas
nodes = {}
sha256_on_arch_stanzas(cask_body) do |node, method, value|
2024-03-07 16:20:20 +00:00
nodes[method.to_s.delete_prefix("on_").to_sym] = { node:, value: }
end
return if !nodes.key?(:arm) || !nodes.key?(:intel)
offending_node(nodes[:arm][:node])
replacement_string = "sha256 arm: #{nodes[:arm][:value].inspect}, intel: #{nodes[:intel][:value].inspect}"
problem "Use `#{replacement_string}` instead of nesting the `sha256` stanzas in " \
"`on_intel` and `on_arm` blocks" do |corrector|
corrector.replace(nodes[:arm][:node].source_range, replacement_string)
corrector.replace(nodes[:intel][:node].source_range, "")
end
end
def_node_search :sha256_on_arch_stanzas, <<~PATTERN
$(block
(send nil? ${:on_intel :on_arm})
(args)
(send nil? :sha256
(str $_)))
PATTERN
end
end
end
end