From 1f7e2ebf71424c76384d76476df5c1dc3da9f54b Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 2 Mar 2024 01:06:06 +0900 Subject: [PATCH] Fix an error for `Prism::Translation::Parser` This PR fixes an error for `Prism::Translation::Parser` when using `case`/`when`/`end` without `then` expression. ```console $ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'p Prism::Translation::Parser33.parse("case foo when x; end").children.to_a[1].loc.begin.source' ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22] /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:1610:in `visit_when_node': undefined method `then_keyword_loc' for an instance of Prism::WhenNode (NoMethodError) if node.then_keyword_loc ^^^^^^^^^^^^^^^^^ from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:18060:in `accept' from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `block in visit_all' from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `map' from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `visit_all' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:361:in `visit_case_node' from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:3434:in `accept' from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `block in visit_all' from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `map' from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:35:in `visit_all' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:1476:in `visit_statements_node' from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:16964:in `accept' from /Users/koic/src/github.com/ruby/prism/lib/prism/compiler.rb:30:in `visit' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser/compiler.rb:1300:in `visit_program_node' from /Users/koic/src/github.com/ruby/prism/lib/prism/node.rb:14988:in `accept' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:159:in `build_ast' from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:48:in `parse' from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:33:in `parse' from -e:1:in `
' ``` --- lib/prism/translation/parser/compiler.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index 1369fe6f8..7643c4af0 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -1607,11 +1607,7 @@ def visit_when_node(node) builder.when( token(node.keyword_loc), visit_all(node.conditions), - if node.then_keyword_loc - token(node.then_keyword_loc) - else - srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset || (node.conditions.last.location.end_offset + 1), [";"]) - end, + srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset || node.statements&.then_keyword_loc, [";", "then"]), visit(node.statements) ) end