Bump more Cask RuboCops to Sorbet typed: strict

- This includes a new Tapioca compiler for
  `RuboCop::Cask::AST::Stanza` dynamic methods like
  `caveats?`.
This commit is contained in:
Issy Long 2025-01-22 23:17:22 +00:00
parent fa40d2f2fd
commit 9a97456767
No known key found for this signature in database
13 changed files with 254 additions and 24 deletions

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
require "forwardable"
@ -12,23 +12,34 @@ module RuboCop
class Stanza
extend Forwardable
sig {
params(
method_node: T.any(RuboCop::AST::SendNode, RuboCop::AST::BlockNode),
all_comments: T::Array[T.any(String, Parser::Source::Comment)],
).void
}
def initialize(method_node, all_comments)
@method_node = method_node
@all_comments = all_comments
@method_node = T.let(method_node, T.any(RuboCop::AST::SendNode, RuboCop::AST::BlockNode))
@all_comments = T.let(all_comments, T::Array[T.any(String, Parser::Source::Comment)])
end
attr_reader :method_node, :all_comments
sig { returns(T.any(RuboCop::AST::SendNode, RuboCop::AST::BlockNode)) }
attr_reader :method_node
alias stanza_node method_node
sig { returns(T::Array[T.any(Parser::Source::Comment, String)]) }
attr_reader :all_comments
def_delegator :stanza_node, :parent, :parent_node
def_delegator :stanza_node, :arch_variable?
def_delegator :stanza_node, :on_system_block?
sig { returns(Parser::Source::Range) }
def source_range
stanza_node.location_expression
end
sig { returns(Parser::Source::Range) }
def source_range_with_comments
comments.reduce(source_range) do |range, comment|
range.join(comment.loc.expression)
@ -39,34 +50,47 @@ module RuboCop
def_delegator :source_range_with_comments, :source,
:source_with_comments
sig { returns(Symbol) }
def stanza_name
return :on_arch_conditional if arch_variable?
stanza_node.method_name
end
sig { returns(T.nilable(T::Array[Symbol])) }
def stanza_group
Constants::STANZA_GROUP_HASH[stanza_name]
end
sig { returns(T.nilable(Integer)) }
def stanza_index
Constants::STANZA_ORDER.index(stanza_name)
end
sig { params(other: Stanza).returns(T::Boolean) }
def same_group?(other)
stanza_group == other.stanza_group
end
sig { returns(T::Array[Parser::Source::Comment]) }
def comments
@comments ||= stanza_node.each_node.reduce([]) do |comments, node|
comments | comments_hash[node.loc]
end
@comments ||= T.let(
stanza_node.each_node.reduce([]) do |comments, node|
comments | comments_hash[node.loc]
end,
T.nilable(T::Array[Parser::Source::Comment]),
)
end
sig { returns(T::Hash[Parser::Source::Range, T::Array[Parser::Source::Comment]]) }
def comments_hash
@comments_hash ||= Parser::Source::Comment.associate_locations(stanza_node.parent, all_comments)
@comments_hash ||= T.let(
Parser::Source::Comment.associate_locations(stanza_node.parent, all_comments),
T.nilable(T::Hash[Parser::Source::Range, T::Array[Parser::Source::Comment]]),
)
end
sig { params(other: T.untyped).returns(T::Boolean) }
def ==(other)
self.class == other.class && stanza_node == other.stanza_node
end

View File

@ -16,7 +16,7 @@ module RuboCop
sig { params(stanza: RuboCop::Cask::AST::Stanza).void }
def on_desc_stanza(stanza)
@name = T.let(cask_block.header.cask_token, T.nilable(String))
@name = T.let(cask_block&.header&.cask_token, T.nilable(String))
desc_call = stanza.stanza_node
audit_desc(:cask, @name, desc_call)
end

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
module RuboCop
@ -11,6 +11,7 @@ module RuboCop
MESSAGE = "Use `deprecate!` instead of `caveats { discontinued }`."
sig { override.params(stanza_block: RuboCop::Cask::AST::StanzaBlock).void }
def on_cask_stanza_block(stanza_block)
stanza_block.stanzas.select(&:caveats?).each do |stanza|
find_discontinued_method_call(stanza.stanza_node) do |node|

