summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEarlopain <[email protected]>2025-03-20 22:00:28 +0100
committergit <[email protected]>2025-03-20 21:20:23 +0000
commitab8b199be82c3191723067867c4f078dee94df89 (patch)
treecf37fdea021c4d6ec6057c74795c6203be429cb6 /lib
parentbfe6068417ca41a6b88a1ba5fcde04f9a76718a7 (diff)
[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
Diffstat (limited to 'lib')
-rw-r--r--lib/prism/prism.gemspec1
-rw-r--r--lib/prism/translation.rb1
-rw-r--r--lib/prism/translation/parser_current.rb21
3 files changed, 23 insertions, 0 deletions
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