Skip to content

Commit 43fdf3e

Browse files
committed
Deprecate Prism::Translator::Parser
Either use `Prism::Translation::Parser34` or `Parser::Translation::ParserCurrent`. It's a remnamt of when prism was only able to parse a single ruby version. Every method instantiates the parser class, so a deprecation in `initialize` is good enough. In the future this can be cleaned up, currently the autload on the parser class is in the way.
1 parent c57b28d commit 43fdf3e

File tree

16 files changed

+85
-24
lines changed

16 files changed

+85
-24
lines changed

lib/prism/translation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Prism
55
# syntax trees.
66
module Translation # steep:ignore
77
autoload :Parser, "prism/translation/parser"
8+
autoload :ParserBase, "prism/translation/parser_base"
89
autoload :ParserCurrent, "prism/translation/parser_current"
910
autoload :Parser33, "prism/translation/parser33"
1011
autoload :Parser34, "prism/translation/parser34"

lib/prism/translation/parser.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module Prism
4+
module Translation
5+
class Parser < ParserBase # :nodoc:
6+
def self.inherited(subclass)
7+
warn(<<~MSG, uplevel: 1, category: :deprecated)
8+
[deprecation]: Using `Prism::Translation::Parser` is deprecated and will be \
9+
removed in the next major version. Use `Prism::Translation::Parser34` instead.
10+
MSG
11+
super
12+
end
13+
14+
def initialize(builder = Prism::Translation::Parser::Builder.new, parser: Prism)
15+
# Filter parser frames, find first user code
16+
offset = caller(1..5)&.find_index do |loc|
17+
!loc.include?("/lib/parser/base.rb")
18+
end
19+
20+
warn(<<~MSG, uplevel: 1 + (offset || 0), category: :deprecated)
21+
[deprecation]: Using `Prism::Translation::Parser` is deprecated and will be \
22+
removed in the next major version. Use `Prism::Translation::Parser34` instead.
23+
MSG
24+
super
25+
end
26+
27+
def version # :nodoc:
28+
34
29+
end
30+
end
31+
end
32+
end

lib/prism/translation/parser/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Prism
44
module Translation
5-
class Parser
5+
class Parser < ParserBase
66
# A builder that knows how to convert more modern Ruby syntax
77
# into whitequark/parser gem's syntax tree.
88
class Builder < ::Parser::Builders::Default

lib/prism/translation/parser/compiler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Prism
44
module Translation
5-
class Parser
5+
class Parser < ParserBase
66
# A visitor that knows how to convert a prism syntax tree into the
77
# whitequark/parser gem's syntax tree.
88
class Compiler < ::Prism::Compiler

lib/prism/translation/parser/lexer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
module Prism
77
module Translation
8-
class Parser
8+
class Parser < ParserBase
99
# Accepts a list of prism tokens and converts them into the expected
1010
# format for the parser gem.
1111
class Lexer

lib/prism/translation/parser33.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
module Prism
44
module Translation
5-
# This class is the entry-point for Ruby 3.3 of `Prism::Translation::Parser`.
6-
class Parser33 < Parser
5+
# This class is the entry-point for Ruby 3.3.
6+
class Parser33 < ParserBase
77
def version # :nodoc:
88
33
99
end

lib/prism/translation/parser34.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
module Prism
44
module Translation
5-
# This class is the entry-point for Ruby 3.4 of `Prism::Translation::Parser`.
6-
class Parser34 < Parser
5+
# This class is the entry-point for Ruby 3.4.
6+
class Parser34 < ParserBase
77
def version # :nodoc:
88
34
99
end

lib/prism/translation/parser35.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
module Prism
44
module Translation
5-
# This class is the entry-point for Ruby 3.5 of `Prism::Translation::Parser`.
6-
class Parser35 < Parser
5+
# This class is the entry-point for Ruby 3.5.
6+
class Parser35 < ParserBase
77
def version # :nodoc:
88
35
99
end

lib/prism/translation/parser_base.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Translation
1818
# whitequark/parser gem's syntax tree. It inherits from the base parser for
1919
# the parser gem, and overrides the parse* methods to parse with prism and
2020
# then translate.
21-
class Parser < ::Parser::Base
21+
class ParserBase < ::Parser::Base # :nodoc:
2222
Diagnostic = ::Parser::Diagnostic # :nodoc:
2323
private_constant :Diagnostic
2424

@@ -66,7 +66,7 @@ def initialize(message, level, reason, location)
6666
def initialize(builder = Prism::Translation::Parser::Builder.new, parser: Prism)
6767
if !builder.is_a?(Prism::Translation::Parser::Builder)
6868
warn(<<~MSG, uplevel: 1, category: :deprecated)
69-
[deprecation]: The builder passed to `Prism::Translation::Parser.new` is not a \
69+
[deprecation]: The builder passed to `#{self.class}.new` is not a \
7070
`Prism::Translation::Parser::Builder` subclass. This will raise in the next major version.
7171
MSG
7272
end
@@ -75,10 +75,6 @@ def initialize(builder = Prism::Translation::Parser::Builder.new, parser: Prism)
7575
super(builder)
7676
end
7777