View File

@ -6,7 +6,7 @@ module RuboCop
class Discontinued < Base
sig {
params(
base_node: RuboCop::AST::BlockNode,
base_node: T.any(RuboCop::AST::BlockNode, RuboCop::AST::SendNode),
block: T.nilable(T.proc.params(node: RuboCop::AST::SendNode).void),
).returns(T::Boolean)
}
@ -14,7 +14,7 @@ module RuboCop
sig {
params(
base_node: RuboCop::AST::BlockNode,
base_node: T.any(RuboCop::AST::BlockNode, RuboCop::AST::SendNode),
block: T.proc.params(node: RuboCop::AST::SendNode).void,
).void
}

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
module RuboCop
@ -20,6 +20,7 @@ module RuboCop
(on_system_block? && each_ancestor.any?(&:cask_block?)) || false
end
sig { returns(T::Boolean) }
def stanza?
return true if arch_variable?
@ -30,10 +31,12 @@ module RuboCop
end
end
sig { returns(T::Boolean) }
def heredoc?
loc.is_a?(Parser::Source::Map::Heredoc)
end
sig { returns(Parser::Source::Range) }
def location_expression
base_expression = loc.expression
descendants.select(&:heredoc?).reduce(base_expression) do |expr, node|

View File

@ -19,7 +19,7 @@ module RuboCop
sig { params(stanza: RuboCop::Cask::AST::Stanza).void }
def on_homepage_stanza(stanza)
@name = T.let(cask_block.header.cask_token, T.nilable(String))
@name = T.let(cask_block&.header&.cask_token, T.nilable(String))
desc_call = stanza.stanza_node
url_node = desc_call.first_argument

View File

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

View File

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

View File

@ -26,7 +26,7 @@ module RuboCop
return if (on_blocks = on_system_methods(cask_stanzas)).none?
on_blocks.map(&:method_node).select(&:block_type?).each do |on_block|
stanzas = inner_stanzas(on_block, processed_source.comments)
stanzas = inner_stanzas(T.cast(on_block, RuboCop::AST::BlockNode), processed_source.comments)
add_offenses(stanzas)
end
end

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
require "rubocops/shared/url_helper"
@ -24,6 +24,7 @@ module RuboCop
include OnUrlStanza
include UrlHelper
sig { params(stanza: RuboCop::Cask::AST::Stanza).void }
def on_url_stanza(stanza)
if stanza.stanza_node.block_type?
if cask_tap == "homebrew-cask"

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
require "forwardable"
@ -26,8 +26,9 @@ module RuboCop
extend AutoCorrector
include CaskHelp
sig { override.params(cask_block: RuboCop::Cask::AST::CaskBlock).void }
def on_cask(cask_block)
@cask_block = cask_block
@cask_block = T.let(cask_block, T.nilable(RuboCop::Cask::AST::CaskBlock))
add_offenses
end
@ -35,6 +36,7 @@ module RuboCop
def_delegator :@cask_block, :cask_node
sig { void }
def add_offenses
variable_assignment(cask_node) do |node, var_name, arch_condition, true_node, false_node|
arm_node, intel_node = if arch_condition == :arm?
@ -59,10 +61,11 @@ module RuboCop
end
end
sig { params(node: RuboCop::AST::Node).returns(T::Boolean) }
def blank_node?(node)
case node.type
when :str
node.value.empty?
node.str_content.empty?
when :nil
true
else

View File

