From ab8b199be82c3191723067867c4f078dee94df89 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 20 Mar 2025 22:00:28 +0100 Subject: [PATCH] [ruby/prism] Add `Prism::Translation::ParserCurrent` It's not my favorite api but for users that currently use the same thing from `parser`, moving over is more difficult than it needs to be. If you plan to support both old and new ruby versions, you definitly need to branch somewhere on the ruby version to either choose prism or parser. But with prism you then need to enumerate all the versions again and choose the correct one. Also, don't recommend to use `Prism::Translation::Parser` in docs. It's version-less but actually always just uses Ruby 3.4 which is probably not what the user intended. Note: parser also warns when the patch version doesn't match what it expects. But I don't think prism has such a concept, and anyways it would require releases anytime ruby releases, which I don't think is very desirable https://github.com/ruby/prism/commit/77177f9e92 --- lib/prism/prism.gemspec | 1 + lib/prism/translation.rb | 1 + lib/prism/translation/parser_current.rb | 21 +++++++++++++++++++++ test/prism/ruby/parser_test.rb | 9 +++++++++ 4 files changed, 32 insertions(+) create mode 100644 lib/prism/translation/parser_current.rb diff --git a/lib/prism/prism.gemspec b/lib/prism/prism.gemspec index e602be9f0f..8c064e31b3 100644 --- a/lib/prism/prism.gemspec +++ b/lib/prism/prism.gemspec @@ -96,6 +96,7 @@ Gem::Specification.new do |spec| "lib/prism/string_query.rb", "lib/prism/translation.rb", "lib/prism/translation/parser.rb", + "lib/prism/translation/parser_current.rb", "lib/prism/translation/parser33.rb", "lib/prism/translation/parser34.rb", "lib/prism/translation/parser35.rb", diff --git a/lib/prism/translation.rb b/lib/prism/translation.rb index f5044b9e38..511c80febc 100644 --- a/lib/prism/translation.rb +++ b/lib/prism/translation.rb @@ -5,6 +5,7 @@ module Prism # syntax trees. module Translation # steep:ignore autoload :Parser, "prism/translation/parser" + autoload :ParserCurrent, "prism/translation/parser_current" autoload :Parser33, "prism/translation/parser33" autoload :Parser34, "prism/translation/parser34" autoload :Parser35, "prism/translation/parser35" diff --git a/lib/prism/translation/parser_current.rb b/lib/prism/translation/parser_current.rb new file mode 100644 index 0000000000..b5a9484666 --- /dev/null +++ b/lib/prism/translation/parser_current.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true +# typed: ignore + +module Prism + module Translation + case RUBY_VERSION + when /^3\.3\./ + ParserCurrent = Parser33 + when /^3\.4\./ + ParserCurrent = Parser34 + when /^3\.5\./ + ParserCurrent = Parser35 + else + # Keep this in sync with released Ruby. + parser = Parser34 + warn "warning: `Prism::Translation::Current` is loading #{parser.name}, " \ + "but you are running #{RUBY_VERSION.to_f}." + ParserCurrent = parser + end + end +end diff --git a/test/prism/ruby/parser_test.rb b/test/prism/ruby/parser_test.rb index 731db1763d..1324ac45e4 100644 --- a/test/prism/ruby/parser_test.rb +++ b/test/prism/ruby/parser_test.rb @@ -155,6 +155,15 @@ module Prism assert_empty(warnings) end + if RUBY_VERSION >= "3.3" + def test_current_parser_for_current_ruby + major, minor, _patch = Gem::Version.new(RUBY_VERSION).segments + # Let's just hope there never is a Ruby 3.10 or similar + expected = major * 10 + minor + assert_equal(expected, Translation::ParserCurrent.new.version) + end + end + def test_it_block_parameter_syntax it_fixture_path = Pathname(__dir__).join("../../../test/prism/fixtures/it.txt")