[ruby/prism] Warn when the parser translator receives an incompatible builder class

In https://github.com/ruby/prism/pull/3494 I added a bit of code
so that using the new builder doesn't break stuff.
This code can be dropped when it is enforced that builder
is _always_ the correct subclass (and makes future issues like that unlikely).

https://github.com/ruby/prism/commit/193d4b806d
This commit is contained in:
Earlopain 2025-03-19 16:40:58 +01:00 committed by git
parent 4e11ea4268
commit e5e160475b
3 changed files with 25 additions and 8 deletions

View File

@ -59,6 +59,12 @@ module Prism
# should be implemented as needed.
#
def initialize(builder = Prism::Translation::Parser::Builder.new, parser: Prism)
if !builder.is_a?(Prism::Translation::Parser::Builder)
warn(<<~MSG, uplevel: 1)
[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
end
@parser = parser
super(builder)

View File

@ -145,6 +145,16 @@ module Prism
end
end
def test_non_prism_builder_class_deprecated
warnings = capture_warnings { Prism::Translation::Parser33.new(Parser::Builders::Default.new) }
assert_include(warnings, "#{__FILE__}:#{__LINE__ - 2}")
assert_include(warnings, "is not a `Prism::Translation::Parser::Builder` subclass")
warnings = capture_warnings { Prism::Translation::Parser33.new }
assert_empty(warnings)
end
def test_it_block_parameter_syntax
it_fixture_path = Pathname(__dir__).join("../../../test/prism/fixtures/it.txt")

View File

@ -314,15 +314,16 @@ module Prism
end
end
def ignore_warnings
previous = $VERBOSE
$VERBOSE = nil
def capture_warnings
$stderr = StringIO.new
yield
$stderr.string
ensure
$stderr = STDERR
end
begin
yield
ensure
$VERBOSE = previous
end
def ignore_warnings
capture_warnings { return yield }
end
end
end