Skip to content

Commit

Permalink
Merge pull request #2419 from koic/support_multi_versioning_for_prism…
Browse files Browse the repository at this point in the history
…_translation_parser

Support multi-versioning for `Prism::Translation::Parser`
  • Loading branch information
kddnewton authored Feb 15, 2024
2 parents 111acad + 62d3991 commit 184fcdf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
18 changes: 15 additions & 3 deletions lib/prism/translation/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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"

Expand Down
12 changes: 9 additions & 3 deletions lib/prism/translation/parser/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
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
# 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
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
Expand Down
12 changes: 12 additions & 0 deletions lib/prism/translation/parser33.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/prism/translation/parser34.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions prism.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 184fcdf

Please sign in to comment.