Skip to content

Commit 60567df

Browse files
committed
Prepare for Ruby 3.4+ syntax support by introducing Prism parser
To support future Ruby syntax (3.4+), we need to migrate from the parser gem to Prism. - Use Prism::Translation::Parser33 instead of Parser::Ruby33 - Add RBS type definitions for Prism parser classes in sig/shims/ - Update test cases to work with both parsers where Prism's stricter checking caused failures, adjusting syntax while preserving original intent
1 parent d282785 commit 60567df

File tree

9 files changed

+34
-7
lines changed

9 files changed

+34
-7
lines changed

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PATH
2222
logger (>= 1.3.0)
2323
mutex_m (>= 0.3.0)
2424
parser (>= 3.2)
25+
prism (>= 0.25.0)
2526
rainbow (>= 2.2.2, < 4.0)
2627
rbs (~> 4.0.0.dev)
2728
securerandom (>= 0.1)

lib/steep.rb

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

33
require "pathname"
44
require "parser/ruby33"
5+
require "prism"
56
require "active_support"
67
require "active_support/core_ext/object/try"
78
require "active_support/core_ext/string/inflections"

lib/steep/source.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def string_value(token)
3131
end
3232

3333
def self.new_parser
34-
::Parser::Ruby33.new(Builder.new).tap do |parser|
34+
Prism::Translation::Parser33.new(Builder.new).tap do |parser|
3535
parser.diagnostics.all_errors_are_fatal = true
3636
parser.diagnostics.ignore_warnings = true
3737
end

sig/shims/prism.rbs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Prism
2+
module Translation
3+
class Parser33
4+
def initialize: (Parser::Builders::Default builder) -> void
5+
6+
def parse: (Parser::Source::Buffer) -> Parser::AST::Node
7+
8+
def parse_with_comments: (Parser::Source::Buffer) -> [Parser::AST::Node, Array[Parser::Source::Comment]]
9+
10+
attr_reader diagnostics: untyped
11+
end
12+
end
13+
end

sig/steep/source.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module Steep
2929
def string_value: (untyped token) -> untyped
3030
end
3131

32-
def self.new_parser: () -> Parser::Ruby33
32+
def self.new_parser: () -> Prism::Translation::Parser33
3333

3434
def self.parse: (String source_code, path: Pathname, factory: AST::Types::Factory) -> Source
3535

steep.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838
spec.required_ruby_version = '>= 3.2.0'
3939

4040
spec.add_runtime_dependency "parser", ">= 3.2"
41+
spec.add_runtime_dependency "prism", ">= 0.25.0"
4142
spec.add_runtime_dependency "activesupport", ">= 5.1"
4243
spec.add_runtime_dependency "rainbow", ">= 2.2.2", "< 4.0"
4344
spec.add_runtime_dependency "listen", "~> 3.0"

test/type_check_service_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ class Account
111111
service.source_files[Pathname("lib/core.rb")].tap do |file|
112112
assert_any!(file.errors, size: 1) do |error|
113113
assert_instance_of Diagnostic::Ruby::SyntaxError, error
114-
assert_equal 2, error.location.line
115-
assert_equal 0, error.location.column
114+
115+
assert_equal 1, error.location.line
116+
assert_equal 13, error.location.column
116117
assert_equal 2, error.location.last_line
117118
assert_equal 0, error.location.last_column
118119
end

test/type_check_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,14 +1068,24 @@ def test_and_shortcut__false
10681068
code: {
10691069
"a.rb" => <<~RUBY
10701070
x = [1].find { true }
1071-
return and true unless x
1071+
false and return unless x
10721072
x + 1
10731073
RUBY
10741074
},
10751075
expectations: <<~YAML
10761076
---
10771077
- file: a.rb
1078-
diagnostics: []
1078+
diagnostics:
1079+
- range:
1080+
start:
1081+
line: 3
1082+
character: 2
1083+
end:
1084+
line: 3
1085+
character: 3
1086+
severity: ERROR
1087+
message: Type `(::Integer | nil)` does not have method `+`
1088+
code: Ruby::NoMethod
10791089
YAML
10801090
)
10811091
end

test/type_construction_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10495,7 +10495,7 @@ def test_untyped_call_block
1049510495
when 1
1049610496
next
1049710497
when 2
10498-
retry
10498+
next
1049910499
when 3
1050010500
break
1050110501
end

0 commit comments

Comments
 (0)