Skip to content

Commit ab8b199

Browse files
Earlopainmatzbot
authored andcommitted
[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 ruby/prism@77177f9e92
1 parent bfe6068 commit ab8b199

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

lib/prism/prism.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Gem::Specification.new do |spec|
9696
"lib/prism/string_query.rb",
9797
"lib/prism/translation.rb",
9898
"lib/prism/translation/parser.rb",
99+
"lib/prism/translation/parser_current.rb",
99100
"lib/prism/translation/parser33.rb",
100101
"lib/prism/translation/parser34.rb",
101102
"lib/prism/translation/parser35.rb",

lib/prism/translation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Prism
55
# syntax trees.
66
module Translation # steep:ignore
77
autoload :Parser, "prism/translation/parser"
8+
autoload :ParserCurrent, "prism/translation/parser_current"
89
autoload :Parser33, "prism/translation/parser33"
910
autoload :Parser34, "prism/translation/parser34"
1011
autoload :Parser35, "prism/translation/parser35"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
# typed: ignore
3+
4+
module Prism
5+
module Translation
6+
case RUBY_VERSION
7+
when /^3\.3\./
8+
ParserCurrent = Parser33
9+
when /^3\.4\./
10+
ParserCurrent = Parser34
11+
when /^3\.5\./
12+
ParserCurrent = Parser35
13+
else
14+
# Keep this in sync with released Ruby.
15+
parser = Parser34
16+
warn "warning: `Prism::Translation::Current` is loading #{parser.name}, " \
17+
"but you are running #{RUBY_VERSION.to_f}."
18+
ParserCurrent = parser
19+
end
20+
end
21+
end

test/prism/ruby/parser_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ def test_non_prism_builder_class_deprecated
155155
assert_empty(warnings)
156156
end
157157

158+
if RUBY_VERSION >= "3.3"
159+
def test_current_parser_for_current_ruby
160+
major, minor, _patch = Gem::Version.new(RUBY_VERSION).segments
161+
# Let's just hope there never is a Ruby 3.10 or similar
162+
expected = major * 10 + minor
163+
assert_equal(expected, Translation::ParserCurrent.new.version)
164+
end
165+
end
166+
158167
def test_it_block_parameter_syntax
159168
it_fixture_path = Pathname(__dir__).join("../../../test/prism/fixtures/it.txt")
160169

0 commit comments

Comments
 (0)