@ -6,18 +6,186 @@
class RuboCop::Cask::AST::Stanza
sig { returns(T::Boolean) }
def app?; end
sig { returns(T::Boolean) }
def appcast?; end
sig { returns(T::Boolean) }
def arch?; end
sig { params(args: T.untyped, block: T.untyped).returns(T::Boolean) }
def arch_variable?(*args, &block); end
sig { returns(T::Boolean) }
def artifact?; end
sig { returns(T::Boolean) }
def audio_unit_plugin?; end
sig { returns(T::Boolean) }
def auto_updates?; end
sig { returns(T::Boolean) }
def binary?; end
sig { returns(T::Boolean) }
def caveats?; end
sig { returns(T::Boolean) }
def colorpicker?; end
sig { returns(T::Boolean) }
def conflicts_with?; end
sig { returns(T::Boolean) }
def container?; end
sig { returns(T::Boolean) }
def depends_on?; end
sig { returns(T::Boolean) }
def desc?; end
sig { returns(T::Boolean) }
def dictionary?; end
sig { returns(T::Boolean) }
def font?; end
sig { returns(T::Boolean) }
def homepage?; end
sig { returns(T::Boolean) }
def input_method?; end
sig { returns(T::Boolean) }
def installer?; end
sig { returns(T::Boolean) }
def internet_plugin?; end
sig { returns(T::Boolean) }
def keyboard_layout?; end
sig { returns(T::Boolean) }
def language?; end
sig { returns(T::Boolean) }
def livecheck?; end
sig { returns(T::Boolean) }
def manpage?; end
sig { returns(T::Boolean) }
def mdimporter?; end
sig { returns(T::Boolean) }
def name?; end
sig { returns(T::Boolean) }
def on_arch_conditional?; end
sig { returns(T::Boolean) }
def on_arm?; end
sig { returns(T::Boolean) }
def on_big_sur?; end
sig { returns(T::Boolean) }
def on_catalina?; end
sig { returns(T::Boolean) }
def on_el_capitan?; end
sig { returns(T::Boolean) }
def on_high_sierra?; end
sig { returns(T::Boolean) }
def on_intel?; end
sig { returns(T::Boolean) }
def on_mojave?; end
sig { returns(T::Boolean) }
def on_monterey?; end
sig { returns(T::Boolean) }
def on_sequoia?; end
sig { returns(T::Boolean) }
def on_sierra?; end
sig { returns(T::Boolean) }
def on_sonoma?; end
sig { params(args: T.untyped, block: T.untyped).returns(T::Boolean) }
def on_system_block?(*args, &block); end
sig { returns(T::Boolean) }
def on_ventura?; end
sig { params(args: T.untyped, block: T.untyped).returns(T.untyped) }
def parent_node(*args, &block); end
sig { returns(T::Boolean) }
def pkg?; end
sig { returns(T::Boolean) }
def postflight?; end
sig { returns(T::Boolean) }
def preflight?; end
sig { returns(T::Boolean) }
def prefpane?; end
sig { returns(T::Boolean) }
def qlplugin?; end
sig { returns(T::Boolean) }
def screen_saver?; end
sig { returns(T::Boolean) }
def service?; end
sig { returns(T::Boolean) }
def sha256?; end
sig { params(args: T.untyped, block: T.untyped).returns(T.untyped) }
def source(*args, &block); end
sig { params(args: T.untyped, block: T.untyped).returns(T.untyped) }
def source_with_comments(*args, &block); end
sig { returns(T::Boolean) }
def stage_only?; end
sig { returns(T::Boolean) }
def suite?; end
sig { returns(T::Boolean) }
def uninstall?; end
sig { returns(T::Boolean) }
def uninstall_postflight?; end
sig { returns(T::Boolean) }
def uninstall_preflight?; end
sig { returns(T::Boolean) }
def url?; end
sig { returns(T::Boolean) }
def version?; end
sig { returns(T::Boolean) }
def vst3_plugin?; end
sig { returns(T::Boolean) }
def vst_plugin?; end
sig { returns(T::Boolean) }
def zap?; end
end

View File

@ -0,0 +1,26 @@
# typed: strict
# frozen_string_literal: true
require "method_source"
require "rubocop"
require_relative "../../../rubocops"
module Tapioca
module Compilers
class Stanza < Tapioca::Dsl::Compiler
ConstantType = type_member { { fixed: Module } }
sig { override.returns(T::Enumerable[Module]) }
def self.gather_constants = [::RuboCop::Cask::AST::Stanza]
sig { override.void }
def decorate
root.create_module(T.must(constant.name)) do |mod|
::RuboCop::Cask::Constants::STANZA_ORDER.each do |stanza|
mod.create_method("#{stanza}?", return_type: "T::Boolean", class_method: false)
end
end
end
end
end
end