diff --git a/lib/prism/translation/parser.rb b/lib/prism/translation/parser.rb index 6e678dde6b4..0354e8195ba 100644 --- a/lib/prism/translation/parser.rb +++ b/lib/prism/translation/parser.rb @@ -43,7 +43,7 @@ def parse(source_buffer) source = source_buffer.source offset_cache = build_offset_cache(source) - result = unwrap(Prism.parse(source, filepath: source_buffer.name), offset_cache) + result = unwrap(Prism.parse(source, filepath: source_buffer.name, version: convert_for_prism(version)), offset_cache) build_ast(result.value, offset_cache) ensure @@ -56,7 +56,7 @@ def parse_with_comments(source_buffer) source = source_buffer.source offset_cache = build_offset_cache(source) - result = unwrap(Prism.parse(source, filepath: source_buffer.name), offset_cache) + result = unwrap(Prism.parse(source, filepath: source_buffer.name, version: convert_for_prism(version)), offset_cache) [ build_ast(result.value, offset_cache), @@ -75,7 +75,7 @@ def tokenize(source_buffer, recover = false) offset_cache = build_offset_cache(source) result = begin - unwrap(Prism.parse_lex(source, filepath: source_buffer.name), offset_cache) + unwrap(Prism.parse_lex(source, filepath: source_buffer.name, version: convert_for_prism(version)), offset_cache) rescue ::Parser::SyntaxError raise if !recover end @@ -168,6 +168,18 @@ def build_range(location, offset_cache) ) end + # Converts the version format handled by Parser to the format handled by Prism. + def convert_for_prism(version) + case version + when 33 + "3.3.0" + when 34 + "3.4.0" + else + "latest" + end + end + require_relative "parser/compiler" require_relative "parser/lexer" diff --git a/lib/prism/translation/parser/rubocop.rb b/lib/prism/translation/parser/rubocop.rb index 3e34fc7ace6..24e7ad70830 100644 --- a/lib/prism/translation/parser/rubocop.rb +++ b/lib/prism/translation/parser/rubocop.rb @@ -9,9 +9,12 @@ module Prism module Translation class Parser - # This is the special version number that should be used in rubocop + # 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 @@ -19,8 +22,11 @@ module ProcessedSource # list of known parsers. def parser_class(ruby_version) if ruby_version == Prism::Translation::Parser::VERSION_3_3 - require "prism/translation/parser" - Prism::Translation::Parser + require "prism/translation/parser33" + Prism::Translation::Parser33 + if ruby_version == Prism::Translation::Parser::VERSION_3_4 + require "prism/translation/parser34" + Prism::Translation::Parser34 else super end diff --git a/lib/prism/translation/parser33.rb b/lib/prism/translation/parser33.rb new file mode 100644 index 00000000000..cfaa623163a --- /dev/null +++ b/lib/prism/translation/parser33.rb @@ -0,0 +1,12 @@ +require_relative "parser" + +module Prism + module Translation + # This class is the entry-point for Ruby 3.3 of `Prism::Translation::Parser`. + class Parser33 < Parser + def version # :nodoc: + 33 + end + end + end +end diff --git a/lib/prism/translation/parser34.rb b/lib/prism/translation/parser34.rb new file mode 100644 index 00000000000..0a34758659c --- /dev/null +++ b/lib/prism/translation/parser34.rb @@ -0,0 +1,12 @@ +require_relative "parser" + +module Prism + module Translation + # This class is the entry-point for Ruby 3.4 of `Prism::Translation::Parser`. + class Parser34 < Parser + def version # :nodoc: + 34 + end + end + end +end diff --git a/prism.gemspec b/prism.gemspec index 6464cd71d63..6c314c2903b 100644 --- a/prism.gemspec +++ b/prism.gemspec @@ -87,6 +87,8 @@ Gem::Specification.new do |spec| "lib/prism/serialize.rb", "lib/prism/translation.rb", "lib/prism/translation/parser.rb", + "lib/prism/translation/parser33.rb", + "lib/prism/translation/parser34.rb", "lib/prism/translation/parser/compiler.rb", "lib/prism/translation/parser/lexer.rb", "lib/prism/translation/parser/rubocop.rb",