78-
def version # :nodoc:
79-
34
80-
end
81-
8278
# The default encoding for Ruby files is UTF-8.
8379
def default_encoding
8480
Encoding::UTF_8
@@ -359,8 +355,15 @@ def convert_for_prism(version)
359355
require_relative "parser/compiler"
360356
require_relative "parser/lexer"
361357

358+
# This can be simplified by moving `Prism::Translation::ParserBase` to
359+
# `Prism::Translation::Parser::Base` after `Prism::Translation::Parser` has been removed.
360+
Compiler = Parser::Compiler
362361
private_constant :Compiler
362+
Lexer = Parser::Lexer
363363
private_constant :Lexer
364+
365+
Parser.private_constant :Compiler
366+
Parser.private_constant :Lexer
364367
end
365368
end
366369
end

prism.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Gem::Specification.new do |spec|
9696
"lib/prism/string_query.rb",
9797
"lib/prism/translation.rb",
9898
"lib/prism/translation/parser.rb",
99+
"lib/prism/translation/parser_base.rb",
99100
"lib/prism/translation/parser_current.rb",
100101
"lib/prism/translation/parser33.rb",
101102
"lib/prism/translation/parser34.rb",
@@ -119,6 +120,7 @@ Gem::Specification.new do |spec|
119120
"rbi/prism/reflection.rbi",
120121
"rbi/prism/string_query.rbi",
121122
"rbi/prism/translation/parser.rbi",
123+
"rbi/prism/translation/parser_base.rbi",
122124
"rbi/prism/translation/parser33.rbi",
123125
"rbi/prism/translation/parser34.rbi",
124126
"rbi/prism/translation/parser35.rbi",

rbi/prism/translation/parser.rbi

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# typed: strict
22

3-
# We keep these shims in here because our client libraries might not have parser
4-
# in their bundle.
5-
module Parser; end
6-
class Parser::Base; end
7-
8-
class Prism::Translation::Parser < Parser::Base
3+
class Prism::Translation::Parser < Prism::Translation::ParserBase
94
sig { overridable.returns(Integer) }
105
def version; end
116
end

rbi/prism/translation/parser33.rbi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# typed: strict
22

3-
class Prism::Translation::Parser33 < Prism::Translation::Parser
3+
class Prism::Translation::Parser33 < Prism::Translation::ParserBase
44
sig { override.returns(Integer) }
55
def version; end
66
end

rbi/prism/translation/parser34.rbi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# typed: strict
22

3-
class Prism::Translation::Parser34 < Prism::Translation::Parser
3+
class Prism::Translation::Parser34 < Prism::Translation::ParserBase
44
sig { override.returns(Integer) }
55
def version; end
66
end

rbi/prism/translation/parser35.rbi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# typed: strict
22

3-
class Prism::Translation::Parser35 < Prism::Translation::Parser
3+
class Prism::Translation::Parser35 < Prism::Translation::ParserBase
44
sig { override.returns(Integer) }
55
def version; end
66
end

rbi/prism/translation/parser_base.rbi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# typed: strict
2+
3+
# We keep these shims in here because our client libraries might not have parser
4+
# in their bundle.
5+
module Parser; end
6+
class Parser::Base; end
7+
8+
class Prism::Translation::ParserBase < Parser::Base
9+
sig { overridable.returns(Integer) }
10+
def version; end
11+
end

test/prism/ruby/parser_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ def test_non_prism_builder_class_deprecated
155155
assert_empty(warnings)
156156
end
157157

158+
def test_version_less_parser_deprecated
159+
warnings = capture_warnings { Prism::Translation::Parser.parse("foo") }
160+
161+
assert_include(warnings, "#{__FILE__}:#{__LINE__ - 2}")
162+
assert_include(warnings, "`Prism::Translation::Parser` is deprecated")
163+
164+
warnings = capture_warnings { Prism::Translation::Parser.new }
165+
166+
assert_include(warnings, "#{__FILE__}:#{__LINE__ - 2}")
167+
assert_include(warnings, "`Prism::Translation::Parser` is deprecated")
168+
169+
warnings = capture_warnings { Class.new(Prism::Translation::Parser) }
170+
171+
assert_include(warnings, "#{__FILE__}:#{__LINE__ - 2}")
172+
assert_include(warnings, "`Prism::Translation::Parser` is deprecated")
173+
end
174+
158175
if RUBY_VERSION >= "3.3"
159176
def test_current_parser_for_current_ruby
160177
major, minor, _patch = Gem::Version.new(RUBY_VERSION).segments

0 commit comments

Comments
 (0)