Skip to content

Commit

Permalink
Merge pull request #2558 from koic/duplicate_target_80_82_73_83_77
Browse files Browse the repository at this point in the history
Deprecate `TargetRubyVersion: 80_82_73_83_77.xx`
  • Loading branch information
kddnewton committed Mar 6, 2024
2 parents 7f5f426 + 0e4bc31 commit cb7c87d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
23 changes: 14 additions & 9 deletions docs/parser_translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ Prism::Translation::Parser.parse_file("path/to/file.rb")

### RuboCop

To run RuboCop using the `prism` gem as the parser, you will need to require the `prism/translation/parser/rubocop` file. This file injects `prism` into the known options for both `rubocop` and `rubocop-ast`, such that you can specify it in your `.rubocop.yml` file. Unfortunately `rubocop` doesn't support any direct way to do this, so we have to get a bit hacky.
Prism as a parser engine is directly supported since RuboCop 1.62. The class used for parsing is `Prism::Translation::Parser`.

First, set the `TargetRubyVersion` in your RuboCop configuration file to `80_82_73_83_77.33`. This is the version of Ruby that `prism` reports itself as. (The leading numbers are the ASCII values for `PRISM`.)
First, specify `prism` in your Gemfile:

```yaml
AllCops:
TargetRubyVersion: 80_82_73_83_77.33
```ruby
gem "prism"
```

Now when you run `rubocop` you will need to require the `prism/translation/parser/rubocop` file before executing so that it can inject the `prism` parser into the known options.
To use Prism with RuboCop, specify `ParserEngine` and `TargetRubyVersion` in your RuboCop configuration file:

```yaml
AllCops:
ParserEngine: parser_prism
TargetRubyVersion: 3.3
```
bundle exec ruby -rprism/translation/parser/rubocop $(bundle exec which rubocop)
```

This should run RuboCop using the `prism` parser.
The default value for `ParserEngine` is `parser_whitequark`, which indicates the Parser gem. You need to explicitly switch it to `parser_prism` to indicate Prism. Additionally, the value for `TargetRubyVersion` must be specified as `3.3` or higher, as Prism supports parsing versions of Ruby 3.3 and higher.
The parser class is determined by the combination of values for `ParserEngine` and `TargetRubyVersion`. For example, if `TargetRubyVersion: 3.3`, parsing is performed by `Prism::Translation::Parser33`, and for `TargetRubyVersion 3.4`, parsing is performed by `Prism::Translation::Parser34`.

For further information, please refer to the RuboCop documentation:
https://docs.rubocop.org/rubocop/configuration.html#setting-the-parser-engine
10 changes: 10 additions & 0 deletions lib/prism/translation/parser/rubocop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true
# typed: ignore

warn "WARN: Prism is directly supported since RuboCop 1.62. The `prism/translation/parser/rubocop` file is deprecated."

require "parser"
require "rubocop"

Expand All @@ -27,9 +29,13 @@ module ProcessedSource
# list of known parsers.
def parser_class(ruby_version)
if ruby_version == Prism::Translation::Parser::VERSION_3_3
warn "WARN: Setting `TargetRubyVersion: 80_82_73_83_77.33` is deprecated. " \
"Set to `ParserEngine: parser_prism` and `TargetRubyVersion: 3.3` instead."
require "prism/translation/parser33"
Prism::Translation::Parser33
elsif ruby_version == Prism::Translation::Parser::VERSION_3_4
warn "WARN: Setting `TargetRubyVersion: 80_82_73_83_77.34` is deprecated. " \
"Set to `ParserEngine: parser_prism` and `TargetRubyVersion: 3.4` instead."
require "prism/translation/parser34"
Prism::Translation::Parser34
else
Expand All @@ -41,9 +47,13 @@ def parser_class(ruby_version)
# list of known parsers.
def parser_class(ruby_version, _parser_engine)
if ruby_version == Prism::Translation::Parser::VERSION_3_3
warn "WARN: Setting `TargetRubyVersion: 80_82_73_83_77.33` is deprecated. " \
"Set to `ParserEngine: parser_prism` and `TargetRubyVersion: 3.3` instead."
require "prism/translation/parser33"
Prism::Translation::Parser33
elsif ruby_version == Prism::Translation::Parser::VERSION_3_4
warn "WARN: Setting `TargetRubyVersion: 80_82_73_83_77.34` is deprecated. " \
"Set to `ParserEngine: parser_prism` and `TargetRubyVersion: 3.4` instead."
require "prism/translation/parser34"
Prism::Translation::Parser34
else
Expand Down

0 comments on commit cb7c87d

Please sign in to comment.