[ruby/prism] Polyfill Kernel#warn category parameter

https://github.com/ruby/prism/commit/d85c72a1b9
This commit is contained in:
Kevin Newton 2025-03-19 14:54:12 -04:00 committed by git
parent b5e9a2da4c
commit 050ffab82b
5 changed files with 46 additions and 2 deletions

View File

@ -68,6 +68,7 @@ module Prism
end
require_relative "prism/polyfill/byteindex"
require_relative "prism/polyfill/warn"
require_relative "prism/node"
require_relative "prism/node_ext"
require_relative "prism/parse_result"

View File

@ -9,7 +9,7 @@ module Prism
location = location[0].label if location
suggest = replacements.map { |replacement| "#{self.class}##{replacement}" }
warn(<<~MSG)
warn(<<~MSG, uplevel: 1, category: :deprecated)
[deprecation]: #{self.class}##{location} is deprecated and will be \
removed in the next major version. Use #{suggest.join("/")} instead.
#{(caller(1, 3) || []).join("\n")}

View File

@ -0,0 +1,42 @@
# frozen_string_literal: true
# Polyfill for Kernel#warn with the category parameter. Not all Ruby engines
# have Method#parameters implemented, so we check the arity instead if
# necessary.
if (method = Kernel.instance_method(:warn)).respond_to?(:parameters) ? method.parameters.none? { |_, name| name == :category } : (method.arity == -1)
Kernel.prepend(
Module.new {
def warn(*msgs, uplevel: nil, category: nil) # :nodoc:
uplevel =
case uplevel
when nil
1
when Integer
uplevel + 1
else
uplevel.to_int + 1
end
super(*msgs, uplevel: uplevel)
end
}
)
Object.prepend(
Module.new {
def warn(*msgs, uplevel: nil, category: nil) # :nodoc:
uplevel =
case uplevel
when nil
1
when Integer
uplevel + 1
else
uplevel.to_int + 1
end
super(*msgs, uplevel: uplevel)
end
}
)
end

View File

@ -89,6 +89,7 @@ Gem::Specification.new do |spec|
"lib/prism/polyfill/append_as_bytes.rb",
"lib/prism/polyfill/byteindex.rb",
"lib/prism/polyfill/unpack1.rb",
"lib/prism/polyfill/warn.rb",
"lib/prism/reflection.rb",
"lib/prism/relocation.rb",
"lib/prism/serialize.rb",

View File

@ -60,7 +60,7 @@ module Prism
#
def initialize(builder = Prism::Translation::Parser::Builder.new, parser: Prism)
if !builder.is_a?(Prism::Translation::Parser::Builder)
warn(<<~MSG, uplevel: 1)
warn(<<~MSG, uplevel: 1, category: :deprecated)
[deprecation]: The builder passed to `Prism::Translation::Parser.new` is not a \
`Prism::Translation::Parser::Builder` subclass. This will raise in the next major version.
MSG