ruby/lib/prism/translation/parser/rubocop.rb
Koichi ITO 4d04e1bbef [ruby/prism] Deprecate TargetRubyVersion: 80_82_73_83_77.xx
Prism has been directly supported as a parser engine since RuboCop 1.62:
https://github.com/rubocop/rubocop/releases/tag/v1.62.0

This makes specifying `TargetRubyVersion` with special values like `80_82_73_83_77.33`
using the `prism/translation/parser/rubocop` file unnecessary.
As a result, it would be possible to deprecate this approach.

OTOH, early users might be surprised if `prism/translation/parser/rubocop` were to be suddenly removed.
Therefore, this PR deprecates the parameters related to `prism/translation/parser/rubocop`.

```console
$ bundle exec ruby -rrubocop -rprism/translation/parser/rubocop -e "RuboCop::AST::ProcessedSource.new('42', 80_82_73_83_77.33).ast"
WARN: Prism is directly supported since RuboCop 1.62. The `prism/translation/parser/rubocop` file is deprecated.
WARN: Setting `TargetRubyVersion: 80_82_73_83_77.33` is deprecated. Set to `ParserEngine: parser_prism` and `TargetRubyVersion: 3.3` instead.

$ bundle exec ruby -rrubocop -rprism/translation/parser/rubocop -e "RuboCop::AST::ProcessedSource.new('42', 80_82_73_83_77.34).ast"
WARN: Prism is directly supported since RuboCop 1.62. The `prism/translation/parser/rubocop` file is deprecated.
WARN: Setting `TargetRubyVersion: 80_82_73_83_77.34` is deprecated. Set to `ParserEngine: parser_prism` and `TargetRubyVersion: 3.4` instead.
```

Eventually, it will be possible to remove it at some point.

Regarding documentation, it has been updated to not show the old, discouraged usage but rather
the new way of specifying it in RuboCop.

0e4bc31463
2024-03-06 16:11:07 +00:00

73 lines
3 KiB
Ruby

# 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"
require "prism"
require "prism/translation/parser"
module Prism
module Translation
class Parser
# This is the special version numbers that should be used in RuboCop
# configuration files to trigger using prism.
# For Ruby 3.3
VERSION_3_3 = 80_82_73_83_77.33
# For Ruby 3.4
VERSION_3_4 = 80_82_73_83_77.34
# This module gets prepended into RuboCop::AST::ProcessedSource.
module ProcessedSource
# This condition is compatible with rubocop-ast versions up to 1.30.0.
if RuboCop::AST::ProcessedSource.instance_method(:parser_class).arity == 1
# Redefine parser_class so that we can inject the prism parser into the
# 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
super
end
end
else
# Redefine parser_class so that we can inject the prism parser into the
# 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
super
end
end
end
end
end
end
end
# :stopdoc:
RuboCop::AST::ProcessedSource.prepend(Prism::Translation::Parser::ProcessedSource)
known_rubies = RuboCop::TargetRuby.const_get(:KNOWN_RUBIES)
RuboCop::TargetRuby.send(:remove_const, :KNOWN_RUBIES)
RuboCop::TargetRuby::KNOWN_RUBIES = [*known_rubies, Prism::Translation::Parser::VERSION_3_3].freeze