summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEarlopain <14981592+Earlopain@users.noreply.github.com>2026-02-04 14:31:32 +0100
committergit <svn-admin@ruby-lang.org>2026-02-04 14:58:08 +0000
commit673c37d76ac2767b13a1ab7ba74b0ea8cda17c81 (patch)
tree7447e58dbe4772161e957b1646f38d0366fb47be /lib
parent1258992bdff8818d3ce868e98ccc3496069d996d (diff)
[ruby/prism] Rewrite "version: nearest" to require no maintenance
Currently I see myself not updating this when a new version is added. Instead, rewrite it to just work with new versions: * Try to set the version * If that doesn't succeed, check if it is lower * If it isn't lower, it must be higher We can then use `PM_OPTIONS_VERSION_LATEST` which doesn't need to change. Also added a very basic test. https://github.com/ruby/prism/commit/443e9b9959
Diffstat (limited to 'lib')
-rw-r--r--lib/prism/ffi.rb43
1 files changed, 19 insertions, 24 deletions
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb
index 57d878a33f..618324ded6 100644
--- a/lib/prism/ffi.rb
+++ b/lib/prism/ffi.rb
@@ -423,27 +423,28 @@ module Prism
# Return the value that should be dumped for the version option.
def dump_options_version(version)
- checking =
- case version
- when "current"
- RUBY_VERSION
- when "latest"
- nil
- when "nearest"
- if RUBY_VERSION <= "3.3"
- "3.3"
- elsif RUBY_VERSION >= "4.1"
- "4.1"
- else
- RUBY_VERSION
- end
+ case version
+ when "current"
+ version_string_to_number(RUBY_VERSION) || raise(CurrentVersionError, RUBY_VERSION)
+ when "latest", nil
+ 0 # Handled in pm_parser_init
+ when "nearest"
+ dump = version_string_to_number(RUBY_VERSION)
+ return dump if dump
+ if RUBY_VERSION < "3.3"
+ version_string_to_number("3.3")
else
- version
+ 0 # Handled in pm_parser_init
end
+ else
+ version_string_to_number(version) || raise(ArgumentError, "invalid version: #{version}")
+ end
+ end
- case checking
- when nil
- 0 # Handled in pm_parser_init
+ # Converts a version string like "4.0.0" or "4.0" into a number.
+ # Returns nil if the version is unknown.
+ def version_string_to_number(version)
+ case version
when /\A3\.3(\.\d+)?\z/
1
when /\A3\.4(\.\d+)?\z/
@@ -452,12 +453,6 @@ module Prism
3
when /\A4\.1(\.\d+)?\z/
4
- else
- if version == "current"
- raise CurrentVersionError, RUBY_VERSION
- else
- raise ArgumentError, "invalid version: #{version}"
- end
end
end