From e5e160475bbd94ab90525132f7987cf29e9afc11 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Wed, 19 Mar 2025 16:40:58 +0100 Subject: [PATCH] [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 --- lib/prism/translation/parser.rb | 6 ++++++ test/prism/ruby/parser_test.rb | 10 ++++++++++ test/prism/test_helper.rb | 17 +++++++++-------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/prism/translation/parser.rb b/lib/prism/translation/parser.rb index 5f2f01dbda..e91d13143f 100644 --- a/lib/prism/translation/parser.rb +++ b/lib/prism/translation/parser.rb @@ -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) diff --git a/test/prism/ruby/parser_test.rb b/test/prism/ruby/parser_test.rb index e7cd06bf32..731db1763d 100644 --- a/test/prism/ruby/parser_test.rb +++ b/test/prism/ruby/parser_test.rb @@ -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") diff --git a/test/prism/test_helper.rb b/test/prism/test_helper.rb index c5afdc0ea5..8a137cb636 100644 --- a/test/prism/test_helper.rb +++ b/test/prism/test_helper.rb @@ -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