Skip to content

Add Prism::Translation::ParserCurrent#3508

Merged
kddnewton merged 1 commit intoruby:mainfrom
Earlopain:parser-current-ruby
Mar 20, 2025
Merged

Add Prism::Translation::ParserCurrent#3508
kddnewton merged 1 commit intoruby:mainfrom
Earlopain:parser-current-ruby

Conversation

@Earlopain
Copy link
Collaborator

@Earlopain Earlopain commented Mar 20, 2025

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.

I wrote something on how to migrate to parser/prism in whitequark/parser#1072 and it contains the following snippet:

code_version = RUBY_VERSION.to_f

if code_version <= 3.3
  require 'parser/current'
  parser = Parser::CurrentRuby
else
  require 'prism'
  case code_version
  when 3.3
    Prism::Translation::Parser33
  when 3.4
    Prism::Translation::Parser34
  else
    warn "Unknown Ruby version #{code_version}, using 3.4 as a fallback"
    Prism::Translation::Parser34
  end
end

With this change, it can be written like this (and doesn't require changes from the programmer when new rubies get released)

code_version = RUBY_VERSION.to_f


if code_version <= 3.3
  require 'parser/current'
  parser = Parser::CurrentRuby
else
  require 'prism'
  parser = Prism::Translation::ParserCurrent
end

@Earlopain Earlopain force-pushed the parser-current-ruby branch 2 times, most recently from cede790 to c3cee1c Compare March 20, 2025 20:43
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
Copy link
Collaborator

@kddnewton kddnewton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this seems fine

@kddnewton kddnewton merged commit a37a989 into ruby:main Mar 20, 2025
57 checks passed
@Earlopain
Copy link
Collaborator Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants