Bump more RuboCops to Sorbet typed: strict

This commit is contained in:
Issy Long 2025-01-05 23:45:23 +00:00
parent 0c268f9234
commit 94085ebb57
No known key found for this signature in database
9 changed files with 43 additions and 20 deletions

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "forwardable" require "forwardable"
@ -33,10 +33,13 @@ module RuboCop
->(node) { node.parent == block_node } ->(node) { node.parent == block_node }
end end
@stanzas ||= block_body.each_node @stanzas ||= T.let(
.select(&:stanza?) block_body.each_node
.select(&is_stanza) .select(&:stanza?)
.map { |node| Stanza.new(node, comments) } .select(&is_stanza)
.map { |node| Stanza.new(node, comments) },
T.nilable(T::Array[Stanza]),
)
end end
end end
@ -46,17 +49,20 @@ module RuboCop
class CaskBlock < StanzaBlock class CaskBlock < StanzaBlock
extend Forwardable extend Forwardable
sig { returns(RuboCop::AST::BlockNode) }
def cask_node def cask_node
block_node block_node
end end
def_delegator :cask_node, :block_body, :cask_body def_delegator :cask_node, :block_body, :cask_body
sig { returns(CaskHeader) }
def header def header
@header ||= CaskHeader.new(block_node.method_node) @header ||= T.let(CaskHeader.new(block_node.method_node), T.nilable(CaskHeader))
end end
# TODO: Use `StanzaBlock#stanzas` for all cops, where possible. # TODO: Use `StanzaBlock#stanzas` for all cops, where possible.
sig { returns(T::Array[Stanza]) }
def stanzas def stanzas
return [] unless cask_body return [] unless cask_body
@ -65,6 +71,7 @@ module RuboCop
.map { |node| Stanza.new(node, comments) } .map { |node| Stanza.new(node, comments) }
end end
sig { returns(T::Array[Stanza]) }
def toplevel_stanzas def toplevel_stanzas
# If a `cask` block only contains one stanza, it is that stanza's direct parent, # If a `cask` block only contains one stanza, it is that stanza's direct parent,
# otherwise stanzas are grouped in a block and `cask` is that block's parent. # otherwise stanzas are grouped in a block and `cask` is that block's parent.
@ -74,7 +81,7 @@ module RuboCop
->(stanza) { stanza.parent_node.cask_block? } ->(stanza) { stanza.parent_node.cask_block? }
end end
@toplevel_stanzas ||= stanzas.select(&is_toplevel_stanza) @toplevel_stanzas ||= T.let(stanzas.select(&is_toplevel_stanza), T.nilable(T::Array[Stanza]))
end end
end end
end end

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "rubocops/cask/mixin/on_desc_stanza" require "rubocops/cask/mixin/on_desc_stanza"
@ -14,8 +14,9 @@ module RuboCop
include DescHelper include DescHelper
extend AutoCorrector extend AutoCorrector
sig { params(stanza: RuboCop::Cask::AST::Stanza).void }
def on_desc_stanza(stanza) def on_desc_stanza(stanza)
@name = cask_block.header.cask_token @name = T.let(cask_block.header.cask_token, T.nilable(String))
desc_call = stanza.stanza_node desc_call = stanza.stanza_node
audit_desc(:cask, @name, desc_call) audit_desc(:cask, @name, desc_call)
end end

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "forwardable" require "forwardable"
@ -17,8 +17,9 @@ module RuboCop
MSG_NO_SLASH = "'%<url>s' must have a slash after the domain." MSG_NO_SLASH = "'%<url>s' must have a slash after the domain."
sig { params(stanza: RuboCop::Cask::AST::Stanza).void }
def on_homepage_stanza(stanza) def on_homepage_stanza(stanza)
@name = cask_block.header.cask_token @name = T.let(cask_block.header.cask_token, T.nilable(String))
desc_call = stanza.stanza_node desc_call = stanza.stanza_node
url_node = desc_call.first_argument url_node = desc_call.first_argument

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop
@ -9,8 +9,9 @@ module RuboCop
extend Forwardable extend Forwardable
include CaskHelp include CaskHelp
sig { override.params(cask_block: T.nilable(RuboCop::Cask::AST::CaskBlock)).void }
def on_cask(cask_block) def on_cask(cask_block)
@cask_block = cask_block @cask_block = T.let(cask_block, T.nilable(RuboCop::Cask::AST::CaskBlock))
toplevel_stanzas.select(&:url?).each do |stanza| toplevel_stanzas.select(&:url?).each do |stanza|
on_url_stanza(stanza) on_url_stanza(stanza)
@ -19,6 +20,7 @@ module RuboCop
private private
sig { returns(T.nilable(RuboCop::Cask::AST::CaskBlock)) }
attr_reader :cask_block attr_reader :cask_block
def_delegators :cask_block, def_delegators :cask_block,

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop
@ -17,6 +17,7 @@ module RuboCop
"Add it to an `on_{system}` block instead. " \ "Add it to an `on_{system}` block instead. " \
"Use `:or_older` or `:or_newer` to specify a range of macOS versions." "Use `:or_older` or `:or_newer` to specify a range of macOS versions."
sig { override.params(cask_block: RuboCop::Cask::AST::CaskBlock).void }
def on_cask(cask_block) def on_cask(cask_block)
cask_stanzas = cask_block.toplevel_stanzas cask_stanzas = cask_block.toplevel_stanzas
@ -34,8 +35,9 @@ module RuboCop
end end
end end
sig { params(on_system: T::Array[RuboCop::Cask::AST::Stanza]).returns(T::Set[Symbol]) }
def on_system_stanzas(on_system) def on_system_stanzas(on_system)
names = Set.new names = T.let(Set.new, T::Set[Symbol])
method_nodes = on_system.map(&:method_node) method_nodes = on_system.map(&:method_node)
method_nodes.select(&:block_type?).each do |node| method_nodes.select(&:block_type?).each do |node|
node.child_nodes.each do |child| node.child_nodes.each do |child|
@ -56,14 +58,17 @@ module RuboCop
names names
end end
sig { params(node: RuboCop::AST::Node).returns(T::Boolean) }
def inside_livecheck_defined?(node) def inside_livecheck_defined?(node)
single_stanza_livecheck_defined?(node) || multi_stanza_livecheck_defined?(node) single_stanza_livecheck_defined?(node) || multi_stanza_livecheck_defined?(node)
end end
sig { params(node: RuboCop::AST::Node).returns(T::Boolean) }
def single_stanza_livecheck_defined?(node) def single_stanza_livecheck_defined?(node)
node.parent.block_type? && node.parent.method_name == :livecheck node.parent.block_type? && node.parent.method_name == :livecheck
end end
sig { params(node: RuboCop::AST::Node).returns(T::Boolean) }
def multi_stanza_livecheck_defined?(node) def multi_stanza_livecheck_defined?(node)
grandparent_node = node.parent.parent grandparent_node = node.parent.parent
node.parent.begin_type? && grandparent_node.block_type? && grandparent_node.method_name == :livecheck node.parent.begin_type? && grandparent_node.block_type? && grandparent_node.method_name == :livecheck

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "forwardable" require "forwardable"
@ -34,8 +34,9 @@ module RuboCop
FLIGHT_STANZA_NAMES = [:preflight, :postflight, :uninstall_preflight, :uninstall_postflight].freeze FLIGHT_STANZA_NAMES = [:preflight, :postflight, :uninstall_preflight, :uninstall_postflight].freeze
sig { override.params(cask_block: RuboCop::Cask::AST::CaskBlock).void }
def on_cask(cask_block) def on_cask(cask_block)
@cask_block = cask_block @cask_block = T.let(cask_block, T.nilable(RuboCop::Cask::AST::CaskBlock))
toplevel_stanzas.each do |stanza| toplevel_stanzas.each do |stanza|
next unless FLIGHT_STANZA_NAMES.include? stanza.stanza_name next unless FLIGHT_STANZA_NAMES.include? stanza.stanza_name
@ -50,10 +51,12 @@ module RuboCop
private private
sig { returns(T.nilable(RuboCop::Cask::AST::CaskBlock)) }
attr_reader :cask_block attr_reader :cask_block
def_delegators :cask_block, :toplevel_stanzas, :cask_body def_delegators :cask_block, :toplevel_stanzas, :cask_body
sig { void }
def simplify_sha256_stanzas def simplify_sha256_stanzas
nodes = {} nodes = {}

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop
@ -12,6 +12,7 @@ module RuboCop
MSG_CSV = "Use `version.csv.first` instead of `version.before_comma` " \ MSG_CSV = "Use `version.csv.first` instead of `version.before_comma` " \
"and `version.csv.second` instead of `version.after_comma`." "and `version.csv.second` instead of `version.after_comma`."
sig { override.params(stanza: RuboCop::Cask::AST::Stanza).void }
def on_url_stanza(stanza) def on_url_stanza(stanza)
return if stanza.stanza_node.type == :block return if stanza.stanza_node.type == :block

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop
@ -9,6 +9,7 @@ module RuboCop
MSG = "Only use `Homebrew.install_bundler_gems!` in dev-cmd." MSG = "Only use `Homebrew.install_bundler_gems!` in dev-cmd."
RESTRICT_ON_SEND = [:install_bundler_gems!].freeze RESTRICT_ON_SEND = [:install_bundler_gems!].freeze
sig { params(node: RuboCop::AST::Node).void }
def on_send(node) def on_send(node)
file_path = processed_source.file_path file_path = processed_source.file_path
return if file_path.match?(%r{/(dev-cmd/.+|standalone/init|startup/bootsnap)\.rb\z}) return if file_path.match?(%r{/(dev-cmd/.+|standalone/init|startup/bootsnap)\.rb\z})

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module RuboCop module RuboCop
@ -30,6 +30,7 @@ module RuboCop
(send !nil? :rmtree ...) (send !nil? :rmtree ...)
PATTERN PATTERN
sig { params(node: RuboCop::AST::SendNode).void }
def on_send(node) def on_send(node)
return if neither_rm_rf_nor_rmtree?(node) return if neither_rm_rf_nor_rmtree?(node)
@ -51,6 +52,7 @@ module RuboCop
end end
end end
sig { params(node: RuboCop::AST::SendNode).returns(T::Boolean) }
def neither_rm_rf_nor_rmtree?(node) def neither_rm_rf_nor_rmtree?(node)
!any_receiver_rm_r_f?(node) && !no_receiver_rm_r_f?(node) && !any_receiver_rm_r_f?(node) && !no_receiver_rm_r_f?(node) &&
!any_receiver_rmtree?(node) && !no_receiver_rmtree?(node) !any_receiver_rmtree?(node) && !no_receiver_rmtree?(node)