[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
This commit is contained in:
Earlopain 2025-03-20 22:00:28 +01:00 committed by git
parent bfe6068417
commit ab8b199be8
4 changed files with 32 additions and 0 deletions

View File

@ -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",

View File

@ -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"

View File

@ -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

View File

@ -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")