|
18 | 18 |
|
19 | 19 | # Library namespace |
20 | 20 | module Unparser # rubocop:disable Metrics/ModuleLength |
21 | | - # Unparser specific AST builder defaulting to modern AST format |
22 | | - if Gem::Version.new(RUBY_VERSION) <= '3.4' |
23 | | - require 'parser/current' |
24 | | - class Builder < Parser::Builders::Default |
25 | | - modernize |
26 | | - |
27 | | - # mutant:disable |
28 | | - def initialize |
29 | | - super |
| 21 | + require 'prism' |
30 | 22 |
|
31 | | - self.emit_file_line_as_literals = false |
32 | | - end |
33 | | - end |
34 | | - else |
35 | | - require 'prism' |
36 | | - class Builder < Prism::Translation::Parser::Builder |
37 | | - modernize |
| 23 | + # Unparser specific AST builder defaulting to modern AST format |
| 24 | + class Builder < Prism::Translation::Parser::Builder |
| 25 | + modernize |
38 | 26 |
|
39 | | - # mutant:disable |
40 | | - def initialize |
41 | | - super |
| 27 | + # mutant:disable |
| 28 | + def initialize |
| 29 | + super |
42 | 30 |
|
43 | | - self.emit_file_line_as_literals = false |
44 | | - end |
| 31 | + self.emit_file_line_as_literals = false |
45 | 32 | end |
46 | 33 | end |
47 | 34 |
|
| 35 | + # Select appropriate Prism translation parser based on Ruby version |
48 | 36 | PARSER_CLASS = |
49 | | - if Gem::Version.new(RUBY_VERSION) <= '3.4' |
50 | | - Class.new(Parser::CurrentRuby) do |
51 | | - def declare_local_variable(local_variable) |
52 | | - static_env.declare(local_variable) |
53 | | - end |
54 | | - end |
| 37 | + if RUBY_VERSION >= '3.5' |
| 38 | + Prism::Translation::Parser35 |
| 39 | + elsif RUBY_VERSION >= '3.4' |
| 40 | + Prism::Translation::Parser34 |
55 | 41 | else |
56 | | - Class.new(Prism::Translation::Parser34) do |
57 | | - def declare_local_variable(local_variable) |
58 | | - (@local_variables ||= Set.new) << local_variable |
59 | | - end |
60 | | - |
61 | | - def prism_options |
62 | | - super.merge(scopes: [@local_variables.to_a]) |
63 | | - end |
64 | | - end |
| 42 | + Prism::Translation::Parser33 |
| 43 | + end |
| 44 | + |
| 45 | + # Create parser instance with local variable declaration support |
| 46 | + PARSER_INSTANCE_CLASS = Class.new(PARSER_CLASS) do |
| 47 | + def declare_local_variable(local_variable) |
| 48 | + (@local_variables ||= Set.new) << local_variable |
65 | 49 | end |
66 | 50 |
|
| 51 | + def prism_options |
| 52 | + super.merge(scopes: [@local_variables.to_a]) |
| 53 | + end |
| 54 | + end |
| 55 | + |
67 | 56 | EMPTY_STRING = ''.freeze |
68 | 57 | EMPTY_ARRAY = [].freeze |
69 | 58 |
|
@@ -238,7 +227,7 @@ def self.parse_ast(source, static_local_variables: Set.new) |
238 | 227 | # @api private |
239 | 228 | # mutant:disable |
240 | 229 | def self.parser |
241 | | - PARSER_CLASS.new(Builder.new).tap do |parser| |
| 230 | + PARSER_INSTANCE_CLASS.new(Builder.new).tap do |parser| |
242 | 231 | parser.diagnostics.tap do |diagnostics| |
243 | 232 | diagnostics.all_errors_are_fatal = true |
244 | 233 | end |
|
0 commit comments