Skip to content

Conversation

Earlopain
Copy link
Contributor

@Earlopain Earlopain commented Mar 20, 2025

One step further towards rubocop/rubocop#13617

In RuboCop, the parser engine in the default config can be set to default, and with Ruby 3.5, RuboCop will then automatically choose prism to do its job.

More discussion and considerations in #371

@Earlopain Earlopain force-pushed the use-prism-automatically branch from 6c19492 to 2685ff7 Compare March 20, 2025 12:45
@Earlopain Earlopain force-pushed the use-prism-automatically branch from 2685ff7 to a4b5f25 Compare March 21, 2025 08:37
@Earlopain Earlopain force-pushed the use-prism-automatically branch from a4b5f25 to 1b7ec6d Compare March 21, 2025 09:19
@Earlopain Earlopain changed the title Automatically use prism with the translation parser on Ruby >= 3.4 Also support Prism::Translation::Parser35 for Ruby 3.5 parser Mar 21, 2025
@@ -35,25 +35,21 @@ class ProcessedSource # rubocop:disable Metrics/ClassLength
INVALID_LEVELS = %i[error fatal].freeze
private_constant :INVALID_LEVELS

PARSER_ENGINES = %i[parser_whitequark parser_prism].freeze
PARSER_ENGINES = %i[automatic parser_whitequark parser_prism].freeze
Copy link
Member

Choose a reason for hiding this comment

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

The term "automatic" might not be the most suitable. "default" might be more preferable.

Suggested change
PARSER_ENGINES = %i[automatic parser_whitequark parser_prism].freeze
PARSER_ENGINES = %i[default parser_whitequark parser_prism].freeze

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if default is actually better but I also don't like automatic particularly.

In the original version, I opted to simply use nil to signify this instead. But that has backwards compatibility concerns, where it might choose prism when it previously wouldn't when the user doesn't pass parser_engine to the method at all.

I changed it to :default.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I'm not sure if default is the most suitable choice, but it's a simple term. It's understood that this is a parameter for compatibility and it would be nice if a better name were suggested :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, most users won't have to configure this. So I wouldn't think too much about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed.

@Earlopain Earlopain force-pushed the use-prism-automatically branch from 1b7ec6d to c2ea41b Compare March 21, 2025 09:34
@Earlopain Earlopain marked this pull request as ready for review March 21, 2025 09:39
@Earlopain Earlopain force-pushed the use-prism-automatically branch from c2ea41b to 8d4ad5a Compare March 21, 2025 09:43
@@ -109,10 +109,14 @@ for the `:prism_result` keyword argument. Otherwise, the source code is parsed.

[source,ruby]
----
# Using the Parser gem with `prism_result: nil` is the default, meaning the source code is parsed.
# Using the Parser gem with `prism_result: nil` is the default, meaning the source code is parsed until `ruby_version` is 3.4.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Using the Parser gem with `prism_result: nil` is the default, meaning the source code is parsed until `ruby_version` is 3.4.
# Using the Parser gem with `prism_result: nil` is the default, meaning the source code is parsed until `ruby_version` is 3.4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, butchered the copy-paste

# The Parser gem does not support Ruby 3.5 or later.
# It is also not fully compatible with Ruby 3.4 but for
# now respects using parser for backwards compatibility.
def best_parser_engine(ruby_version)
Copy link
Member

Choose a reason for hiding this comment

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

Can you rename it to the following?

Suggested change
def best_parser_engine(ruby_version)
def default_parser_engine(ruby_version)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that name makes good sense now

It chooses the prism parser automatically when Ruby 3.5 is selected.
In RuboCop, The `ParserEngine` must be set to `automatic` to make use of this.

In the future, the prism gem can also be chosen for Ruby 3.4

Co-authored-by: Koichi ITO <koic.ito@gmail.com>
@Earlopain Earlopain force-pushed the use-prism-automatically branch from 8d4ad5a to c17948b Compare March 21, 2025 10:09
@Earlopain Earlopain changed the title Also support Prism::Translation::Parser35 for Ruby 3.5 parser Support Prism::Translation::Parser35 for Ruby 3.5 parser Mar 21, 2025
@bbatsov bbatsov merged commit b129cc9 into rubocop:master Mar 24, 2025
22 checks passed
@bbatsov
Copy link
Contributor

bbatsov commented Mar 24, 2025

🚀

@Earlopain Earlopain deleted the use-prism-automatically branch March 24, 2025 10:32
@Earlopain
Copy link
Contributor Author

Thanks for working with me on this @koic

Earlopain added a commit to Earlopain/rubocop that referenced this pull request Mar 24, 2025
This builds on top of rubocop/rubocop-ast#370, to let `rubocop-ast` choose the parser engine automatically
Earlopain added a commit to Earlopain/rubocop that referenced this pull request Mar 24, 2025
This builds on top of rubocop/rubocop-ast#370, to let `rubocop-ast` choose the parser engine automatically
Earlopain added a commit to Earlopain/rubocop that referenced this pull request Mar 24, 2025
This builds on top of rubocop/rubocop-ast#370, to let `rubocop-ast` choose the parser engine automatically
Earlopain added a commit to Earlopain/rubocop that referenced this pull request Mar 24, 2025
This builds on top of rubocop/rubocop-ast#370, to let `rubocop-ast` choose the parser engine automatically
@Earlopain
Copy link
Contributor Author

I openend rubocop/rubocop#14025 since I already had half of it in rubocop/rubocop#14010 to make it work. Hope that's ok.

@bbatsov
Copy link
Contributor

bbatsov commented Mar 24, 2025

Of course!

Earlopain added a commit to Earlopain/rubocop that referenced this pull request Mar 25, 2025
This builds on top of rubocop/rubocop-ast#370, to let `rubocop-ast` choose the parser engine automatically
bbatsov pushed a commit to rubocop/rubocop that referenced this pull request Mar 25, 2025
This builds on top of rubocop/rubocop-ast#370, to let `rubocop-ast` choose the parser engine automatically
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