brew vendor-gems: commit updates.
This commit is contained in:
parent
83261c2c34
commit
9856fe006d
@ -99,7 +99,7 @@ $:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-performance-1.17.1/lib")
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-rails-2.19.1/lib")
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-rspec-2.20.0/lib")
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-sorbet-0.7.3/lib")
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/rubocop-sorbet-0.7.4/lib")
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/ruby-macho-4.0.0/lib")
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/extensions/universal-darwin-22/#{Gem.extension_api_version}/ruby-prof-1.4.3")
|
||||
$:.unshift File.expand_path("#{__dir__}/../#{RUBY_ENGINE}/#{Gem.ruby_api_version}/gems/ruby-prof-1.4.3/lib")
|
||||
|
||||
@ -97,6 +97,13 @@ Sorbet/ForbidSuperclassConstLiteral:
|
||||
Exclude:
|
||||
- db/migrate/*.rb
|
||||
|
||||
Sorbet/ForbidTStruct:
|
||||
Description: 'Forbid usage of T::Struct.'
|
||||
Enabled: false
|
||||
VersionAdded: <<next>>
|
||||
VersionChanged: <<next>>
|
||||
Safe: false
|
||||
|
||||
Sorbet/ForbidTUnsafe:
|
||||
Description: 'Forbid usage of T.unsafe.'
|
||||
Enabled: false
|
||||
@ -171,6 +178,23 @@ Sorbet/ObsoleteStrictMemoization:
|
||||
Safe: true
|
||||
SafeAutoCorrect: true
|
||||
|
||||
Sorbet/BuggyObsoleteStrictMemoization:
|
||||
Description: >-
|
||||
Checks for the a mistaken variant of the "obsolete memoization pattern" that used to be required
|
||||
for older Sorbet versions in `#typed: strict` files. The mistaken variant would overwrite the ivar with `nil`
|
||||
on every call, causing the memoized value to be discarded and recomputed on every call.
|
||||
|
||||
This cop will correct it to read from the ivar instead of `nil`, which will memoize it correctly.
|
||||
|
||||
The result of this correction will be the "obsolete memoization pattern", which can further be corrected by
|
||||
the `Sorbet/ObsoleteStrictMemoization` cop.
|
||||
|
||||
See `Sorbet/ObsoleteStrictMemoization` for more details.
|
||||
Enabled: true
|
||||
VersionAdded: '0.7.3'
|
||||
Safe: true
|
||||
SafeAutoCorrect: false
|
||||
|
||||
Sorbet/OneAncestorPerLine:
|
||||
Description: 'Enforces one ancestor per call to requires_ancestor'
|
||||
Enabled: false
|
||||
@ -0,0 +1,83 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "rubocop"
|
||||
|
||||
module RuboCop
|
||||
module Cop
|
||||
module Sorbet
|
||||
# Checks for the a mistaken variant of the "obsolete memoization pattern" that used to be required
|
||||
# for older Sorbet versions in `#typed: strict` files. The mistaken variant would overwrite the ivar with `nil`
|
||||
# on every call, causing the memoized value to be discarded and recomputed on every call.
|
||||
#
|
||||
# This cop will correct it to read from the ivar instead of `nil`, which will memoize it correctly.
|
||||
#
|
||||
# The result of this correction will be the "obsolete memoization pattern", which can further be corrected by
|
||||
# the `Sorbet/ObsoleteStrictMemoization` cop.
|
||||
#
|
||||
# See `Sorbet/ObsoleteStrictMemoization` for more details.
|
||||
#
|
||||
# @safety
|
||||
# If the computation being memoized had side effects, calling it only once (instead of once on every call
|
||||
# to the affected method) can be observed, and might be a breaking change.
|
||||
#
|
||||
# @example
|
||||
# # bad
|
||||
# sig { returns(Foo) }
|
||||
# def foo
|
||||
# # This `nil` is likely a mistake, causing the memoized value to be discarded and recomputed on every call.
|
||||
# @foo = T.let(nil, T.nilable(Foo))
|
||||
# @foo ||= some_computation
|
||||
# end
|
||||
#
|
||||
# # good
|
||||
# sig { returns(Foo) }
|
||||
# def foo
|
||||
# # This will now memoize the value as was likely intended, so `some_computation` is only ever called once.
|
||||
# # ⚠️If `some_computation` has side effects, this might be a breaking change!
|
||||
# @foo = T.let(@foo, T.nilable(Foo))
|
||||
# @foo ||= some_computation
|
||||
# end
|
||||
#
|
||||
# @see Sorbet/ObsoleteStrictMemoization
|
||||
class BuggyObsoleteStrictMemoization < RuboCop::Cop::Base
|
||||
include RuboCop::Cop::MatchRange
|
||||
include RuboCop::Cop::Alignment
|
||||
include RuboCop::Cop::LineLengthHelp
|
||||
include RuboCop::Cop::RangeHelp
|
||||
extend AutoCorrector
|
||||
|
||||
include TargetSorbetVersion
|
||||
|
||||
MSG = "This might be a mistaken variant of the two-stage workaround that used to be needed for memoization in "\
|
||||
"`#typed: strict` files. See https://sorbet.org/docs/type-assertions#put-type-assertions-behind-memoization."
|
||||
|
||||
# @!method buggy_legacy_memoization_pattern?(node)
|
||||
def_node_matcher :buggy_legacy_memoization_pattern?, <<~PATTERN
|
||||
(begin
|
||||
... # Ignore any other lines that come first.
|
||||
(ivasgn $_ivar # First line: @_ivar = ...
|
||||
(send # T.let(_ivar, T.nilable(_ivar_type))
|
||||
(const {nil? cbase} :T) :let
|
||||
$nil
|
||||
(send (const {nil? cbase} :T) :nilable _ivar_type))) # T.nilable(_ivar_type)
|
||||
(or-asgn (ivasgn _ivar) _initialization_expr)) # Second line: @_ivar ||= _initialization_expr
|
||||
PATTERN
|
||||
|
||||
def on_begin(node)
|
||||
buggy_legacy_memoization_pattern?(node) do |ivar, nil_node|
|
||||
add_offense(nil_node) do |corrector|
|
||||
corrector.replace(
|
||||
range_between(nil_node.source_range.begin_pos, nil_node.source_range.end_pos),
|
||||
ivar,
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def relevant_file?(file)
|
||||
super && sorbet_enabled?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,223 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "rubocop"
|
||||
|
||||
module RuboCop
|
||||
module Cop
|
||||
module Sorbet
|
||||
# Disallow using `T::Struct` and `T::Props`.
|
||||
#
|
||||
# @example
|
||||
#
|
||||
# # bad
|
||||
# class MyStruct < T::Struct
|
||||
# const :foo, String
|
||||
# prop :bar, Integer, default: 0
|
||||
#
|
||||
# def some_method; end
|
||||
# end
|
||||
#
|
||||
# # good
|
||||
# class MyStruct
|
||||
# extend T::Sig
|
||||
#
|
||||
# sig { returns(String) }
|
||||
# attr_reader :foo
|
||||
#
|
||||
# sig { returns(Integer) }
|
||||
# attr_accessor :bar
|
||||
#
|
||||
# sig { params(foo: String, bar: Integer) }
|
||||
# def initialize(foo:, bar: 0)
|
||||
# @foo = foo
|
||||
# @bar = bar
|
||||
# end
|
||||
#
|
||||
# def some_method; end
|
||||
# end
|
||||
class ForbidTStruct < RuboCop::Cop::Base
|
||||
include Alignment
|
||||
include RangeHelp
|
||||
include CommentsHelp
|
||||
extend AutoCorrector
|
||||
|
||||
RESTRICT_ON_SEND = [:include, :prepend, :extend].freeze
|
||||
|
||||
MSG_STRUCT = "Using `T::Struct` or its variants is deprecated."
|
||||
MSG_PROPS = "Using `T::Props` or its variants is deprecated."
|
||||
|
||||
# This class walks down the class body of a T::Struct and collects all the properties that will need to be
|
||||
# translated into `attr_reader` and `attr_accessor` methods.
|
||||
class TStructWalker
|
||||
include AST::Traversal
|
||||
extend AST::NodePattern::Macros
|
||||
|
||||
attr_reader :props, :has_extend_t_sig
|
||||
|
||||
def initialize
|
||||
@props = []
|
||||
@has_extend_t_sig = false
|
||||
end
|
||||
|
||||
# @!method extend_t_sig?(node)
|
||||
def_node_matcher :extend_t_sig?, <<~PATTERN
|
||||
(send _ :extend (const (const {nil? | cbase} :T) :Sig))
|
||||
PATTERN
|
||||
|
||||
# @!method t_struct_prop?(node)
|
||||
def_node_matcher(:t_struct_prop?, <<~PATTERN)
|
||||
(send nil? {:const :prop} ...)
|
||||
PATTERN
|
||||
|
||||
def on_send(node)
|
||||
if extend_t_sig?(node)
|
||||
# So we know we won't need to generate again a `extend T::Sig` line in the new class body
|
||||
@has_extend_t_sig = true
|
||||
return
|
||||
end
|
||||
|
||||
return unless t_struct_prop?(node)
|
||||
|
||||
kind = node.method?(:const) ? :attr_reader : :attr_accessor
|
||||
name = node.arguments[0].source.delete_prefix(":")
|
||||
type = node.arguments[1].source
|
||||
default = nil
|
||||
factory = nil
|
||||
|
||||
node.arguments[2..-1].each do |arg|
|
||||
next unless arg.hash_type?
|
||||
|
||||
arg.each_pair do |key, value|
|
||||
case key.source
|
||||
when "default"
|
||||
default = value.source
|
||||
when "factory"
|
||||
factory = value.source
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@props << Property.new(node, kind, name, type, default: default, factory: factory)
|
||||
end
|
||||
end
|
||||
|
||||
class Property
|
||||
attr_reader :node, :kind, :name, :type, :default, :factory
|
||||
|
||||
def initialize(node, kind, name, type, default:, factory:)
|
||||
@node = node
|
||||
@kind = kind
|
||||
@name = name
|
||||
@type = type
|
||||
@default = default
|
||||
@factory = factory
|
||||
|
||||
# A T::Struct should have both a default and a factory, if we find one let's raise an error
|
||||
raise if @default && @factory
|
||||
end
|
||||
|
||||
def attr_sig
|
||||
"sig { returns(#{type}) }"
|
||||
end
|
||||
|
||||
def attr_accessor
|
||||
"#{kind} :#{name}"
|
||||
end
|
||||
|
||||
def initialize_sig_param
|
||||
"#{name}: #{type}"
|
||||
end
|
||||
|
||||
def initialize_param
|
||||
rb = String.new
|
||||
rb << "#{name}:"
|
||||
rb << " #{default}" if default
|
||||
rb << " #{factory}" if factory
|
||||
rb
|
||||
end
|
||||
|
||||
def initialize_assign
|
||||
rb = String.new
|
||||
rb << "@#{name} = #{name}"
|
||||
rb << ".call" if factory
|
||||
rb
|
||||
end
|
||||
end
|
||||
|
||||
# @!method t_struct?(node)
|
||||
def_node_matcher(:t_struct?, <<~PATTERN)
|
||||
(const (const {nil? cbase} :T) {:Struct :ImmutableStruct :InexactStruct})
|
||||
PATTERN
|
||||
|
||||
# @!method t_props?(node)
|
||||
def_node_matcher(:t_props?, "(send nil? {:include :prepend :extend} `(const (const {nil? cbase} :T) :Props))")
|
||||
|
||||
def on_class(node)
|
||||
return unless t_struct?(node.parent_class)
|
||||
|
||||
add_offense(node, message: MSG_STRUCT) do |corrector|
|
||||
walker = TStructWalker.new
|
||||
walker.walk(node.body)
|
||||
|
||||
range = range_between(node.identifier.source_range.end_pos, node.parent_class.source_range.end_pos)
|
||||
corrector.remove(range)
|
||||
next if node.single_line?
|
||||
|
||||
unless walker.has_extend_t_sig
|
||||
indent = offset(node)
|
||||
corrector.insert_after(node.identifier, "\n#{indent} extend T::Sig\n")
|
||||
end
|
||||
|
||||
first_prop = walker.props.first
|
||||
walker.props.each do |prop|
|
||||
node = prop.node
|
||||
indent = offset(node)
|
||||
line_range = range_by_whole_lines(prop.node.source_range)
|
||||
new_line = prop != first_prop && !previous_line_blank?(node)
|
||||
trailing_comments = processed_source.each_comment_in_lines(line_range.line..line_range.line)
|
||||
|
||||
corrector.replace(
|
||||
line_range,
|
||||
"#{new_line ? "\n" : ""}" \
|
||||
"#{trailing_comments.map { |comment| "#{indent}#{comment.text}\n" }.join}" \
|
||||
"#{indent}#{prop.attr_sig}\n#{indent}#{prop.attr_accessor}",
|
||||
)
|
||||
end
|
||||
|
||||
last_prop = walker.props.last
|
||||
if last_prop
|
||||
indent = offset(last_prop.node)
|
||||
line_range = range_by_whole_lines(last_prop.node.source_range, include_final_newline: true)
|
||||
corrector.insert_after(line_range, initialize_method(indent, walker.props))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def on_send(node)
|
||||
return unless t_props?(node)
|
||||
|
||||
add_offense(node, message: MSG_PROPS)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def initialize_method(indent, props)
|
||||
# We sort optional keyword arguments after required ones
|
||||
props = props.sort_by { |prop| prop.default || prop.factory ? 1 : 0 }
|
||||
|
||||
string = +"\n"
|
||||
string << "#{indent}sig { params(#{props.map(&:initialize_sig_param).join(", ")}).void }\n"
|
||||
string << "#{indent}def initialize(#{props.map(&:initialize_param).join(", ")})\n"
|
||||
props.each do |prop|
|
||||
string << "#{indent} #{prop.initialize_assign}\n"
|
||||
end
|
||||
string << "#{indent}end\n"
|
||||
end
|
||||
|
||||
def previous_line_blank?(node)
|
||||
processed_source.buffer.source_line(node.source_range.line - 1).blank?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -11,21 +11,25 @@ module RuboCop
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# The version of the Sorbet static type checker required by this cop
|
||||
# Sets the version of the Sorbet static type checker required by this cop
|
||||
def minimum_target_sorbet_static_version(version)
|
||||
@minimum_target_sorbet_static_version = Gem::Version.new(version)
|
||||
end
|
||||
|
||||
def support_target_sorbet_static_version?(version)
|
||||
def supports_target_sorbet_static_version?(version)
|
||||
@minimum_target_sorbet_static_version <= Gem::Version.new(version)
|
||||
end
|
||||
end
|
||||
|
||||
def sorbet_enabled?
|
||||
!target_sorbet_static_version_from_bundler_lock_file.nil?
|
||||
end
|
||||
|
||||
def enabled_for_sorbet_static_version?
|
||||
sorbet_static_version = target_sorbet_static_version_from_bundler_lock_file
|
||||
return false unless sorbet_static_version
|
||||
|
||||
self.class.support_target_sorbet_static_version?(sorbet_static_version)
|
||||
self.class.supports_target_sorbet_static_version?(sorbet_static_version)
|
||||
end
|
||||
|
||||
def target_sorbet_static_version_from_bundler_lock_file
|
||||
@ -54,7 +54,7 @@ module RuboCop
|
||||
$(ivasgn $_ivar # First line: @_ivar = ...
|
||||
(send # T.let(_ivar, T.nilable(_ivar_type))
|
||||
$(const {nil? cbase} :T) :let
|
||||
{(ivar _ivar) nil}
|
||||
(ivar _ivar)
|
||||
(send (const {nil? cbase} :T) :nilable $_ivar_type))) # T.nilable(_ivar_type)
|
||||
$(or-asgn (ivasgn _ivar) $_initialization_expr)) # Second line: @_ivar ||= _initialization_expr
|
||||
PATTERN
|
||||
@ -26,6 +26,7 @@ module RuboCop
|
||||
# end
|
||||
#
|
||||
class RedundantExtendTSig < RuboCop::Cop::Base
|
||||
include RangeHelp
|
||||
extend AutoCorrector
|
||||
|
||||
MSG = "Do not redundantly `extend T::Sig` when it is already included in all modules."
|
||||
@ -40,7 +41,7 @@ module RuboCop
|
||||
return unless extend_t_sig?(node)
|
||||
|
||||
add_offense(node) do |corrector|
|
||||
corrector.remove(node)
|
||||
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -10,11 +10,13 @@ require_relative "sorbet/forbid_untyped_struct_props"
|
||||
require_relative "sorbet/implicit_conversion_method"
|
||||
require_relative "sorbet/one_ancestor_per_line"
|
||||
require_relative "sorbet/callback_conditionals_binding"
|
||||
require_relative "sorbet/forbid_t_struct"
|
||||
require_relative "sorbet/forbid_t_unsafe"
|
||||
require_relative "sorbet/forbid_t_untyped"
|
||||
require_relative "sorbet/redundant_extend_t_sig"
|
||||
require_relative "sorbet/type_alias_name"
|
||||
require_relative "sorbet/obsolete_strict_memoization"
|
||||
require_relative "sorbet/buggy_obsolete_strict_memoization"
|
||||
|
||||
require_relative "sorbet/rbi/forbid_extend_t_sig_helpers_in_shims"
|
||||
require_relative "sorbet/rbi/forbid_rbi_outside_of_allowed_paths"
|
||||
@ -2,6 +2,6 @@
|
||||
|
||||
module RuboCop
|
||||
module Sorbet
|
||||
VERSION = "0.7.3"
|
||||
VERSION = "0.7.4"
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user